Skip to content

Backend None Stop CD #90

Backend None Stop CD

Backend None Stop CD #90

Workflow file for this run

name: Backend None Stop CD
on:
workflow_dispatch:
push:
branches:
- main
paths:
- backend/**
jobs:
find_previous_image_version:
name: 🔎Find Previous Docker Image Version
runs-on: [ self-hosted, devel-up-prod-a ]
outputs:
previousImageVersion: ${{steps.find_version.outputs.name}}
steps:
- id: find_version
run: |
PREVIOUS_IMAGE_NAME=$(docker ps --format "{{.Image}}")
echo "name=$PREVIOUS_IMAGE_NAME" >> $GITHUB_OUTPUT
echo $PREVIOUS_IMAGE_NAME
build:
name: 🏗️ Build Jar and Upload Docker Image
runs-on: ubuntu-latest
defaults:
run:
working-directory: backend
steps:
- uses: actions/checkout@v4
with:
submodules: true
token: ${{ secrets.SUBMODULE_GITHUB_TOKEN }}
- name: 🏗️ Set up JDK 21
uses: actions/setup-java@v4
with:
distribution: 'corretto'
java-version: 21
- name: 🏗️ Set up Gradle
uses: gradle/actions/setup-gradle@v3
- name: 🏗️ Build with Gradle
run: ./gradlew clean bootJar
- name: 🐳 Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: 🐳 Docker Image Build and Push
uses: docker/build-push-action@v6
with:
context: ./backend
push: true
tags: ${{ secrets.DOCKER_REPOSITORY_NAME }}:${{ github.sha }}
platforms: linux/arm64
deploy_to_a:
name: 🚀 Server A Deployment
needs: build
runs-on: [ self-hosted, devel-up-prod-a ]
defaults:
run:
working-directory: backend
env:
BACKEND_APP_IMAGE_NAME: ${{ secrets.DOCKER_REPOSITORY_NAME }}:${{ github.sha }}
steps:
- uses: actions/checkout@v4
- name: 🐳 Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: 🐳 Docker Compose up
run: docker compose -f compose.yml up -d
health_check_a:
name: 🙏 Server A Health Check
needs: deploy_to_a
defaults:
run:
working-directory: backend
runs-on: [ self-hosted, devel-up-prod-a ]
steps:
- name: ♻️ Send Helth Check Request
run: chmod u+x ./scripts/healthcheck.sh && ./scripts/healthcheck.sh
roll-back_a:
name: 🚀 Server A RollBack
needs: [health_check_a, find_previous_image_version]
if: failure()
runs-on: [ self-hosted, devel-up-prod-a ]
defaults:
run:
working-directory: backend
env:
BACKEND_APP_IMAGE_NAME: ${{ needs.find_previous_image_version.outputs.previousImageVersion }}
steps:
- uses: actions/checkout@v4
- name: 🐳 Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: 🐳 Docker Compose up
run: docker compose -f compose.yml up -d
health_check_a_after_roll-back:
name: 🙏 Server A Health Check After RollBack
needs: roll-back_a
if : ${{always() && needs.roll-back_a.result != 'skipped'}}
defaults:
run:
working-directory: backend
runs-on: [ self-hosted, devel-up-prod-a ]
steps:
- name: ♻️ Send Helth Check Request
run: chmod u+x ./scripts/healthcheck.sh && ./scripts/healthcheck.sh
deploy_to_b:
name: 🚀 Server B Deployment
needs: health_check_a
if: ${{needs.health_check_a.result == 'success'}}
runs-on: [ self-hosted, devel-up-prod-b ]
defaults:
run:
working-directory: backend
env:
BACKEND_APP_IMAGE_NAME: ${{ secrets.DOCKER_REPOSITORY_NAME }}:${{ github.sha }}
steps:
- uses: actions/checkout@v4
- name: 🐳 Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: 🐳 Docker Compose up
run: docker compose -f compose.yml up -d
health_check_b:
name: 🙏 Server B Health Check
needs: deploy_to_b
defaults:
run:
working-directory: backend
runs-on: [ self-hosted, devel-up-prod-b ]
steps:
- name: ♻️ Send Helth Check Request
run: chmod u+x ./scripts/healthcheck.sh && ./scripts/healthcheck.sh
deploy_success_notify:
name: 📢Send Deploy Success Notification
runs-on: ubuntu-latest
needs:
- health_check_a
- health_check_b
if: ${{needs.health_check_a.result == 'success' && needs.health_check_b.result == 'success'}}
steps:
- name: Extract Commit Title
run: |
COMMIT_TITLE=$(echo "${{ github.event.head_commit.message }}" | head -n 1)
echo "COMMIT_TITLE=$COMMIT_TITLE" >> $GITHUB_ENV
- name: Build and Deploy Success
uses: slackapi/[email protected]
with:
channel-id: ${{ secrets.ISSUE_CHANNEL }}
payload: |
{
"text": "Build and Deploy Status",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "<!channel> \n 📣 Server Build & Deploy 결과를 안내 드립니다. 📣 \n\t • 🚀 Build Success \n\t • 🟢 Deploy Success \n\t • 🏷️ 관련 Commit: <${{ github.event.head_commit.url }}|${{ env.COMMIT_TITLE }}>"
}
}
]
}
env:
SLACK_BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
deploy_to_b_fail_notify:
name: 📢Send Deploy To Server B Fail Notification
runs-on: ubuntu-latest
needs:
- health_check_b
if: ${{failure() && needs.health_check_b.result == 'failure'}}
steps:
- name: Extract Commit Title
run: |
COMMIT_TITLE=$(echo "${{ github.event.head_commit.message }}" | head -n 1)
echo "COMMIT_TITLE=$COMMIT_TITLE" >> $GITHUB_ENV
- name: Build and Deploy Success
uses: slackapi/[email protected]
with:
channel-id: ${{ secrets.ISSUE_CHANNEL }}
payload: |
{
"text": "Build and Deploy Status",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "<!channel> \n 📣 Server Build & Deploy 결과를 안내 드립니다. 📣 \n\t • 🚀 Build Success \n\t • 🔴 Server B Deploy Fail \n\t • 🏷️ 관련 Commit: <${{ github.event.head_commit.url }}|${{ env.COMMIT_TITLE }}>"
}
}
]
}
env:
SLACK_BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
roll-back_a_success_notifiy:
name: 📢Send Server A RollBack Success Notification
runs-on: ubuntu-latest
needs:
- health_check_a_after_roll-back
if: ${{failure() && needs.health_check_a_after_roll-back.result == 'success'}}
steps:
- name: Extract Commit Title
run: |
COMMIT_TITLE=$(echo "${{ github.event.head_commit.message }}" | head -n 1)
echo "COMMIT_TITLE=$COMMIT_TITLE" >> $GITHUB_ENV
- name: Build and Deploy Success
uses: slackapi/[email protected]
with:
channel-id: ${{ secrets.ISSUE_CHANNEL }}
payload: |
{
"text": "Build and Deploy Status",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "<!channel> \n 📣 Server Build & Deploy 결과를 안내 드립니다. 📣 \n\t • 🚀 Build Success \n\t • 🟠 Server A RollBack Success \n\t • 🏷️ 관련 Commit: <${{ github.event.head_commit.url }}|${{ env.COMMIT_TITLE }}>"
}
}
]
}
env:
SLACK_BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
roll-back_a_fail_notifiy:
name: 📢Send Server A RollBack Fail Notification
runs-on: ubuntu-latest
needs:
- health_check_a_after_roll-back
if: ${{failure() && needs.health_check_a_after_roll-back.result == 'failure'}}
steps:
- name: Extract Commit Title
run: |
COMMIT_TITLE=$(echo "${{ github.event.head_commit.message }}" | head -n 1)
echo "COMMIT_TITLE=$COMMIT_TITLE" >> $GITHUB_ENV
- name: Build and Deploy Success
uses: slackapi/[email protected]
with:
channel-id: ${{ secrets.ISSUE_CHANNEL }}
payload: |
{
"text": "Build and Deploy Status",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "<!channel> \n 📣 Server Build & Deploy 결과를 안내 드립니다. 📣 \n\t • 🚀 Build Success \n\t • 🔴Server A RollBack Fail \n\t • 🏷️ 관련 Commit: <${{ github.event.head_commit.url }}|${{ env.COMMIT_TITLE }}>"
}
}
]
}
env:
SLACK_BOT_TOKEN: ${{ secrets.BOT_TOKEN }}