-
Notifications
You must be signed in to change notification settings - Fork 1
188 lines (160 loc) · 7.88 KB
/
Copy pathdepoly-to-vm.yml
File metadata and controls
188 lines (160 loc) · 7.88 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
name: Deploy to Oracle Cloud VM
on:
workflow_run:
workflows: ["Docker Build and Push to GHCR"]
types:
- completed
jobs:
deploy-main:
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 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="${{ steps.setup.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
# 배포 함수
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 \
--network app-net \
--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-main
if: always()
steps:
- name: Determine Status
id: status
run: |
MAIN_STATUS="${{ needs.deploy-main.outputs.status }}"
echo "MAIN_STATUS: $MAIN_STATUS"
# 안전한 조건 처리
if [[ "$MAIN_STATUS" == "success" ]]; then
echo "notification_status=success" >> $GITHUB_OUTPUT
echo "notification_color=0x00ff00" >> $GITHUB_OUTPUT
echo "notification_title=🎉 Deployment Successful" >> $GITHUB_OUTPUT
echo "main_icon=🟢 ✅" >> $GITHUB_OUTPUT
echo "main_text=SUCCESS" >> $GITHUB_OUTPUT
echo "footer_message=🎊 MAIN VM deployment completed successfully!" >> $GITHUB_OUTPUT
else
echo "notification_status=failure" >> $GITHUB_OUTPUT
echo "notification_color=0xff0000" >> $GITHUB_OUTPUT
echo "notification_title=🚨 Deployment Failed" >> $GITHUB_OUTPUT
echo "main_icon=🔴 ❌" >> $GITHUB_OUTPUT
echo "main_text=FAILED" >> $GITHUB_OUTPUT
echo "footer_message=🚨 MAIN VM deployment failed. Manual intervention needed." >> $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.main_icon }} **MAIN VM**: ${{ steps.status.outputs.main_text }}
**Image**: `${{ needs.deploy-main.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: "기도함께 도우미"