-
Notifications
You must be signed in to change notification settings - Fork 0
187 lines (159 loc) · 6.54 KB
/
deploy.yml
File metadata and controls
187 lines (159 loc) · 6.54 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
name: Deploy to GCP Compute Engine
on:
# push:
# branches: [ develop ]
# paths-ignore:
# - 'api/src/docs/asciidoc/**'
# - '.github/workflows/docs.yml'
workflow_dispatch:
concurrency:
group: build-and-deploy
cancel-in-progress: true
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Create application.yml in api module
run: |
mkdir -p api/src/main/resources
echo "${{ secrets.APPLICATION_DEV }}" > api/src/main/resources/application.yml
echo "✅ application.yml created successfully"
ls -la api/src/main/resources/
- name: Create .env
run: |
echo "${{ secrets.ENV_FILE }}" > .env
echo "✅ .env file created successfully"
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/sent-app:${{ github.run_number }}
${{ secrets.DOCKERHUB_USERNAME }}/sent-app:latest
- name: Verify Docker image push
run: |
echo "✅ Docker image pushed successfully"
echo "🐳 Image tag: ${{ secrets.DOCKERHUB_USERNAME }}/sent-app:latest"
- name: Prepare server directory
uses: appleboy/ssh-action@v0.1.7
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SERVER_SSH_KEY }}
script: |
mkdir -p /home/sentthoughts/Sent-Server
echo "📁 Server directory prepared"
- name: Copy all files to server
uses: appleboy/scp-action@v0.1.7
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SERVER_SSH_KEY }}
source: .
target: /home/sentthoughts/Sent-Server/
- name: Setup SSL certificates
uses: appleboy/ssh-action@v0.1.7
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SERVER_SSH_KEY }}
script: |
cd /home/sentthoughts/Sent-Server
if [ ! -d "certs" ]; then
ln -s /home/ubuntu/certs/devcerts ./certs
fi
if [ ! -f "certs/fullchain.pem" ] || [ ! -f "certs/privkey.pem" ]; then
echo "❌ SSL certificates not found!"
exit 1
fi
- name: Check Docker status before deployment
uses: appleboy/ssh-action@v0.1.7
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SERVER_SSH_KEY }}
script: |
echo "🐳 Current Docker containers:"
docker ps -a
echo ""
echo "🌐 Current Docker networks:"
docker network ls
echo ""
echo "💾 Docker system info:"
docker system df
- name: Deploy with Docker Compose
uses: appleboy/ssh-action@v0.1.7
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SERVER_SSH_KEY }}
script: |
cd /home/sentthoughts/Sent-Server
echo "📍 Current directory: $(pwd)"
echo "📥 Pulling latest Docker images..."
docker-compose pull
echo "✅ Docker images pulled successfully"
echo "🛑 Stopping existing services..."
docker-compose down
echo "✅ Services stopped"
echo "🚀 Starting services with docker-compose..."
docker-compose up -d
echo "✅ Services started successfully"
echo "📋 Container status:"
docker-compose ps
echo "📝 Service logs (last 20 lines):"
docker-compose logs --tail 20
- name: Health check
uses: appleboy/ssh-action@v0.1.7
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SERVER_SSH_KEY }}
script: |
cd /home/sentthoughts/Sent-Server
echo "🏥 Starting health check..."
# Wait for services to start
echo "⏰ Waiting for backend (sent-server) to initialize..."
MAX_ATTEMPTS=15
SLEEP_SEC=8
for i in $(seq 1 $MAX_ATTEMPTS)
do
BACKEND_STATUS=$(curl -A "health-probe" -s -o /dev/null -w "%{http_code}" -k https://localhost/health || echo "000")
echo "⏳ Health check attempt $i/$MAX_ATTEMPTS: backend $BACKEND_STATUS"
if [ "$BACKEND_STATUS" = "200" ]; then
echo "✅ Backend health check passed on attempt $i!"
echo "📋 Final service status:"
docker-compose ps
echo "📝 Backend health response:"
curl -s -k https://localhost/health || echo "No response"
# nginx 상태/로그는 백엔드가 정상일 때만 한 번 확인
echo "📝 Nginx logs (last 10 lines):"
docker-compose logs --tail 10 nginx-proxy 2>/dev/null || echo "⚠️ Unable to get nginx logs"
echo "📝 Nginx status:"
docker-compose ps nginx-proxy 2>/dev/null || echo "⚠️ Unable to get nginx status"
exit 0
fi
# 3회마다 최근 로그 출력
if (( $i % 3 == 0 )); then
echo "📝 [Attempt $i] Recent backend logs:"
docker-compose logs --tail 10 sent-server 2>/dev/null || echo "⚠️ Unable to get backend logs"
fi
sleep $SLEEP_SEC
done
echo "❌ Health check failed after $MAX_ATTEMPTS attempts!"
echo "🔍 Final backend logs:"
docker-compose logs --tail 30 sent-server 2>/dev/null || echo "⚠️ Unable to get backend logs"
echo "🔍 Nginx logs:"
docker-compose logs --tail 30 nginx-proxy 2>/dev/null || echo "⚠️ Unable to get nginx logs"
echo "🔍 Service status:"
docker-compose ps 2>/dev/null || echo "⚠️ Unable to get docker-compose status"
exit 1