From adf7633888b2f79054f6dcff5926d2f87173aaaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A1=B0=ED=95=98=EB=8B=B4?= <35447878+ChoHadam@users.noreply.github.com> Date: Tue, 2 Jan 2024 10:21:24 +0900 Subject: [PATCH] Refactor deploy-development.yml and reload.sh for CI/CD performance (#535) * Rollback deploy file to test with Github Hosted Runner * Update branch name to push * Add step to check default github workspace path * Add path option for actions/checkout * Delete unnecessary code * Use bash instead of sh * Get the latest changes from the branch * Manage branch name with environment variable * Use '&&' to ensure each step runs successfully * Write branch name directly * Fetch all branches from remote repository * Update reload.sh to delete duplicate code and clarify roles * Remove git reset and leave only git pull * Reflect the changes so far in deployment-development.yml * Delete the file for testing using Github Hosted Runner * Delete code for checking github workspace path * Reflect the changes so far in deployment-production-1.yml * Reflect the changes so far in deployment-production-2.yml * Reflect the changes so far in deployment-production.yml --- .github/workflows/deploy-development.yml | 14 ++++--- .github/workflows/deploy-production-1.yml | 44 +++++++++++++--------- .github/workflows/deploy-production-2.yml | 44 +++++++++++++--------- .github/workflows/deploy-production.yml | 45 ++++++++++++++--------- script/reload.sh | 8 ---- 5 files changed, 91 insertions(+), 64 deletions(-) diff --git a/.github/workflows/deploy-development.yml b/.github/workflows/deploy-development.yml index ea2b3d28..f58cd28b 100644 --- a/.github/workflows/deploy-development.yml +++ b/.github/workflows/deploy-development.yml @@ -30,13 +30,17 @@ jobs: 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 + 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 }} && + git fetch --all && + git checkout ${{ env.BRANCH_NAME }} && + git pull origin ${{ env.BRANCH_NAME }} && + bash script/prebuild.sh && + bash script/reload.sh env: + BRANCH_NAME: dev NODE_ENV: development SERVER_PROFILE: development PROJECT_PATH: ~/actions-runner/_work/A.fume.Server/A.fume.Server/development diff --git a/.github/workflows/deploy-production-1.yml b/.github/workflows/deploy-production-1.yml index f4401222..12d42c29 100644 --- a/.github/workflows/deploy-production-1.yml +++ b/.github/workflows/deploy-production-1.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 production-1 server to ec2 on: @@ -10,28 +7,41 @@ on: - production-1 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@v2 - with: - path: ${{ env.SERVER_PROFILE }} + - name: Checkout Repository + uses: actions/checkout@v3 + - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v2 + 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 }} && + git fetch --all && + git checkout ${{ env.BRANCH_NAME }} && + git pull origin ${{ env.BRANCH_NAME }} && + bash script/prebuild.sh && + bash script/reload.sh + env: + BRANCH_NAME: production-1 NODE_ENV: production SERVER_PROFILE: production-1 PROJECT_PATH: ~/actions-runner/_work/A.fume.Server/A.fume.Server/production-1 diff --git a/.github/workflows/deploy-production-2.yml b/.github/workflows/deploy-production-2.yml index e442ca15..a59a5854 100644 --- a/.github/workflows/deploy-production-2.yml +++ b/.github/workflows/deploy-production-2.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 production-2 server to ec2 on: @@ -10,28 +7,41 @@ on: - production-2 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@v2 - with: - path: ${{ env.SERVER_PROFILE }} + - name: Checkout Repository + uses: actions/checkout@v3 + - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v2 + 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 }} && + git fetch --all && + git checkout ${{ env.BRANCH_NAME }} && + git pull origin ${{ env.BRANCH_NAME }} && + bash script/prebuild.sh && + bash script/reload.sh + env: + BRANCH_NAME: production-2 NODE_ENV: production SERVER_PROFILE: production-2 PROJECT_PATH: ~/actions-runner/_work/A.fume.Server/A.fume.Server/production-2 diff --git a/.github/workflows/deploy-production.yml b/.github/workflows/deploy-production.yml index f330e40e..82397e0b 100644 --- a/.github/workflows/deploy-production.yml +++ b/.github/workflows/deploy-production.yml @@ -1,36 +1,47 @@ -# 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 production server to ec2 on: + workflow_dispatch: push: branches: - main 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@v2 - with: - path: ${{ env.SERVER_PROFILE }} + - name: Checkout Repository + uses: actions/checkout@v3 + - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v2 + 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 }} && + git fetch --all && + git checkout ${{ env.BRANCH_NAME }} && + git pull origin ${{ env.BRANCH_NAME }} && + bash script/prebuild.sh && + bash script/reload.sh + env: + BRANCH_NAME: main NODE_ENV: production SERVER_PROFILE: production PROJECT_PATH: ~/actions-runner/_work/A.fume.Server/A.fume.Server/production diff --git a/script/reload.sh b/script/reload.sh index 1db728da..beb6b5b3 100755 --- a/script/reload.sh +++ b/script/reload.sh @@ -3,14 +3,6 @@ # 에러 핸들링 추가 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