diff --git a/.github/workflows/deploy-development.yml b/.github/workflows/deploy-development.yml index c15bc545..ea2b3d28 100644 --- a/.github/workflows/deploy-development.yml +++ b/.github/workflows/deploy-development.yml @@ -1,6 +1,3 @@ -# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node -# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions - name: Deploy development server to ec2 on: @@ -9,27 +6,36 @@ on: - dev jobs: - build: - runs-on: self-hosted + deploy: + runs-on: ubuntu-latest + strategy: matrix: node-version: [16.x] - # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ - defaults: - run: - working-directory: ${{ env.SERVER_PROFILE }} + steps: - - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 #v3.1.0 - with: - path: ${{ env.SERVER_PROFILE }} + - name: Checkout Repository + uses: actions/checkout@v3 + - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 #v.3.5.1 + uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} - - run: cp ~/config-injection/ecosystem-${{ env.SERVER_PROFILE }}.json ${{ env.PROJECT_PATH }}/ecosystem.json - - run: cp ~/config-injection/envs/.env.${{ env.NODE_ENV }} ${{ env.PROJECT_PATH }}/.env - - run: bash ${{ env.PROJECT_PATH }}/script/prebuild.sh - - run: sh ${{ env.PROJECT_PATH }}/script/reload.sh + + - name: Deploy to EC2 using SSH + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.SERVER_HOST }} + username: ${{ secrets.SSH_USERNAME }} + key: ${{ secrets.SSH_PRIVATE_KEY }} + port: 22 + script: | + cp ~/config-injection/ecosystem-${{ env.SERVER_PROFILE }}.json ${{ env.PROJECT_PATH }}/ecosystem.json + cp ~/config-injection/envs/.env.${{ env.NODE_ENV }} ${{ env.PROJECT_PATH }}/.env + cd ${{ env.PROJECT_PATH }} + bash script/prebuild.sh + sh script/reload.sh + env: NODE_ENV: development SERVER_PROFILE: development diff --git a/script/prebuild.sh b/script/prebuild.sh index 3a8a83a9..f2bc5326 100755 --- a/script/prebuild.sh +++ b/script/prebuild.sh @@ -1,7 +1,18 @@ #!/bin/bash +# 에러 핸들링 추가 +set -e + +# ecosystem.json 파일이 존재하면 pm2로 실행 중인 프로세스 중지 if [[ -f "ecosystem.json" ]]; then - pm2 stop ecosystem.json + pm2 stop ecosystem.json || true fi -npm ci \ No newline at end of file +# 종속성 설치 +npm ci || exit 1 + +# 프로젝트 빌드 +npm run build || exit 1 + +# Exit code를 출력하여 스크립트가 성공적으로 실행되었는지 확인 +echo $? \ No newline at end of file diff --git a/script/reload.sh b/script/reload.sh index c8af0c4a..1db728da 100755 --- a/script/reload.sh +++ b/script/reload.sh @@ -1,6 +1,17 @@ -#!/bin/sh +#!/bin/bash -pm2 stop ecosystem.json -npm run build -pm2 restart ecosystem.json -echo $? +# 에러 핸들링 추가 +set -e + +# ecosystem.json 파일이 존재하면 pm2로 실행 중인 프로세스를 중지 +if [[ -f "ecosystem.json" ]]; then + pm2 stop ecosystem.json || true +fi + +# npm run build 명령어로 프로젝트 빌드 +npm run build || exit 1 + +# ecosystem.json 사용하여 pm2로 프로세스 재시작 +if [[ -f "ecosystem.json" ]]; then + pm2 restart ecosystem.json || exit 1 +fi \ No newline at end of file