Refactor/kotlin #59
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |