-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
253 lines (218 loc) Β· 10.6 KB
/
Jenkinsfile
File metadata and controls
253 lines (218 loc) Β· 10.6 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
pipeline {
agent any
options {
buildDiscarder(logRotator(numToKeepStr: '5'))
timeout(time: 30, unit: 'MINUTES')
}
environment {
IMAGE_NAME = "kyumin19/ai"
IMAGE_TAG = "${BUILD_NUMBER}"
DOCKER_IMAGE = "${IMAGE_NAME}:${IMAGE_TAG}"
DISCORD_WEBHOOK = credentials('discord-webhook')
// λ°°ν¬ μλ² μ 보
DEPLOY_HOST = "ec2-54-180-208-86.ap-northeast-2.compute.amazonaws.com"
DEPLOY_USER = "ubuntu"
DEPLOY_PATH = "/srv/app"
// ν¬μ€μ²΄ν¬ μ€μ
HEALTH_CHECK_TIMEOUT = "60"
}
stages {
stage('Checkout') {
steps {
git branch: 'main',
url: 'https://github.com/TeamOldYoung/AI.git'
}
}
stage('Build Validation') {
steps {
sh '''
echo "π λΉλ νκ²½ κ²μ¦ μ€..."
# νμ νμΌ νμΈ
for file in Dockerfile requirements.txt app.py; do
if [ ! -f "$file" ]; then
echo "β $fileμ΄ μμ΅λλ€!"
exit 1
fi
done
echo "β
νμ νμΌ νμΈ μλ£"
echo "π νλ‘μ νΈ κ΅¬μ‘°:"
ls -la
'''
script {
env.COMMIT_INFO = sh(
script: "git log -1 --pretty=format:'%h | %an | %s'",
returnStdout: true
).trim()
env.GIT_COMMIT_SHORT = sh(
script: "git rev-parse --short HEAD",
returnStdout: true
).trim()
}
}
}
stage('Docker Build & Push') {
steps {
script {
echo "π³ Docker μ΄λ―Έμ§ λΉλ μμ: ${DOCKER_IMAGE}"
docker.withRegistry('https://index.docker.io/v1/', 'dockerhub-credentials') {
def image = docker.build("${DOCKER_IMAGE}", "--no-cache --pull .")
echo "π€ Docker μ΄λ―Έμ§ νΈμ μ€..."
image.push("${BUILD_NUMBER}")
image.push("${env.GIT_COMMIT_SHORT}")
image.push("latest")
echo "β
Docker μ΄λ―Έμ§ νΈμ μλ£: ${IMAGE_NAME}:latest"
}
}
}
}
stage('Pre-Deploy Check') {
steps {
script {
sshagent(credentials: ['service-server-ssh']) {
echo "π λ°°ν¬ μ μλ² μν νμΈ..."
def preDeployStatus = sh(
script: """
ssh -o StrictHostKeyChecking=yes -o ConnectTimeout=10 \
${DEPLOY_USER}@${DEPLOY_HOST} '
cd ${DEPLOY_PATH} &&
docker compose ps flask-api || echo "flask-api μλΉμ€ μμ"
'
""",
returnStdout: true
).trim()
echo "νμ¬ μν: ${preDeployStatus}"
}
}
}
}
stage('Deploy') {
steps {
script {
sshagent(credentials: ['service-server-ssh']) {
try {
echo "π Flask AI μλΉμ€ λ°°ν¬ μμ..."
// 1. μ μ΄λ―Έμ§ Pull & μλΉμ€ μ¬μμ
sh """
ssh -o StrictHostKeyChecking=yes -o ConnectTimeout=10 \
${DEPLOY_USER}@${DEPLOY_HOST} '
cd ${DEPLOY_PATH} &&
export FLASK_IMAGE=${IMAGE_NAME} &&
export FLASK_TAG=latest &&
echo "π₯ μ μ΄λ―Έμ§ λ€μ΄λ‘λ μ€..." &&
docker compose pull flask-api &&
echo "π μλΉμ€ μ¬μμ μ€..." &&
docker compose up -d flask-api
'
"""
// 2. 컨ν
μ΄λ μμ λκΈ°
echo "Ⳡ컨ν
μ΄λ μμ λκΈ° (15μ΄)..."
sh """
ssh -o StrictHostKeyChecking=yes -o ConnectTimeout=10 \
${DEPLOY_USER}@${DEPLOY_HOST} 'sleep 15'
"""
// 3. ν¬μ€μ²΄ν¬
echo "β³ ν¬μ€μ²΄ν¬ μμ (μ΅λ ${HEALTH_CHECK_TIMEOUT}μ΄ λκΈ°)..."
def healthCheckResult = sh(
script: """
ssh -o StrictHostKeyChecking=yes -o ConnectTimeout=10 \
${DEPLOY_USER}@${DEPLOY_HOST} '
# 컨ν
μ΄λ μν νμΈ
echo "π 컨ν
μ΄λ μν νμΈ..."
docker ps --filter "name=flask-app" --format "table {{.Names}}\\t{{.Status}}"
# 컨ν
μ΄λκ° μ€ν μ€μΈμ§ νμΈ
if ! docker ps --filter "name=flask-app" --filter "status=running" | grep -q flask-app; then
echo "β flask-app 컨ν
μ΄λκ° μ€νλμ§ μμμ΅λλ€"
docker logs flask-app --tail 20
exit 1
fi
# ν¬μ€μ²΄ν¬ μμ
for i in \$(seq 1 ${HEALTH_CHECK_TIMEOUT}); do
echo "ν¬μ€μ²΄ν¬ \$i/${HEALTH_CHECK_TIMEOUT}..."
# μ§μ curlλ‘ ν
μ€νΈ (docker exec λ΄λΆμμ)
if docker exec flask-app curl -s --fail http://localhost:3000/health > /dev/null 2>&1; then
echo "β
ν¬μ€μ²΄ν¬ μ±κ³΅!"
docker exec flask-app curl -s http://localhost:3000/health
exit 0
fi
# μ€ν¨ μ μμΈ λ‘κ·Έ
if [ \$i -eq 1 ] || [ \$((\$i % 10)) -eq 0 ]; then
echo "π λλ²κ·Έ μ 보 (μλ \$i):"
docker exec flask-app curl -v http://localhost:3000/health || true
echo "π 컨ν
μ΄λ λ‘κ·Έ:"
docker logs flask-app --tail 5
fi
sleep 1
done
echo "β ν¬μ€μ²΄ν¬ μ€ν¨ - μ΅μ’
μν:"
docker logs flask-app --tail 20
exit 1
'
""",
returnStatus: true
)
if (healthCheckResult != 0) {
error "β ν¬μ€μ²΄ν¬ μ€ν¨! λ°°ν¬ μ€λ¨"
}
echo "β
Flask AI μλΉμ€ λ°°ν¬ μ±κ³΅!"
} catch (Exception e) {
echo "β λ°°ν¬ μ€ν¨: ${e.getMessage()}"
throw e
}
}
}
}
}
stage('Post-Deploy Verification') {
steps {
script {
sshagent(credentials: ['service-server-ssh']) {
sh """
ssh -o StrictHostKeyChecking=yes -o ConnectTimeout=10 \
${DEPLOY_USER}@${DEPLOY_HOST} '
echo "π μ΅μ’
μν νμΈ..."
echo "=== 컨ν
μ΄λ μν ==="
docker ps --filter "name=flask-app" --format "table {{.Names}}\\t{{.Status}}\\t{{.Ports}}"
echo "\\n=== μλΉμ€ μλ΅ ν
μ€νΈ ==="
curl -s http://localhost:3000/health | head -3 || echo "μλ΅ μμ"
echo "\\n=== λμ€ν¬ μ¬μ©λ ==="
df -h / | tail -1
'
"""
}
}
}
}
}
post {
success {
script {
sendDiscordNotification("β
Flask AI λ°°ν¬ μ±κ³΅!\\nμ΄λ―Έμ§: ${DOCKER_IMAGE}\\nμλ²: ${DEPLOY_HOST}")
}
}
failure {
script {
sendDiscordNotification("π¨ Flask AI λ°°ν¬ μ€ν¨!\\nλΉλ: #${BUILD_NUMBER}\\nλΈλμΉ: main")
}
}
always {
script {
sh '''
docker system prune -f || echo "Docker μ 리 μ€ν¨"
docker image prune -f || echo "Docker μ΄λ―Έμ§ μ 리 μ€ν¨"
'''
}
cleanWs()
}
}
}
def sendDiscordNotification(String message) {
def commitInfo = (env.COMMIT_INFO ?: "μ 보 μμ").replace('"', '\\"')
def fullMessage = "${message}\\n컀λ°: ${commitInfo}\\nμκ°: ${new Date().format('yyyy-MM-dd HH:mm:ss')}"
withCredentials([string(credentialsId: 'discord-webhook', variable: 'WEBHOOK_URL')]) {
sh """
curl -s -X POST "\${WEBHOOK_URL}" \
-H "Content-Type: application/json" \
-d '{"content": "${fullMessage}"}' || echo "Discord μλ¦Ό μ€ν¨"
"""
}
}