Skip to content

Commit

Permalink
merge: merge develop #369
Browse files Browse the repository at this point in the history
  • Loading branch information
PgmJun committed Oct 24, 2024
2 parents 1b17ab5 + 2bc12bc commit 4bbb033
Show file tree
Hide file tree
Showing 34 changed files with 564 additions and 332 deletions.
174 changes: 96 additions & 78 deletions .github/workflows/be-cd-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,104 +8,122 @@ on:
paths:
- backend/**

env:
ARTIFACT_NAME: app-artifact
ARTIFACT_PATH: ./artifact
JAR_NAME: app.jar

jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./backend
outputs:
artifact_name: ${{ env.ARTIFACT_NAME }}
jar_name: ${{ env.JAR_NAME }}

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setting prod-secret.yml
run: |
echo "${{ secrets.PROD_SECRET_YML }}" > ./src/main/resources/prod-secret.yml
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'

- name: BootJar with Gradle
run: ./gradlew bootJar

- name: Move artifact file
run: mv build/libs/*.jar ${{ env.ARTIFACT_PATH }}/${{ env.JAR_NAME }}

- name: Copy scripts file
run: cp scripts/ ${{ env.ARTIFACT_PATH }}/

- name: Upload artifact file
uses: actions/upload-artifact@v4
with:
name: ${{ env.ARTIFACT_NAME }}
path: ${{ env.ARTIFACT_PATH }}
uses: ./.github/workflows/build.yml
with:
profile: prod
secrets:
secret_yml: ${{ secrets.PROD_SECRET_YML }}

deploy-a:
needs: build
uses: ./.github/workflows/self-hosted-runner-deployment.yml
needs: [ build ]
uses: ./.github/workflows/blue-green.yml
with:
self_hosted_runner: prod-c # 추후 변경 prod-a
self_hosted_runner: prod-a
artifact_name: ${{ needs.build.outputs.artifact_name }}
jar_name: ${{ needs.build.outputs.jar_name }}
profile: prod
app_path: ~/app

deploy-b:
needs: build
uses: ./.github/workflows/self-hosted-runner-deployment.yml
needs: [ build ]
uses: ./.github/workflows/blue-green.yml
with:
self_hosted_runner: prod-d # 추후 변경 prod-b
self_hosted_runner: prod-b
artifact_name: ${{ needs.build.outputs.artifact_name }}
jar_name: ${{ needs.build.outputs.jar_name }}
profile: prod
app_path: ~/app

rollback-a:
name: "[Failure] Rollback Deploy A"
needs: [ deploy-a, deploy-b ]
if: failure() && (needs.deploy-a.result == 'failure' || needs.deploy-b.result == 'failure')
uses: ./.github/workflows/shutdown.yml
with:
self_hosted_runner: prod-a
port: ${{ needs.deploy-a.outputs.green_port }}

rollback-b:
name: "[Failure] Rollback Deploy B"
needs: [ deploy-a, deploy-b ]
if: failure() && (needs.deploy-a.result == 'failure' || needs.deploy-b.result == 'failure')
uses: ./.github/workflows/shutdown.yml
with:
self_hosted_runner: prod-b
port: ${{ needs.deploy-b.outputs.green_port }}

error-handling:
deploy-failure-notification:
name: "[Failure] Deploy Failure Notification"
needs: [ deploy-a, deploy-b ]
if: failure() && (needs.deploy-a.result == 'failure' || needs.deploy-b.result == 'failure')
runs-on: ubuntu-latest
if: ${{ failure() }}
steps:
- name: Debug
run: echo "Deploy failed" # 추후 수정 + green 롤백 + 디스코드 알림
- name: Send notification to Discord # todo
run: echo "테스트입니다"

change-nginx-config-a:
needs: [ deploy-a, error-handling]
uses: ./.github/workflows/change_nginx_config.yml
configure-nginx-a:
needs: [ deploy-a, deploy-b ]
uses: ./.github/workflows/nginx-port-forwarding.yml
with:
self_hosted_runner: prod-c # 추후 변경 prod-a
green_port: ${{ needs.deploy-a.outputs.green_port }}

change-nginx-config-b:
needs: [ deploy-b, error-handling]
uses: ./.github/workflows/change_nginx_config.yml
self_hosted_runner: prod-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-a, deploy-b ]
uses: ./.github/workflows/nginx-port-forwarding.yml
with:
self_hosted_runner: prod-d # 추후 변경 prod-b
green_port: ${{ needs.deploy-b.outputs.green_port }}

on-failure:
needs: [change-nginx-config-a, change-nginx-config-b]
runs-on: [ prod-c, prod-d ] # 추후 변경 prod-a, prod-b
if: failure()
self_hosted_runner: prod-b
app_path: ~/app
old_port: ${{ needs.deploy-b.outputs.blue_port }}
new_port: ${{ needs.deploy-b.outputs.green_port }}

rollback-nginx-a:
name: "[Failure] Rollback Nginx A"
needs: [ configure-nginx-a, configure-nginx-b ]
if: failure() && (needs.configure-nginx-a.result == 'failure' || needs.configure-nginx-b.result == 'failure')
uses: ./.github/workflows/nginx-port-forwarding.yml
with:
self_hosted_runner: prod-a
app_path: ~/app
old_port: ${{ needs.configure-nginx-a.outputs.new_port }}
new_port: ${{ needs.configure-nginx-a.outputs.old_port }}
old_shutdown: true

rollback-nginx-b:
name: "[Failure] Rollback Nginx B"
needs: [ configure-nginx-a, configure-nginx-b ]
if: failure() && (needs.configure-nginx-a.result == 'failure' || needs.configure-nginx-b.result == 'failure')
uses: ./.github/workflows/nginx-port-forwarding.yml
with:
self_hosted_runner: prod-b
app_path: ~/app
old_port: ${{ needs.configure-nginx-b.outputs.new_port }}
new_port: ${{ needs.configure-nginx-b.outputs.old_port }}
old_shutdown: true

configure-nginx-faliure-notification:
name: "[Failure] Nginx Failure Notification"
needs: [ configure-nginx-a, configure-nginx-b ]
if: failure() && (needs.configure-nginx-a.result == 'failure' || needs.configure-nginx-b.result == 'failure')
runs-on: ubuntu-latest
steps:
- name: Rollback
run: |
sudo kill -15 $(sudo lsof -t -i: ${{ needs.deploy-a.outputs.green_port }})
- name: Send notification to Discord # todo
run: echo "테스트입니다"

blue-shutdown-a:
needs: [ configure-nginx-a, configure-nginx-b ]
uses: ./.github/workflows/shutdown.yml
with:
self_hosted_runner: prod-a
port: ${{ needs.configure-nginx-a.outputs.old_port }}

on-success:
needs: [change-nginx-config-a, change-nginx-config-b]
runs-on: [ prod-c, prod-d ] # 추후 변경 prod-a, prod-b
if: success()
steps:
- name: Blue Down
run: |
sudo kill -15 $(sudo lsof -t -i: 8080) # todo blue port
blue-shutdown-b:
needs: [ configure-nginx-a, configure-nginx-b ]
uses: ./.github/workflows/shutdown.yml
with:
self_hosted_runner: prod-b
port: ${{ needs.configure-nginx-b.outputs.old_port }}
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 }}
68 changes: 68 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: backend build jar file and upload artifact file

on:
workflow_call:
inputs:
profile:
description: 'profile'
required: true
type: string
artifact_name:
description: 'artifact name'
default: 'app-artifact'
required: false
type: string
jar_name:
description: 'jar name'
default: 'app.jar'
required: false
type: string
gradlew_options:
description: 'gradle options'
required: false
type: string
secrets:
secret_yml:
description: 'secret yml'
required: true
outputs:
artifact_name:
value: ${{ inputs.artifact_name }}
jar_name:
value: ${{ inputs.jar_name }}

jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./backend

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setting ${{ inputs.profile }}-secret.yml
run: |
echo "${{ secrets.secret_yml }}" > ./src/main/resources/${{ inputs.profile }}-secret.yml
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'

- name: BootJar with Gradle
run: ./gradlew bootJar ${{ inputs.gradlew_options }}

- name: Move artifact file # todo script 환경 분리
run: |
mkdir -p ${{ inputs.artifact_name }} && \
mv build/libs/*.jar ${{ inputs.artifact_name }}/${{ inputs.jar_name }} && \
mv scripts/* ${{ inputs.artifact_name }}/
- name: Upload artifact file
uses: actions/upload-artifact@v4
with:
name: ${{ inputs.artifact_name }}
path: ./backend/${{ inputs.artifact_name }}
28 changes: 0 additions & 28 deletions .github/workflows/change_nginx_config.yml

This file was deleted.

Loading

0 comments on commit 4bbb033

Please sign in to comment.