11
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Github ActionsでLaravelとNuxt.jsをEC2に自動デプロイする

Last updated at Posted at 2023-02-23

はじめに

Laravelを管理するEC2と、Nuxt.jsプロジェクトを管理するEC2がそれぞれある環境に対し、
Github Actionsを用いてCI/CDを組みたかったです。

やたらNuxt側に時間がかかったこともあるので備忘録として残しておきます。
プロジェクトとしては、Nodebrewを使ってバージョン管理等を行っている状態です。

Laravelの自動デプロイ

deploy-laravel.yml
name: Deploy Laravel(API)

on:
  push:
    branches:
      - develop

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - name: Git Checkout
      uses: actions/checkout@v3

    - name: Deploy Laravel to EC2
      uses: appleboy/ssh-action@master
      with: 
        host: ${{ secrets.EC2_API_HOST_NAME }}
        username: ${{ secrets.EC2_API_USER_NAME }}
        key: ${{ secrets.EC2_PRIVATE_KEY }}
        timeout: 180s
        script: |
          cd /apps
          git fetch --prune
          git checkout develop
          git pull origin develop
          cd /apps/backend
          php artisan optimize
          php artisan config:clear
          php artisan cache:clear

Nuxtの自動デプロイ

deploy-nuxt.yml
name: Deploy Nuxt

on:
  push:
    branches:
      - develop

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - name: Git Checkout
      uses: actions/checkout@v3

    - name: Install Node.js
      uses: actions/setup-node@v3
      with:
        node-version: 16.6.0

    - name: Deploy Nuxt.js to EC2
      uses: appleboy/ssh-action@master
      with: 
        host: ${{ secrets.EC2_FRONT_HOST_NAME }}
        username: ${{ secrets.EC2_FRONT_USER_NAME }}
        key: ${{ secrets.EC2_PRIVATE_KEY }}
        timeout: 180s
        script: |
          cd /apps
          git fetch --prune
          git checkout develop
          git pull origin develop
          cd /apps/frontend
          pwd
          export PATH=/home/ec2-user/.nodebrew/current/bin:$PATH
          node --version
          npm --version
          npm run build
          pm2 restart walx-front-app-stg

謎に苦戦したところ

最初、.nodebrewのパスを通すときこのようにしていました

- name: Add Node.js to PATH
  run: echo "export PATH=/home/ec2-user/.nodebrew/current/bin:\\$PATH" >> $HOME/.bash_profile

ただ、結果そうではなく実行する直前の箇所で以下のようにパスを通すだけでした。

script: |
    cd /apps
    git fetch --prune
    git checkout develop
    git pull origin develop
    cd /apps/frontend
    pwd
    export PATH=/home/ec2-user/.nodebrew/current/bin:$PATH <-ここ
    node --version
    npm --version
    npm run build
    pm2 restart walx-front-app-stg

以上です。本当にただの備忘録...

11
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?