Skip to content

fix: current_state 읽어 오는 과정 수정 #82

fix: current_state 읽어 오는 과정 수정

fix: current_state 읽어 오는 과정 수정 #82

Workflow file for this run

name: "backend-docker-build"
on:
push:
branches: [ "dev-be", "feature-be-#300" ]
jobs:
build:
name: Build and Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# 노드 버전 설정 및 의존성 설치
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18.17.1'
- name: Corepack Enable
run: corepack enable
- name: Install Dependencies
run: yarn install
# 테스트 실행 (필요한 경우)
# - name: Run Tests
# run: yarn test
docker:
name: Deploy Docker Image
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v3
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Build and Push
uses: docker/build-push-action@v3
with:
context: .
file: ./packages/backend/Dockerfile
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/git-challenge-backend:0.1
deploy:
name: Deploy Backend Blue-Green
runs-on: ubuntu-latest
needs: docker
steps:
- name: Get Current Deployment Status via SSH
id: get-state
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.NGINX_SSH_HOST }}
username: ${{ secrets.NGINX_SSH_USERNAME }}
password: ${{ secrets.NGINX_SSH_PASSWORD }}
port: ${{ secrets.NGINX_SSH_PORT }}
script: |
scp ${{ secrets.NGINX_SSH_USERNAME }}@${{ secrets.NGINX_SSH_HOST }}:/status ./status
- name: Read Status File
id: read-status
run: |
CURRENT_STATE=$(cat ./status)
echo "CURRENT_STATE=$CURRENT_STATE" >> $GITHUB_ENV
- name: SSH and Deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.BACKEND_SSH_HOST }}
username: ${{ secrets.BACKEND_SSH_USERNAME }}
password: ${{ secrets.BACKEND_SSH_PASSWORD }}
port: ${{ secrets.BACKEND_SSH_PORT }}
script: |
echo "The current state is ${{ env.CURRENT_STATE }}"
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/git-challenge-backend:0.1
if [ ${{ env.CURRENT_STATE }} = "blue" ]; then
docker run -d --name backend-green -p 8081:8080 \
-v /${{ secrets.BACKEND_SSH_USERNAME }}/backend-logs:/app/packages/backend/logs/ \
-e CONTAINER_GIT_USERNAME=${{ secrets.CONTAINER_GIT_USERNAME }} \
-e MONGODB_HOST=${{ secrets.MONGODB_HOST }} \
-e SECRET_KEY=${{ secrets.SECRET_KEY }} \
-e X_NCP_CLOVASTUDIO_API_KEY=${{ secrets.X_NCP_CLOVASTUDIO_API_KEY }} \
-e X_NCP_APIGW_API_KEY=${{ secrets.X_NCP_APIGW_API_KEY }} \
-e X_NCP_CLOVASTUDIO_REQUEST_ID=${{ secrets.X_NCP_CLOVASTUDIO_REQUEST_ID }} \
-e CONTAINER_SERVER_HOST=${{ secrets.CONTAINER_SERVER_HOST }} \
-e CONTAINER_POOL_MAX=${{ secrets.CONTAINER_POOL_MAX }} \
${{ secrets.DOCKERHUB_USERNAME }}/git-challenge-backend:0.1
docker rm -f backend-blue || true
echo "green" > /status
else
docker run -d --name backend-blue -p 8080:8080 \
-v /${{ secrets.BACKEND_SSH_USERNAME }}/backend-logs:/app/packages/backend/logs/ \
-e CONTAINER_GIT_USERNAME=${{ secrets.CONTAINER_GIT_USERNAME }} \
-e MONGODB_HOST=${{ secrets.MONGODB_HOST }} \
-e SECRET_KEY=${{ secrets.SECRET_KEY }} \
-e X_NCP_CLOVASTUDIO_API_KEY=${{ secrets.X_NCP_CLOVASTUDIO_API_KEY }} \
-e X_NCP_APIGW_API_KEY=${{ secrets.X_NCP_APIGW_API_KEY }} \
-e X_NCP_CLOVASTUDIO_REQUEST_ID=${{ secrets.X_NCP_CLOVASTUDIO_REQUEST_ID }} \
-e CONTAINER_SERVER_HOST=${{ secrets.CONTAINER_SERVER_HOST }} \
-e CONTAINER_POOL_MAX=${{ secrets.CONTAINER_POOL_MAX }} \
${{ secrets.DOCKERHUB_USERNAME }}/git-challenge-backend:0.1
docker rm -f backend-green || true
echo "blue" > /status
fi
- name: Update NGINX Configuration
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.NGINX_SSH_HOST }}
username: ${{ secrets.NGINX_SSH_USERNAME }}
password: ${{ secrets.NGINX_SSH_PASSWORD }}
port: ${{ secrets.NGINX_SSH_PORT }}
script: |
if [ $(cat /status) = "green" ]; then
sed -i 's/proxy_pass http:\/\/127.0.0.1:808[0-1];/proxy_pass http:\/\/127.0.0.1:8081;/' /etc/nginx/sites-available/default
else
sed -i 's/proxy_pass http:\/\/127.0.0.1:808[0-1];/proxy_pass http:\/\/127.0.0.1:8080;/' /etc/nginx/sites-available/default
fi
sudo nginx -s reload