-
Notifications
You must be signed in to change notification settings - Fork 0
335 lines (289 loc) · 14.9 KB
/
depoly-to-vm.yml
File metadata and controls
335 lines (289 loc) · 14.9 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
name: Deploy to Oracle Cloud VM (Sequential)
on:
workflow_run:
workflows: ["Docker Build and Push to GHCR"]
types:
- completed
jobs:
deploy-sub:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
outputs:
status: ${{ steps.deployment.outcome }}
image_url: ${{ steps.setup.outputs.image_url }}
steps:
- name: Setup Variables
id: setup
run: |
REPO_LOWER=$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]')
IMAGE_URL="ghcr.io/${REPO_LOWER}:latest"
echo "image_url=$IMAGE_URL" >> $GITHUB_OUTPUT
echo "Using image: $IMAGE_URL"
- name: Deploy and Health Check SUB VM
id: deployment
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.SUB_VM_HOST }}
username: ${{ secrets.SUB_VM_USERNAME }}
key: ${{ secrets.SUB_VM_SSH_KEY }}
port: 22
script: |
set -e
# 변수 설정
IMAGE_URL="${{ steps.setup.outputs.image_url }}"
CONTAINER_NAME="pray-together-api"
echo "🚀 [SUB VM] Starting deployment with image: $IMAGE_URL"
# GitHub Container Registry 로그인
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u "${{ github.actor }}" --password-stdin
# 배포 함수
deploy_container() {
echo "🧹 [SUB VM] Cleaning up..."
docker stop $CONTAINER_NAME 2>/dev/null || true
docker rm $CONTAINER_NAME 2>/dev/null || true
docker rmi $IMAGE_URL 2>/dev/null || true
echo "📦 [SUB VM] Deploying new container..."
docker pull $IMAGE_URL
docker run -d \
--name $CONTAINER_NAME \
--restart unless-stopped \
--memory=700m \
-p 8080:8080 \
-v /home/ubuntu/log-pray:/root/log-pray \
-v /home/ubuntu/heapdump:/app/logs \
--log-driver json-file \
--log-opt max-size=50m \
--log-opt max-file=10 \
-e ORACLE_DB_DRIVER="${{ secrets.ORACLE_DB_DRIVER }}" \
-e ORACLE_DB_URL="${{ secrets.ORACLE_DB_URL }}" \
-e ORACLE_DB_USERNAME="${{ secrets.ORACLE_DB_USERNAME }}" \
-e ORACLE_DB_PASSWORD="${{ secrets.ORACLE_DB_PASSWORD }}" \
-e ORACLE_DB_DIALECT="${{ secrets.ORACLE_DB_DIALECT }}" \
-e MAIL_USERNAME="${{ secrets.MAIL_USERNAME }}" \
-e MAIL_PASSWORD="${{ secrets.MAIL_PASSWORD }}" \
-e JWT_SECRET_KEY="${{ secrets.JWT_SECRET_KEY }}" \
-e ACCESS_EXPIRE_TIME="${{ secrets.ACCESS_EXPIRE_TIME }}" \
-e REFRESH_EXPIRE_TIME="${{ secrets.REFRESH_EXPIRE_TIME }}" \
-e APP_VERSION_MINIMUM="${{ secrets.APP_VERSION_MINIMUM }}" \
-e APP_VERSION_UPDATE_FORCE="${{ secrets.APP_VERSION_UPDATE_FORCE }}" \
-e APP_MAINTENANCE_MODE="${{ secrets.APP_MAINTENANCE_MODE }}" \
-e GOOGLE_WEB_CLIENT_ID="${{ secrets.GOOGLE_WEB_CLIENT_ID }}" \
-e APPLE_CLIENT_ID="${{ secrets.APPLE_CLIENT_ID }}" \
$IMAGE_URL
echo "⏳ [SUB VM] Waiting for container to start..."
sleep 30
}
# 헬스체크 함수
health_check() {
echo "🔍 [SUB VM] Starting health check..."
local max_attempts=6
local wait_time=20
for attempt in $(seq 1 $max_attempts); do
echo "⏳ [SUB VM] Health check attempt $attempt/$max_attempts..."
# 컨테이너 상태 확인
if ! docker ps --filter "name=$CONTAINER_NAME" --format "{{.Names}}" | grep -q "^${CONTAINER_NAME}$"; then
echo "❌ [SUB VM] Container '$CONTAINER_NAME' is not running"
echo "📋 [SUB VM] Container logs:"
docker logs --tail=20 $CONTAINER_NAME 2>/dev/null || echo "No logs available"
return 1
fi
# HTTP 헬스체크
local response
local http_code
local body
if response=$(curl -s -w "%{http_code}" --connect-timeout 20 --max-time 20 "http://localhost:8080/health" 2>/dev/null); then
http_code="${response: -3}"
body="${response%???}"
echo "📡 [SUB VM] HTTP Response: $http_code"
echo "📄 [SUB VM] Body: $body"
if [[ "$http_code" == "200" ]] && echo "$body" | grep -q "healthy"; then
echo "✅ [SUB VM] Health check passed!"
return 0
fi
else
echo "🔌 [SUB VM] Health check request failed"
fi
if [[ $attempt -lt $max_attempts ]]; then
echo "⏱️ [SUB VM] Waiting ${wait_time}s before retry..."
sleep $wait_time
fi
done
echo "❌ [SUB VM] Health check failed after $max_attempts attempts"
echo "📋 [SUB VM] Final container logs:"
docker logs --tail=30 $CONTAINER_NAME 2>/dev/null || echo "No logs available"
return 1
}
# 실행
deploy_container
health_check
echo "🎉 [SUB VM] Deployment completed successfully!"
deploy-main:
runs-on: ubuntu-latest
needs: deploy-sub
if: ${{ needs.deploy-sub.outputs.status == 'success' }}
outputs:
status: ${{ steps.deployment.outcome }}
steps:
- name: Deploy and Health Check MAIN VM
id: deployment
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.MAIN_VM_HOST }}
username: ${{ secrets.MAIN_VM_USERNAME }}
key: ${{ secrets.MAIN_VM_SSH_KEY }}
port: 22
script: |
set -e
# 변수 설정
IMAGE_URL="${{ needs.deploy-sub.outputs.image_url }}"
CONTAINER_NAME="pray-together-api"
echo "🚀 [MAIN VM] Starting deployment with image: $IMAGE_URL"
# GitHub Container Registry 로그인
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u "${{ github.actor }}" --password-stdin
# 배포 함수 (SUB와 동일한 로직)
deploy_container() {
echo "🧹 [MAIN VM] Cleaning up..."
docker stop $CONTAINER_NAME 2>/dev/null || true
docker rm $CONTAINER_NAME 2>/dev/null || true
docker rmi $IMAGE_URL 2>/dev/null || true
echo "📦 [MAIN VM] Deploying new container..."
docker pull $IMAGE_URL
docker run -d \
--name $CONTAINER_NAME \
--restart unless-stopped \
--memory=700m \
-p 8080:8080 \
-v /home/ubuntu/log-pray:/root/log-pray \
-v /home/ubuntu/heapdump:/app/logs \
--log-driver json-file \
--log-opt max-size=50m \
--log-opt max-file=10 \
-e ORACLE_DB_DRIVER="${{ secrets.ORACLE_DB_DRIVER }}" \
-e ORACLE_DB_URL="${{ secrets.ORACLE_DB_URL }}" \
-e ORACLE_DB_USERNAME="${{ secrets.ORACLE_DB_USERNAME }}" \
-e ORACLE_DB_PASSWORD="${{ secrets.ORACLE_DB_PASSWORD }}" \
-e ORACLE_DB_DIALECT="${{ secrets.ORACLE_DB_DIALECT }}" \
-e MAIL_USERNAME="${{ secrets.MAIL_USERNAME }}" \
-e MAIL_PASSWORD="${{ secrets.MAIL_PASSWORD }}" \
-e JWT_SECRET_KEY="${{ secrets.JWT_SECRET_KEY }}" \
-e ACCESS_EXPIRE_TIME="${{ secrets.ACCESS_EXPIRE_TIME }}" \
-e REFRESH_EXPIRE_TIME="${{ secrets.REFRESH_EXPIRE_TIME }}" \
-e APP_VERSION_MINIMUM="${{ secrets.APP_VERSION_MINIMUM }}" \
-e APP_VERSION_UPDATE_FORCE="${{ secrets.APP_VERSION_UPDATE_FORCE }}" \
-e APP_MAINTENANCE_MODE="${{ secrets.APP_MAINTENANCE_MODE }}" \
-e GOOGLE_WEB_CLIENT_ID="${{ secrets.GOOGLE_WEB_CLIENT_ID }}" \
-e APPLE_CLIENT_ID="${{ secrets.APPLE_CLIENT_ID }}" \
$IMAGE_URL
echo "⏳ [MAIN VM] Waiting for container to start..."
sleep 30
}
# 헬스체크 함수
health_check() {
echo "🔍 [MAIN VM] Starting health check..."
local max_attempts=6
local wait_time=20
for attempt in $(seq 1 $max_attempts); do
echo "⏳ [MAIN VM] Health check attempt $attempt/$max_attempts..."
if ! docker ps --filter "name=$CONTAINER_NAME" --format "{{.Names}}" | grep -q "^${CONTAINER_NAME}$"; then
echo "❌ [MAIN VM] Container '$CONTAINER_NAME' is not running"
echo "📋 [MAIN VM] Container logs:"
docker logs --tail=20 $CONTAINER_NAME 2>/dev/null || echo "No logs available"
return 1
fi
local response
local http_code
local body
if response=$(curl -s -w "%{http_code}" --connect-timeout 20 --max-time 20 "http://localhost:8080/health" 2>/dev/null); then
http_code="${response: -3}"
body="${response%???}"
echo "📡 [MAIN VM] HTTP Response: $http_code"
echo "📄 [MAIN VM] Body: $body"
if [[ "$http_code" == "200" ]] && echo "$body" | grep -q "healthy"; then
echo "✅ [MAIN VM] Health check passed!"
return 0
fi
else
echo "🔌 [MAIN VM] Health check request failed"
fi
if [[ $attempt -lt $max_attempts ]]; then
echo "⏱️ [MAIN VM] Waiting ${wait_time}s before retry..."
sleep $wait_time
fi
done
echo "❌ [MAIN VM] Health check failed after $max_attempts attempts"
echo "📋 [MAIN VM] Final container logs:"
docker logs --tail=30 $CONTAINER_NAME 2>/dev/null || echo "No logs available"
return 1
}
# 실행
deploy_container
health_check
echo "🎉 [MAIN VM] Deployment completed successfully!"
# 알림 Job - 안전한 조건 처리
notify:
runs-on: ubuntu-latest
needs: [deploy-sub, deploy-main]
if: always()
steps:
- name: Determine Status
id: status
run: |
SUB_STATUS="${{ needs.deploy-sub.outputs.status }}"
MAIN_STATUS="${{ needs.deploy-main.outputs.status }}"
echo "SUB_STATUS: $SUB_STATUS"
echo "MAIN_STATUS: $MAIN_STATUS"
# 안전한 조건 처리
if [[ "$SUB_STATUS" == "success" && "$MAIN_STATUS" == "success" ]]; then
echo "notification_status=success" >> $GITHUB_OUTPUT
echo "notification_color=0x00ff00" >> $GITHUB_OUTPUT
echo "notification_title=🎉 All Deployments Successful" >> $GITHUB_OUTPUT
echo "sub_icon=🟢 ✅" >> $GITHUB_OUTPUT
echo "main_icon=🟢 ✅" >> $GITHUB_OUTPUT
echo "sub_text=SUCCESS" >> $GITHUB_OUTPUT
echo "main_text=SUCCESS" >> $GITHUB_OUTPUT
echo "footer_message=🎊 Zero-downtime deployment completed successfully!" >> $GITHUB_OUTPUT
elif [[ "$SUB_STATUS" == "success" && "$MAIN_STATUS" == "failure" ]]; then
echo "notification_status=failure" >> $GITHUB_OUTPUT
echo "notification_color=0xff8800" >> $GITHUB_OUTPUT
echo "notification_title=⚠️ Partial Deployment (MAIN Failed)" >> $GITHUB_OUTPUT
echo "sub_icon=🟢 ✅" >> $GITHUB_OUTPUT
echo "main_icon=🔴 ❌" >> $GITHUB_OUTPUT
echo "sub_text=SUCCESS" >> $GITHUB_OUTPUT
echo "main_text=FAILED" >> $GITHUB_OUTPUT
echo "footer_message=⚠️ Service running on SUB VM only. Manual intervention needed for MAIN VM." >> $GITHUB_OUTPUT
elif [[ "$SUB_STATUS" == "success" && "$MAIN_STATUS" == "" ]]; then
# MAIN이 실행되지 않은 경우 (skipped)
echo "notification_status=success" >> $GITHUB_OUTPUT
echo "notification_color=0x0099ff" >> $GITHUB_OUTPUT
echo "notification_title=🟡 SUB VM Deployment Only" >> $GITHUB_OUTPUT
echo "sub_icon=🟢 ✅" >> $GITHUB_OUTPUT
echo "main_icon=⏸️ ⏭️" >> $GITHUB_OUTPUT
echo "sub_text=SUCCESS" >> $GITHUB_OUTPUT
echo "main_text=SKIPPED" >> $GITHUB_OUTPUT
echo "footer_message=ℹ️ Only SUB VM was deployed. MAIN VM deployment was skipped." >> $GITHUB_OUTPUT
else
echo "notification_status=failure" >> $GITHUB_OUTPUT
echo "notification_color=0xff0000" >> $GITHUB_OUTPUT
echo "notification_title=🚨 Deployment Failed" >> $GITHUB_OUTPUT
echo "sub_icon=🔴 ❌" >> $GITHUB_OUTPUT
echo "main_icon=⏸️ ⏭️" >> $GITHUB_OUTPUT
echo "sub_text=FAILED" >> $GITHUB_OUTPUT
echo "main_text=SKIPPED" >> $GITHUB_OUTPUT
echo "footer_message=🚨 Critical: SUB VM deployment failed. Service may be unavailable." >> $GITHUB_OUTPUT
fi
- name: Send Discord Notification
uses: sarisia/actions-status-discord@v1
with:
webhook: ${{ secrets.DISCORD_WEBHOOK_URL }}
status: ${{ steps.status.outputs.notification_status }}
color: ${{ steps.status.outputs.notification_color }}
title: "[${{ github.repository }}] ${{ steps.status.outputs.notification_title }}"
description: |
**Deployment Results:**
${{ steps.status.outputs.sub_icon }} **SUB VM**: ${{ steps.status.outputs.sub_text }}
${{ steps.status.outputs.main_icon }} **MAIN VM**: ${{ steps.status.outputs.main_text }}
**Image**: `${{ needs.deploy-sub.outputs.image_url }}`
**Trigger**: ${{ github.event.workflow_run.head_commit.message }}
**Commit**: [`${{ github.event.workflow_run.head_sha }}`](${{ github.event.workflow_run.html_url }})
${{ steps.status.outputs.footer_message }}
url: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
username: "기도함께 도우미"