-
Notifications
You must be signed in to change notification settings - Fork 0
131 lines (117 loc) ยท 5.25 KB
/
backend-deploy.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
name: "backend-docker-build"
on:
push:
branches: [ "dev-be" ]
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: 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: |
CURRENT_STATE=$(curl -s http://${{ secrets.BLUE_GREEN_HOST }}:${{ secrets.BLUE_GREEN_PORT }}/status)
echo "The current state is $CURRENT_STATE"
docker image prune -f
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/git-challenge-backend:0.1
if [ $CURRENT_STATE = "blue" ]; then
docker rm -f backend-green || true
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
until [[ "$(curl -s http://localhost:8081/api/v1/health)" == "OK" ]]; do
echo 'Waiting for the container to be ready...'
sleep 5
done
docker rm -f backend-blue || true
curl -X POST -H "Content-Type: application/json" \
-d '{"status":"green"}' \
http://${{ secrets.BLUE_GREEN_HOST }}:${{ secrets.BLUE_GREEN_PORT }}/status
else
docker rm -f backend-blue || true
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
until [[ "$(curl -s http://localhost:8080/api/v1/health)" == "OK" ]]; do
echo 'Waiting for the container to be ready...'
sleep 5
done
docker rm -f backend-green || true
curl -X POST -H "Content-Type: application/json" \
-d '{"status":"blue"}' \
http://${{ secrets.BLUE_GREEN_HOST }}:${{ secrets.BLUE_GREEN_PORT }}/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: |
CURRENT_STATE=$(curl -s http://${{ secrets.BLUE_GREEN_HOST }}:${{ secrets.BLUE_GREEN_PORT }}/status)
if [ "$CURRENT_STATE" = "green" ]; then
sed -i 's/8080/8081/' /etc/nginx/sites-available/default
else
sed -i 's/8081/8080/' /etc/nginx/sites-available/default
fi
sudo nginx -s reload