-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile
251 lines (243 loc) · 9.45 KB
/
Jenkinsfile
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
pipeline {
agent any
environment {
DOCKERHUB_CREDENTIALS = credentials('mingy1206-dockerhub')
dockerImage = ''
}
stages {
stage('Git Checkout') {
steps {
checkout scmGit(branches: [[name: '*/develop']], extensions: [], userRemoteConfigs: [[credentialsId: 'mingy1206-github', url: "${GITHUB_ADDRESS_BE}"]])
}
}
stage('Build App') {
steps {
sh 'chmod +x gradlew'
sh './gradlew clean build -x test'
}
post {
success {
withCredentials([string(credentialsId: 'jenkins-be-webhook', variable: 'DISCORD')]) {
discordSend description: "Jenkins CI/CD Trigger Alarm",
footer: "이거 완전 럭키비키잖아!",
link: env.BUILD_URL, result: currentBuild.currentResult,
title: "Jenkins CI/CD Build Success",
webhookURL: "$DISCORD"
}
}
failure {
withCredentials([string(credentialsId: 'jenkins-be-webhook', variable: 'DISCORD')]) {
discordSend description: "Jenkins CI/CD Trigger Alarm",
footer: "이거 완전 럭키비키니시티잖아...",
link: env.BUILD_URL, result: currentBuild.currentResult,
title: "Jenkins CI/CD Build Failed",
webhookURL: "$DISCORD"
}
}
}
}
stage('Test App') {
steps {
sh './gradlew test'
}
post {
success {
withCredentials([string(credentialsId: 'jenkins-be-webhook', variable: 'DISCORD')]) {
discordSend description: "Jenkins CI/CD Trigger Alarm",
footer: "이거 완전 럭키비키잖아!",
link: env.BUILD_URL, result: currentBuild.currentResult,
title: "Jenkins CI/CD Test Success",
webhookURL: "$DISCORD"
}
}
failure {
withCredentials([string(credentialsId: 'jenkins-be-webhook', variable: 'DISCORD')]) {
discordSend description: "Jenkins CI/CD Trigger Alarm",
footer: "이거 완전 럭키비키니시티잖아...",
link: env.BUILD_URL, result: currentBuild.currentResult,
title: "Jenkins CI/CD Test Failed",
webhookURL: "$DISCORD"
}
}
}
}
stage('Login Docker') {
steps {
sh "docker login ${CR_ADDRESS} --username ${ACCESS_ID} --password ${ACCESS_PW}"
}
}
stage('Build Image') {
steps {
script {
sh "docker build -t ${BACKEND_CR_ADDRESS}backend:latest ."
sh "docker build -t ${BACKEND_CR_ADDRESS}backend:${BUILD_NUMBER} ."
}
}
post {
success {
withCredentials([string(credentialsId: 'jenkins-be-webhook', variable: 'DISCORD')]) {
discordSend description: "Jenkins CI/CD Trigger Alarm",
footer: "이거 완전 럭키비키잖아!",
link: env.BUILD_URL, result: currentBuild.currentResult,
title: "Jenkins CI/CD Image Build Success",
webhookURL: "$DISCORD"
}
}
failure {
withCredentials([string(credentialsId: 'jenkins-be-webhook', variable: 'DISCORD')]) {
discordSend description: "Jenkins CI/CD Trigger Alarm",
footer: "이거 완전 럭키비키니시티잖아...",
link: env.BUILD_URL, result: currentBuild.currentResult,
title: "Jenkins CI/CD Image Build Failed",
webhookURL: "$DISCORD"
}
}
}
}
stage('Push & Clean Image') {
steps {
script {
sh "docker push ${BACKEND_CR_ADDRESS}backend:latest"
sh "docker push ${BACKEND_CR_ADDRESS}backend:${BUILD_NUMBER}"
sh "docker rmi ${BACKEND_CR_ADDRESS}backend:latest"
sh "docker rmi ${BACKEND_CR_ADDRESS}backend:${BUILD_NUMBER}"
}
}
post {
success {
withCredentials([string(credentialsId: 'jenkins-be-webhook', variable: 'DISCORD')]) {
discordSend description: "Jenkins CI/CD Trigger Alarm",
footer: "이거 완전 럭키비키잖아!",
link: env.BUILD_URL, result: currentBuild.currentResult,
title: "Jenkins CI/CD Image Push Success",
webhookURL: "$DISCORD"
}
}
failure {
withCredentials([string(credentialsId: 'jenkins-be-webhook', variable: 'DISCORD')]) {
discordSend description: "Jenkins CI/CD Trigger Alarm",
footer: "이거 완전 럭키비키니시티잖아...",
link: env.BUILD_URL, result: currentBuild.currentResult,
title: "Jenkins CI/CD Image Push Failed",
webhookURL: "$DISCORD"
}
}
}
}
stage('Canary Deployment to Instance A') {
steps {
script {
sshagent(['kc-enter-be-a']) {
sh """
ssh -o StrictHostKeyChecking=no ubuntu@${KC_ENTER_BE_A_IP} '
sudo docker login ${CR_ADDRESS} --username ${ACCESS_ID} --password ${ACCESS_PW} &&
sudo docker pull ${BACKEND_CR_ADDRESS}backend:latest &&
sudo docker stop app || true &&
sudo docker rm app || true &&
sudo docker run -d -v spring_logs_backend:/var/log/spring -v promtail_positions_backend:/etc/promtail/positions -e JAVA_OPTS1=-Ddev-vault-token=${VAULT_TOKEN} -e JAVA_OPTS2=-Ddev-vault-host=${VAULT_HOST} -e JAVA_OPTS3=-Ddev-vault-port=${VAULT_PORT} --name app -p ${BACKEND_APP_PORT}:${BACKEND_APP_PORT} ${BACKEND_CR_ADDRESS}backend:latest
'
"""
}
}
}
post {
success {
withCredentials([string(credentialsId: 'jenkins-be-webhook', variable: 'DISCORD')]) {
discordSend description: "Jenkins CI/CD Trigger Alarm",
footer: "이거 완전 럭키비키잖아!",
link: env.BUILD_URL, result: currentBuild.currentResult,
title: "Jenkins CI/CD Image Deploy 1/2 Success -> Next Stage 3~9M Wait",
webhookURL: "$DISCORD"
}
}
failure {
withCredentials([string(credentialsId: 'jenkins-be-webhook', variable: 'DISCORD')]) {
discordSend description: "Jenkins CI/CD Trigger Alarm",
footer: "이거 완전 럭키비키니시티잖아...",
link: env.BUILD_URL, result: currentBuild.currentResult,
title: "Jenkins CI/CD Image Deploy 1/2 Failed",
webhookURL: "$DISCORD"
}
}
}
}
stage('Health Check') {
steps {
script {
def retries = 3
def delay = 180 // seconds
def success = false
for (int i = 0; i < retries; i++) {
def canaryHealth = sh(script: "curl -s http://${KC_ENTER_BE_A_IP}:${BACKEND_APP_PORT}/actuator/health", returnStatus: true)
if (canaryHealth == 0) {
success = true
break
}
sleep(delay)
}
if (!success) {
error("Canary deployment failed. Instance A health check failed after ${retries} retries.")
}
}
}
post {
success {
withCredentials([string(credentialsId: 'jenkins-be-webhook', variable: 'DISCORD')]) {
discordSend description: "Jenkins CI/CD Trigger Alarm",
footer: "이거 완전 럭키비키잖아!",
link: env.BUILD_URL, result: currentBuild.currentResult,
title: "Jenkins CI/CD Image Deploy 1/2 State Check Success",
webhookURL: "$DISCORD"
}
}
failure {
withCredentials([string(credentialsId: 'jenkins-be-webhook', variable: 'DISCORD')]) {
discordSend description: "Jenkins CI/CD Trigger Alarm",
footer: "이거 완전 럭키비키니시티잖아...",
link: env.BUILD_URL, result: currentBuild.currentResult,
title: "Jenkins CI/CD Image Deploy 1/2 State Check Failed",
webhookURL: "$DISCORD"
}
}
}
}
stage('Canary Deployment to other Instance') {
steps {
script {
// input message: 'Proceed with full deployment?', ok: 'Deploy '
sshagent(['kc-enter-be-b']) {
sh """
ssh -o StrictHostKeyChecking=no ubuntu@${KC_ENTER_BE_B_IP} '
sudo docker login ${CR_ADDRESS} --username ${ACCESS_ID} --password ${ACCESS_PW} &&
sudo docker pull ${BACKEND_CR_ADDRESS}backend:latest &&
sudo docker stop app || true &&
sudo docker rm app || true &&
sudo docker run -d -v spring_logs_backend:/var/log/spring -v promtail_positions_backend:/etc/promtail/positions -e JAVA_OPTS1=-Ddev-vault-token=${VAULT_TOKEN} -e JAVA_OPTS2=-Ddev-vault-host=${VAULT_HOST} -e JAVA_OPTS3=-Ddev-vault-port=${VAULT_PORT} --name app -p ${BACKEND_APP_PORT}:${BACKEND_APP_PORT} ${BACKEND_CR_ADDRESS}backend:latest
'
"""
}
}
}
post {
success {
withCredentials([string(credentialsId: 'jenkins-be-webhook', variable: 'DISCORD')]) {
discordSend description: "Jenkins CI/CD Trigger Alarm",
footer: "이거 완전 럭키비키잖아!",
link: env.BUILD_URL, result: currentBuild.currentResult,
title: "Jenkins CI/CD Image Deploy 2/2 Success",
webhookURL: "$DISCORD"
}
}
failure {
withCredentials([string(credentialsId: 'jenkins-be-webhook', variable: 'DISCORD')]) {
discordSend description: "Jenkins CI/CD Trigger Alarm",
footer: "이거 완전 럭키비키니시티잖아...",
link: env.BUILD_URL, result: currentBuild.currentResult,
title: "Jenkins CI/CD Image Deploy 2/2 Failed",
webhookURL: "$DISCORD"
}
}
}
}
}
}