Skip to content

Commit

Permalink
fix: 무중단 배포 github action v1.36 #327
Browse files Browse the repository at this point in the history
  • Loading branch information
GIVEN53 committed Oct 24, 2024
1 parent ca98fff commit ee62f6d
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 204 deletions.
45 changes: 0 additions & 45 deletions .github/actions/blue-green/action.yml

This file was deleted.

18 changes: 0 additions & 18 deletions .github/actions/nginx-port-forwarding/action.yml

This file was deleted.

15 changes: 0 additions & 15 deletions .github/actions/shutdown/action.yml

This file was deleted.

188 changes: 67 additions & 121 deletions .github/workflows/be-cd-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ on:
paths:
- backend/**

env:
APP_PATH: ~/app
PROFILE: prod

jobs:
build:
uses: ./.github/workflows/build.yml
Expand All @@ -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 }}
Expand All @@ -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 ] # 알림
Expand Down
60 changes: 60 additions & 0 deletions .github/workflows/blue-green.yml
Original file line number Diff line number Diff line change
@@ -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 }}
Loading

0 comments on commit ee62f6d

Please sign in to comment.