From ee62f6d515f1054c7567c5133b9be0f9bd0757c8 Mon Sep 17 00:00:00 2001 From: Nam Gi Beom Date: Thu, 24 Oct 2024 14:36:26 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EB=AC=B4=EC=A4=91=EB=8B=A8=20=EB=B0=B0?= =?UTF-8?q?=ED=8F=AC=20github=20action=20v1.36=20#327?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/actions/blue-green/action.yml | 45 ----- .../actions/nginx-port-forwarding/action.yml | 18 -- .github/actions/shutdown/action.yml | 15 -- .github/workflows/be-cd-prod.yml | 188 +++++++----------- .github/workflows/blue-green.yml | 60 ++++++ .github/workflows/nginx-port-forwarding.yml | 47 +++++ .github/workflows/shutdown.yml | 20 ++ .../scripts/change_nginx_port_forwarding.sh | 14 +- 8 files changed, 203 insertions(+), 204 deletions(-) delete mode 100644 .github/actions/blue-green/action.yml delete mode 100644 .github/actions/nginx-port-forwarding/action.yml delete mode 100644 .github/actions/shutdown/action.yml create mode 100644 .github/workflows/blue-green.yml create mode 100644 .github/workflows/nginx-port-forwarding.yml create mode 100644 .github/workflows/shutdown.yml diff --git a/.github/actions/blue-green/action.yml b/.github/actions/blue-green/action.yml deleted file mode 100644 index 78214905..00000000 --- a/.github/actions/blue-green/action.yml +++ /dev/null @@ -1,45 +0,0 @@ -name: Blue Green Deployment - -inputs: - artifact_name: - description: 'uploaded artifact name' - required: true - type: string - jar_name: - description: 'uploaded jar name' - required: true - type: string - profile: - description: 'profile' - required: true - type: string - app_path: - description: 'app path' - required: true - type: string -outputs: - green_port: ${{ steps.blue_green_port.outputs.green_port }} - blue_port: ${{ steps.blue_green_port.outputs.blue_port }} - -runs: - using: 'composite' - steps: - - name: Download artifact file - uses: actions/download-artifact@v4 - with: - name: ${{ inputs.artifact_name }} - path: ${{ inputs.app_path }} - - - name: Change permission of shell script - run: chmod +x ${{ inputs.app_path }}/*.sh - - - name: Get blue green port - id: blue_green_port - run: ${{ inputs.app_path }}/get_blue_green_port.sh | awk '{print $0}' >> $GITHUB_OUTPUT - - - name: Run green java application - run: sudo nohup java -Dspring.profiles.active=${{ inputs.profile }} -Dserver.port=${{ steps.blue_green_port.outputs.green_port }} -Duser.timezone=Asia/Seoul -jar ${{ inputs.app_path }}/${{ inputs.jar_name }} & - - - name: Health check green - run: | - ${{ inputs.app_path }}/green_health_check.sh ${{ steps.blue_green_port.outputs.green_port }} diff --git a/.github/actions/nginx-port-forwarding/action.yml b/.github/actions/nginx-port-forwarding/action.yml deleted file mode 100644 index ce87d0f3..00000000 --- a/.github/actions/nginx-port-forwarding/action.yml +++ /dev/null @@ -1,18 +0,0 @@ -name: Update Nginx Port Forwarding - -inputs: - app_path: - description: 'app path' - required: true - type: string - new_port: - description: 'new port for Nginx port forwarding' - required: true - type: string - -runs: - using: 'composite' - steps: - - name: Update Nginx port forwarding to ${{ inputs.new_port }} - run: | - ${{ inputs.app_path }}/change_nginx_port_forwarding.sh ${{ inputs.new_port }} diff --git a/.github/actions/shutdown/action.yml b/.github/actions/shutdown/action.yml deleted file mode 100644 index a79f8b8b..00000000 --- a/.github/actions/shutdown/action.yml +++ /dev/null @@ -1,15 +0,0 @@ -name: Shutdown Server - -inputs: - port: - description: 'shutdown port' - required: true - type: string - -runs: - using: 'composite' - steps: - - name: Shutdown - run: | - echo "Shutdown server - port: ${{ inputs.port }}" - sudo lsof -t -i:${{ inputs.port }} | xargs sudo kill -9 diff --git a/.github/workflows/be-cd-prod.yml b/.github/workflows/be-cd-prod.yml index 1dbbe27d..0b0e6b43 100644 --- a/.github/workflows/be-cd-prod.yml +++ b/.github/workflows/be-cd-prod.yml @@ -8,10 +8,6 @@ on: paths: - backend/** -env: - APP_PATH: ~/app - PROFILE: prod - jobs: build: uses: ./.github/workflows/build.yml @@ -22,39 +18,27 @@ jobs: deploy-a: needs: [ build ] - runs-on: [ prod-c ] # todo a - outputs: - blue_port: ${{ steps.blue-green.outputs.blue_port }} - green_port: ${{ steps.blue-green.outputs.green_port }} - steps: - - name: Deploy blue/green in A - id: blue-green - uses: ./.github/actions/blue-green - with: - artifact_name: ${{ needs.build.outputs.artifact_name }} - jar_name: ${{ needs.build.outputs.jar_name }} - profile: ${{ env.PROFILE }} - app_path: ${{ env.APP_PATH }} + uses: ./.github/workflows/blue-green.yml + with: + self_hosted_runner: prod-c # todo a + artifact_name: ${{ needs.build.outputs.artifact_name }} + jar_name: ${{ needs.build.outputs.jar_name }} + profile: prod + app_path: ~/app deploy-b: needs: [ build ] - runs-on: [ prod-d ] # todo b - outputs: - blue_port: ${{ steps.blue-green.outputs.blue_port }} - green_port: ${{ steps.blue-green.outputs.green_port }} - steps: - - name: Deploy blue/green in B - id: blue-green - uses: ./.github/actions/blue-green - with: - artifact_name: ${{ needs.build.outputs.artifact_name }} - jar_name: ${{ needs.build.outputs.jar_name }} - profile: ${{ env.PROFILE }} - app_path: ${{ env.APP_PATH }} + uses: ./.github/workflows/blue-green.yml + with: + self_hosted_runner: prod-d # todo b + artifact_name: ${{ needs.build.outputs.artifact_name }} + jar_name: ${{ needs.build.outputs.jar_name }} + profile: prod + app_path: ~/app detect-deploy-failure: needs: [ deploy-a, deploy-b ] - if: failure() && contains(needs.*.result, 'failure') + if: failure() && (needs.deploy-a.result == 'failure' || needs.deploy-b.result == 'failure') runs-on: ubuntu-latest outputs: green_port_a: ${{ needs.deploy-a.outputs.green_port }} @@ -65,124 +49,86 @@ jobs: rollback-a: needs: [ detect-deploy-failure ] - if: failure() && contains(needs.*.result, 'success') - runs-on: [ prod-c ] # todo a - steps: - - name: Shutdown green in A - uses: ./.github/actions/shutdown - with: - port: ${{ needs.detect-deploy-failure.outputs.green_port_a }} + if: failure() && needs.detect-deploy-failure.result == 'success' + uses: ./.github/workflows/shutdown.yml + with: + self_hosted_runner: prod-c # todo a + port: ${{ needs.detect-deploy-failure.outputs.green_port_a }} rollback-b: needs: [ detect-deploy-failure ] - if: failure() && contains(needs.*.result, 'success') - runs-on: [ prod-d ] # todo b - steps: - - name: Shutdown green in B - uses: ./.github/actions/shutdown - with: - port: ${{ needs.detect-deploy-failure.outputs.green_port_b }} + if: failure() && needs.detect-deploy-failure.result == 'success' + uses: ./.github/workflows/shutdown.yml + with: + self_hosted_runner: prod-d # todo b + port: ${{ needs.detect-deploy-failure.outputs.green_port_b }} configure-nginx-a: needs: [ deploy-a ] - runs-on: [ prod-c ] # todo a - outputs: - blue_port: ${{ needs.deploy-a.outputs.blue_port }} - green_port: ${{ needs.deploy-a.outputs.green_port }} - steps: - - name: Change Nginx port forwarding to green port in A - uses: ./.github/actions/nginx-port-forwarding - with: - app_path: ${{ env.APP_PATH }} - new_port: ${{ needs.deploy-a.outputs.green_port }} - - - name: Reload Nginx - run: sudo nginx -s reload # todo 에러 처리 + uses: ./.github/workflows/nginx-port-forwarding.yml + with: + self_hosted_runner: prod-c # todo a + app_path: ~/app + old_port: ${{ needs.deploy-a.outputs.blue_port }} + new_port: ${{ needs.deploy-a.outputs.green_port }} configure-nginx-b: needs: [ deploy-b ] - runs-on: [ prod-d ] # todo b - outputs: - blue_port: ${{ needs.deploy-b.outputs.blue_port }} - green_port: ${{ needs.deploy-b.outputs.green_port }} - steps: - - name: Update Nginx port forwarding to green port in B - uses: ./.github/actions/nginx-port-forwarding - with: - app_path: ${{ env.APP_PATH }} - new_port: ${{ needs.deploy-b.outputs.green_port }} - - - name: Reload Nginx - run: sudo nginx -s reload # todo 에러 처리 + uses: ./.github/workflows/nginx-port-forwarding.yml + with: + self_hosted_runner: prod-d # todo b + app_path: ~/app + old_port: ${{ needs.deploy-b.outputs.blue_port }} + new_port: ${{ needs.deploy-b.outputs.green_port }} detect-configure-nginx-faliure: needs: [ configure-nginx-a, configure-nginx-b ] - if: failure() && contains(needs.*.result, 'failure') + if: failure() && (needs.configure-nginx-a.result == 'failure' || needs.configure-nginx-b.result == 'failure') runs-on: ubuntu-latest outputs: - blue_port_a: ${{ needs.configure-nginx-a.outputs.blue_port }} - green_port_a: ${{ needs.configure-nginx-a.outputs.green_port }} - blue_port_b: ${{ needs.configure-nginx-b.outputs.blue_port }} - green_port_b: ${{ needs.configure-nginx-b.outputs.green_port }} + old_port_a: ${{ needs.configure-nginx-a.outputs.old_port }} + new_port_a: ${{ needs.configure-nginx-a.outputs.new_port }} + old_port_b: ${{ needs.configure-nginx-b.outputs.old_port }} + new_port_b: ${{ needs.configure-nginx-b.outputs.new_port }} steps: - name: Send notification to Discord # todo run: echo "테스트입니다" - + rollback-nginx-a: needs: [ detect-configure-nginx-faliure ] - if: failure() && contains(needs.*.result, 'success') - runs-on: [ prod-c ] # todo a - steps: - - name: Rollback Nginx port forwarding to blue port in A - uses: ./.github/actions/nginx-port-forwarding - with: - app_path: ${{ env.APP_PATH }} - new_port: ${{ needs.detect-configure-nginx-faliure.outputs.blue_port_a }} - - - name: Reload Nginx - run: sudo nginx -s reload # todo 에러 처리 - - - name: Shutdown green in A - uses: ./.github/actions/shutdown - with: - port: ${{ needs.detect-configure-nginx-faliure.outputs.green_port_a }} + if: failure() && needs.detect-configure-nginx-faliure.result == 'success' + uses: ./.github/workflows/nginx-port-forwarding.yml + with: + self_hosted_runner: prod-c # todo a + app_path: ~/app + old_port: ${{ needs.detect-configure-nginx-faliure.outputs.new_port_a }} + new_port: ${{ needs.detect-configure-nginx-faliure.outputs.old_port_a }} + with_shutdown: true rollback-nginx-b: needs: [ detect-configure-nginx-faliure ] - if: failure() && contains(needs.*.result, 'success') - runs-on: [ prod-d ] # todo b - steps: - - name: Rollback Nginx port forwarding to blue port in B - uses: ./.github/actions/nginx-port-forwarding - with: - app_path: ${{ env.APP_PATH }} - new_port: ${{ needs.detect-configure-nginx-faliure.outputs.blue_port_b }} - - - name: Reload Nginx - run: sudo nginx -s reload # todo 에러 처리 - - - name: Shutdown green in B - uses: ./.github/actions/shutdown - with: - port: ${{ needs.detect-configure-nginx-faliure.outputs.green_port_b }} + if: failure() && needs.detect-configure-nginx-faliure.result == 'success' + uses: ./.github/workflows/nginx-port-forwarding.yml + with: + self_hosted_runner: prod-d # todo b + app_path: ~/app + old_port: ${{ needs.detect-configure-nginx-faliure.outputs.new_port_b }} + new_port: ${{ needs.detect-configure-nginx-faliure.outputs.old_port_b }} + with_shutdown: true blue-shutdown-a: needs: [ configure-nginx-a ] - runs-on: [ prod-c ] # todo a - steps: - - name: Shutdown blue in A - uses: ./.github/actions/shutdown - with: - port: ${{ needs.configure-nginx-a.outputs.blue_port }} + uses: ./.github/workflows/shutdown.yml + with: + self_hosted_runner: prod-c # todo a + port: ${{ needs.configure-nginx-a.outputs.old_port }} blue-shutdown-b: needs: [ configure-nginx-b ] - runs-on: [ prod-d ] # todo b - steps: - - name: Shutdown blue in B - uses: ./.github/actions/shutdown - with: - port: ${{ needs.configure-nginx-b.outputs.blue_port }} + uses: ./.github/workflows/shutdown.yml + with: + self_hosted_runner: prod-d # todo b + port: ${{ needs.configure-nginx-b.outputs.old_port }} # on-failure: # needs: [ blue-down ] # 알림 diff --git a/.github/workflows/blue-green.yml b/.github/workflows/blue-green.yml new file mode 100644 index 00000000..f2d4648c --- /dev/null +++ b/.github/workflows/blue-green.yml @@ -0,0 +1,60 @@ +name: Blue Green Deployment + +on: + workflow_call: + inputs: + self_hosted_runner: + description: 'self hosted runner label' + required: true + type: string + artifact_name: + description: 'uploaded artifact name' + required: true + type: string + jar_name: + description: 'uploaded jar name' + required: true + type: string + profile: + description: 'profile' + required: true + type: string + app_path: + description: 'app path' + required: true + type: string + outputs: + green_port: + value: ${{ jobs.deploy-green.outputs.green_port }} + blue_port: + value: ${{ jobs.deploy-green.outputs.blue_port }} + +jobs: + deploy-green: + runs-on: ${{ inputs.self_hosted_runner }} + outputs: + green_port: ${{ steps.blue_green_port.outputs.green_port }} + blue_port: ${{ steps.blue_green_port.outputs.blue_port }} + steps: + - name: Download artifact file + uses: actions/download-artifact@v4 + with: + name: ${{ inputs.artifact_name }} + path: ${{ inputs.app_path }} + + - name: Change permission of shell script + run: chmod +x ${{ inputs.app_path }}/*.sh + + - name: Get blue green port + id: blue_green_port + run: ${{ inputs.app_path }}/get_blue_green_port.sh | awk '{print $0}' >> $GITHUB_OUTPUT + + - name: Run green java application in ${{ inputs.self_hosted_runner }} + run: sudo nohup java -Dspring.profiles.active=${{ inputs.profile }} -Dserver.port=${{ steps.blue_green_port.outputs.green_port }} -Duser.timezone=Asia/Seoul -jar ${{ inputs.app_path }}/${{ inputs.jar_name }} & + + health_check: + needs: [ deploy-green ] + runs-on: ${{ inputs.self_hosted_runner }} + steps: + - name: Health check green + run: ${{ inputs.app_path }}/green_health_check.sh ${{ needs.deploy-green.outputs.green_port }} diff --git a/.github/workflows/nginx-port-forwarding.yml b/.github/workflows/nginx-port-forwarding.yml new file mode 100644 index 00000000..8d339abc --- /dev/null +++ b/.github/workflows/nginx-port-forwarding.yml @@ -0,0 +1,47 @@ +name: Update Nginx Port Forwarding + +on: + workflow_call: + inputs: + self_hosted_runner: + description: 'self hosted runner label' + required: true + type: string + app_path: + description: 'app path' + required: true + type: string + old_port: + description: 'old port' + required: true + type: string + new_port: + description: 'new port for Nginx port forwarding' + required: true + type: string + with_shutdown: + description: 'shutdown before update' + required: false + type: boolean + outputs: + old_port: + value: ${{ inputs.old_port }} + new_port: + value: ${{ inputs.new_port }} + +jobs: + shutdown: + if: ${{ inputs.with_shutdown }} + uses: ./.github/workflows/shutdown.yml + with: + self_hosted_runner: ${{ inputs.self_hosted_runner }} + port: ${{ inputs.old_port }} + + update-nginx-port-forwarding: + runs-on: ${{ inputs.self_hosted_runner }} + steps: + - name: Update Nginx port forwarding from ${{ inputs.old_port }} to ${{ inputs.new_port }} + run: ${{ inputs.app_path }}/change_nginx_port_forwarding.sh ${{ inputs.old_port }} ${{ inputs.new_port }} + + - name: Reload Nginx + run: sudo nginx -s reload # todo 에러 처리 diff --git a/.github/workflows/shutdown.yml b/.github/workflows/shutdown.yml new file mode 100644 index 00000000..b9d4a98a --- /dev/null +++ b/.github/workflows/shutdown.yml @@ -0,0 +1,20 @@ +name: Shutdown Server + +on: + workflow_call: + inputs: + self_hosted_runner: + description: 'self hosted runner label' + required: true + type: string + port: + description: 'shutdown port' + required: true + type: string + +jobs: + shutdown: + runs-on: ${{ inputs.self_hosted_runner }} + steps: + - name: Shutdown server - port ${{ inputs.port }} + run: sudo lsof -t -i:${{ inputs.port }} | xargs sudo kill -9 diff --git a/backend/scripts/change_nginx_port_forwarding.sh b/backend/scripts/change_nginx_port_forwarding.sh index 8e396bd5..44605e1e 100755 --- a/backend/scripts/change_nginx_port_forwarding.sh +++ b/backend/scripts/change_nginx_port_forwarding.sh @@ -1,13 +1,17 @@ #!/bin/bash NGINX_CONFIG="/etc/nginx/sites-available/default" -PORT=$1 +OLD_PORT=$1 +NEW_PORT=$2 -CURRENT_PORT=$(sudo grep -oP '127.0.0.1:\K\d+' "$NGINX_CONFIG") +if [ -z "$OLD_PORT" ] || [ -z "$NEW_PORT" ]; then + echo "Invalid arguments: OLD_PORT=$OLD_PORT, NEW_PORT=$NEW_PORT" + exit 1 +fi -if [ -n "$CURRENT_PORT" ]; then - sudo sed -i "s/127.0.0.1:$CURRENT_PORT/127.0.0.1:$PORT/g" "$NGINX_CONFIG" - echo "Switching from ($CURRENT_PORT) to ($PORT)" +if sudo grep -q "127\.0\.0\.1:$OLD_PORT" "$NGINX_CONFIG"; then + sudo sed -i "s/127.0.0.1:$OLD_PORT/127.0.0.1:$NEW_PORT/g" "$NGINX_CONFIG" + echo "Switching from $OLD_PORT to $NEW_PORT" else echo "Can't find port forwarding format in nginx configuration" exit 1