Skip to content

Feat/follow

Feat/follow #44

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