Skip to content

Refactor/kotlin

Refactor/kotlin #59

Workflow file for this run

name: CI/CD
on:
pull_request:
types: [closed]
branches:
- main
- dev
permissions:
pull-requests: read
checks: write
actions: write
jobs:
PR_Merge_label:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
outputs:
build_nestjs: ${{ steps.PR-label-nestjs.outputs.passed }}
build_kotlin: ${{ steps.PR-label-kotlin.outputs.passed }}
steps:
- name: ๋ ˆํฌ ์ฒดํฌ์•„์›ƒ
uses: actions/checkout@main
- name: PR ๋ผ๋ฒจ ๋„ค์ŠคํŠธ
id: PR-label-nestjs
uses: danielchabr/pr-labels-checker@master
with:
hasSome: nestjs
githubToken: ${{ secrets.GITHUB_TOKEN }}
allowFailure: true
- name: PR ๋ผ๋ฒจ ์ฝ”ํ‹€๋ฆฐ
id: PR-label-kotlin
uses: danielchabr/pr-labels-checker@master
with:
hasSome: kotlin
githubToken: ${{ secrets.GITHUB_TOKEN }}
allowFailure: true
nestjs-build:
runs-on: ubuntu-latest
needs: PR_Merge_label
if: needs.PR_Merge_label.outputs.build_nestjs == 'true'
steps:
- name: ๋ ˆํฌ ์ฒดํฌ์•„์›ƒ
uses: actions/checkout@main
- name: Docker Buildx ์„ค์ •
uses: docker/setup-buildx-action@master
- name: Docker Hub ๋กœ๊ทธ์ธ
uses: docker/login-action@master
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
- name: ๋นŒ๋“œ ๋ฐ ํ‘ธ์‹œ
uses: docker/build-push-action@master
with:
context: .
file: ./Dockerfile.nestjs
push: true
tags: ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO_BE }}:nestjs
cache-from: type=gha
cache-to: type=gha,mode=max
nestjs-deploy:
runs-on: ubuntu-latest
needs: nestjs-build
steps:
- name: ๋ ˆํฌ ์ฒดํฌ์•„์›ƒ
uses: actions/checkout@main
- name: .env ์ƒ์„ฑ
env:
SECRETS_JSON: ${{ toJson(secrets) }}
run: |
echo "$SECRETS_JSON" | jq -r 'to_entries | map(select(.key | startswith("ENV"))) | map("\(.key)=\(.value|tostring)") | .[]' > .env
- name: .env ์ „์†ก
uses: appleboy/scp-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
password: ${{ secrets.SSH_PASSWORD }}
port: ${{ secrets.SSH_PORT }}
source: .env
target: ${{ secrets.SERVER_PATH }}/site/${{ secrets.DOCKER_HUB_USERNAME }}
overwrite: true
- name: ์„œ๋ฒ„ ์ ‘์†
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
password: ${{ secrets.SSH_PASSWORD }}
port: ${{ secrets.SSH_PORT }}
script: |
cd ${{ secrets.SERVER_PATH }}/site/${{ secrets.DOCKER_HUB_USERNAME }}
# Docker Hub ๋กœ๊ทธ์ธ
echo ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} | docker login -u ${{ secrets.DOCKER_HUB_USERNAME }} --password-stdin
# ์ด๋ฏธ์ง€ ํ’€๋ง
docker pull ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO_BE }}:nestjs
# ์ƒˆ๋กœ์šด ์ด๋ฏธ์ง€๋ฅผ Green/Blue ์ค‘ ํ•˜๋‚˜๋กœ ํƒœ๊ทธ
if docker ps | grep eatsfinder-nestjs-blue; then
NEW_DEPLOYMENT="green"
OLD_DEPLOYMENT="blue"
else
NEW_DEPLOYMENT="blue"
OLD_DEPLOYMENT="green"
fi
docker-compose up -d eatsfinder-nestjs-$NEW_DEPLOYMENT
# ์ƒˆ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹œ์ž‘๋  ์‹œ๊ฐ„์„ ๊ธฐ๋‹ค๋ฆผ
sleep 10
# Nginx ์„ค์ • ๋ณ€๊ฒฝ
if [ "$NEW_DEPLOYMENT" == "green" ]; then
sed -i "s/proxy_pass http:\/\/eatsfinder-nestjs-$OLD_DEPLOYMENT:${{ secrets.ENV_NESTJS_PORT }};/proxy_pass http:\/\/eatsfinder-nestjs-$NEW_DEPLOYMENT:${{ secrets.ENV_NESTJS_PORT }};/" ${{ secrets.SERVER_PATH }}/nginx/conf.d/eatsfinder.conf
else
sed -i "s/proxy_pass http:\/\/eatsfinder-nestjs-$OLD_DEPLOYMENT:${{ secrets.ENV_NESTJS_PORT }};/proxy_pass http:\/\/eatsfinder-nestjs-$NEW_DEPLOYMENT:${{ secrets.ENV_NESTJS_PORT }};/" ${{ secrets.SERVER_PATH }}/nginx/conf.d/eatsfinder.conf
fi
# Nginx ์žฌ์‹œ์ž‘
docker-compose -f ${{ secrets.SERVER_PATH }}/docker-compose-multi.yml up -d --force-recreate --no-deps nginx
# ์ด์ „ ๋ฒ„์ „์˜ ์ปจํ…Œ์ด๋„ˆ ์ค‘์ง€ ๋ฐ ์ œ๊ฑฐ
docker-compose stop eatsfinder-nestjs-$OLD_DEPLOYMENT
docker-compose rm -f eatsfinder-nestjs-$OLD_DEPLOYMENT
kotlin-build:
runs-on: ubuntu-latest
needs: PR_Merge_label
if: needs.PR_Merge_label.outputs.build_kotlin == 'true'
steps:
- name: ๋ ˆํฌ ์ฒดํฌ์•„์›ƒ
uses: actions/checkout@main
- name: Docker Buildx ์„ค์ •
uses: docker/setup-buildx-action@master
- name: Docker Hub ๋กœ๊ทธ์ธ
uses: docker/login-action@master
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
- name: ๋นŒ๋“œ ๋ฐ ํ‘ธ์‹œ
uses: docker/build-push-action@master
with:
context: .
file: ./Dockerfile.kotlin
push: true
tags: ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO_BE }}:kotlin
cache-from: type=gha
cache-to: type=gha,mode=max
kotlin-deploy:
runs-on: ubuntu-latest
needs: kotlin-build
steps:
- name: ๋ ˆํฌ ์ฒดํฌ์•„์›ƒ
uses: actions/checkout@main
- name: .env ์ƒ์„ฑ
env:
SECRETS_JSON: ${{ toJson(secrets) }}
run: |
echo "$SECRETS_JSON" | jq -r 'to_entries | map(select(.key | startswith("ENV"))) | map("\(.key)=\(.value|tostring)") | .[]' > .env
- name: .env ์ „์†ก
uses: appleboy/scp-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
password: ${{ secrets.SSH_PASSWORD }}
port: ${{ secrets.SSH_PORT }}
source: .env
target: ${{ secrets.SERVER_PATH }}/site/${{ secrets.DOCKER_HUB_USERNAME }}
overwrite: true
- name: ์„œ๋ฒ„ ์ ‘์†
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
password: ${{ secrets.SSH_PASSWORD }}
port: ${{ secrets.SSH_PORT }}
script: |
cd ${{ secrets.SERVER_PATH }}/site/${{ secrets.DOCKER_HUB_USERNAME }}
# Docker Hub ๋กœ๊ทธ์ธ
echo ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} | docker login -u ${{ secrets.DOCKER_HUB_USERNAME }} --password-stdin
# ์ด๋ฏธ์ง€ ํ’€๋ง
docker pull ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO_BE }}:kotlin
# ์ƒˆ๋กœ์šด ์ด๋ฏธ์ง€๋ฅผ Green/Blue ์ค‘ ํ•˜๋‚˜๋กœ ํƒœ๊ทธ
if docker ps | grep eatsfinder-kotlin-blue; then
NEW_DEPLOYMENT="green"
OLD_DEPLOYMENT="blue"
else
NEW_DEPLOYMENT="blue"
OLD_DEPLOYMENT="green"
fi
docker-compose up -d eatsfinder-kotlin-$NEW_DEPLOYMENT
# ์ƒˆ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹œ์ž‘๋  ์‹œ๊ฐ„์„ ๊ธฐ๋‹ค๋ฆผ
sleep 10
# Nginx ์„ค์ • ๋ณ€๊ฒฝ
if [ "$NEW_DEPLOYMENT" == "green" ]; then
sed -i "s/proxy_pass http:\/\/eatsfinder-kotlin-$OLD_DEPLOYMENT:8080;/proxy_pass http:\/\/eatsfinder-kotlin-$NEW_DEPLOYMENT:8080;/" ${{ secrets.SERVER_PATH }}/nginx/conf.d/eatsfinder.conf
else
sed -i "s/proxy_pass http:\/\/eatsfinder-kotlin-$OLD_DEPLOYMENT:8080;/proxy_pass http:\/\/eatsfinder-kotlin-$NEW_DEPLOYMENT:8080;/" ${{ secrets.SERVER_PATH }}/nginx/conf.d/eatsfinder.conf
fi
# Nginx ์žฌ์‹œ์ž‘
docker-compose -f ${{ secrets.SERVER_PATH }}/docker-compose-multi.yml up -d --force-recreate --no-deps nginx
# ์ด์ „ ๋ฒ„์ „์˜ ์ปจํ…Œ์ด๋„ˆ ์ค‘์ง€ ๋ฐ ์ œ๊ฑฐ
docker-compose stop eatsfinder-kotlin-$OLD_DEPLOYMENT
docker-compose rm -f eatsfinder-kotlin-$OLD_DEPLOYMENT