-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJenkinsfile
333 lines (317 loc) · 10.8 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
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
void setBuildStatus(String message, String state) {
step([
$class: "GitHubCommitStatusSetter",
reposSource: [$class: "ManuallyEnteredRepositorySource", url: "https://github.com/Team-Muffin/Server.git"],
contextSource: [$class: "ManuallyEnteredCommitContextSource", context: "ci/jenkins/build-status"],
errorHandlers: [[$class: "ChangingBuildStatusErrorHandler", result: "UNSTABLE"]],
statusResultSource: [$class: "ConditionalStatusResultSource", results: [[$class: "AnyBuildResult", message: message, state: state]]]
])
}
def userApp = false
def boardApp = false
def challengeApp = false
def productApp = false
def creditApp = false
def utils = false
def failureCnt = 0
pipeline {
agent any
post {
failure {
setBuildStatus("Build failed", "FAILURE")
slackSend (
channel: 'C078D2K42MD',
color: '#FF0000',
message: "FAIL: Job ${env.JOB_NAME} [${env.BUILD_NUMBER}]"
)
}
success {
setBuildStatus("Build successful", "SUCCESS")
slackSend (
channel: 'C078D2K42MD',
color: '#00FF00',
message: "SUCCESS: Job ${env.JOB_NAME} [${env.BUILD_NUMBER}]"
)
}
}
stages {
stage('init') {
steps {
echo 'init pipeline, check changes'
script {
def buildCause = currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause')
echo "manually started by ${buildCause}"
if (!buildCause.isEmpty()) {
echo "triggered by user"
userApp = true
boardApp = true
challengeApp = true
productApp = true
creditApp = true
alertApp = true
} else {
def changedFiles = sh(returnStdout: true, script: 'git diff --name-only --diff-filter=ACMRT HEAD^ HEAD').trim().split('\n')
def changedDirs = new HashSet()
echo "files changed : ${changedFiles}"
for (def file : changedFiles) {
echo "new file : ${file}"
if (file.startsWith('utils/')) {
utils = true
break
} else if (file.startsWith('applications/')) {
def dir = file.split('/')[1]
changedDirs.add(dir)
echo "modified : ${dir}"
} else {
changedDirs.add(file)
}
}
userApp = changedDirs.contains('user-application')
boardApp = changedDirs.contains('board-application')
challengeApp = changedDirs.contains('challenge-application')
productApp = changedDirs.contains('product-application')
creditApp = changedDirs.contains('credit-application')
alertApp = changedDirs.contains('alert-application')
}
}
sh 'cp /var/jenkins_home/workspace/configs/server/s3-keys.properties ./utils/s3-utils/src/main/resources/application-keys.properties'
echo "utils : ${utils}, user : ${userApp}, board : ${boardApp}, challenge : ${challengeApp}, product : ${productApp}, credit : ${creditApp}, alert: ${alertApp}"
}
}
stage('build user app') {
when {
anyOf {
expression { userApp }
expression { utils }
}
}
steps {
echo 'copy configuration files for user app'
sh 'cp /var/jenkins_home/workspace/configs/server/user/application.yml ./applications/user-application/src/main/resources/application.yml'
echo 'start gradle build for user app'
dir('./') {
sh 'chmod +x ./gradlew'
sh './gradlew clean'
sh './gradlew :applications:user-application:build'
}
echo 'start docker build for user app'
dir('./applications/user-application/') {
sh 'docker build -t bkkmw/tofin-user-api .'
sh 'docker push bkkmw/tofin-user-api'
}
echo 'publish over ssh for user app'
script {
try {
publishOverSSH('user-api', 'tofin-user-api')
echo "Publish over ssh Successful"
} catch(Exception e) {
echo "Publish over ssh failed : ${e.message}"
currentBuild.result = 'FAILURE'
// stop on failure
failFast true
}
}
}
}
stage('build board app') {
when {
anyOf {
expression { boardApp }
expression { utils }
}
}
steps {
echo 'copy configuration files for board app'
sh 'cp /var/jenkins_home/workspace/configs/server/board/application.yml ./applications/board-application/src/main/resources/application.yml'
echo 'start gradle build for board app'
dir('./') {
sh 'chmod +x ./gradlew'
sh './gradlew clean'
sh './gradlew :applications:board-application:build'
}
echo 'start docker build for board app'
dir('./applications/board-application/') {
sh 'docker build -t bkkmw/tofin-board-api .'
sh 'docker push bkkmw/tofin-board-api'
}
echo 'publish over ssh for board app'
script {
try {
publishOverSSH('board-api', 'tofin-board-api')
echo "Publish over ssh Successful"
} catch(Exception e) {
echo "Publish over ssh failed : ${e.message}"
currentBuild.result = 'FAILURE'
// stop on failure
failFast true
}
}
}
}
stage('build product app') {
when {
anyOf {
expression { productApp }
expression { utils }
}
}
steps {
echo 'copy configuration files for board app'
sh 'cp /var/jenkins_home/workspace/configs/server/product/application.yml ./applications/product-application/src/main/resources/application.yml'
echo 'start gradle build for product app'
dir('./') {
sh 'chmod +x ./gradlew'
sh './gradlew clean'
sh './gradlew :applications:product-application:build'
}
echo 'start docker build for product app'
dir('./applications/product-application/') {
sh 'docker build -t bkkmw/tofin-prod-api .'
sh 'docker push bkkmw/tofin-prod-api'
}
echo 'publish over ssh for product app'
script {
try {
publishOverSSH('prod-api', 'tofin-prod-api')
echo "Publish over ssh Successful"
} catch(Exception e) {
echo "Publish over ssh failed : ${e.message}"
currentBuild.result = 'FAILURE'
// stop on failure
failFast true
}
}
}
}
stage('build challenge app') {
when {
anyOf {
expression { challengeApp }
expression { utils }
}
}
steps {
echo 'copy configuration files for challenge app'
sh 'cp /var/jenkins_home/workspace/configs/server/challenge/application.yml ./applications/challenge-application/src/main/resources/application.yml'
echo 'start gradle build for challenge app'
dir('./') {
sh 'chmod +x ./gradlew'
sh './gradlew clean'
sh './gradlew :applications:challenge-application:build'
}
echo 'start docker build for challenge app'
dir('./applications/challenge-application/') {
sh 'docker build -t bkkmw/tofin-challenge-api .'
sh 'docker push bkkmw/tofin-challenge-api'
}
echo 'publish over ssh for challenge app'
script {
try {
publishOverSSH('challenge-api', 'tofin-challenge-api')
echo "Publish over ssh Successful"
} catch(Exception e) {
echo "Publish over ssh failed : ${e.message}"
currentBuild.result = 'FAILURE'
}
}
}
}
stage('build credit app') {
when {
anyOf {
expression { creditApp }
expression { utils }
}
}
steps {
echo 'copy configuration files for credit app'
sh 'cp /var/jenkins_home/workspace/configs/server/credit/application.yml ./applications/credit-application/src/main/resources/application.yml'
echo 'start gradle build for credit app'
dir('./') {
sh 'chmod +x ./gradlew'
sh './gradlew clean'
sh './gradlew :applications:credit-application:build'
}
echo 'start docker build for credit app'
dir('./applications/credit-application/') {
sh 'docker build -t bkkmw/tofin-credit-api .'
sh 'docker push bkkmw/tofin-credit-api'
}
echo 'publish over ssh for credit app'
script {
try {
publishOverSSH('credit-api', 'tofin-credit-api')
echo "Publish over ssh Successful"
} catch(Exception e) {
echo "Publish over ssh failed : ${e.message}"
currentBuild.result = 'FAILURE'
}
}
}
}
stage('build alert app') {
when {
anyOf {
expression { alertApp }
expression { utils }
}
}
steps {
echo 'copy configuration files for alert app'
sh 'cp /var/jenkins_home/workspace/configs/server/alert/application.yml ./applications/alert-application/src/main/resources/application.yml'
echo 'start gradle build for alert app'
dir('./') {
sh 'chmod +x ./gradlew'
sh './gradlew clean'
sh './gradlew :applications:alert-application:build'
}
echo 'start docker build for alert app'
dir('./applications/alert-application/') {
sh 'docker build -t bkkmw/tofin-alert-api .'
sh 'docker push bkkmw/tofin-alert-api'
}
echo 'publish over ssh for alert app'
script {
try {
publishOverSSH('alert-api', 'tofin-alert-api')
echo "Publish over ssh Successful"
} catch(Exception e) {
echo "Publish over ssh failed : ${e.message}"
currentBuild.result = 'FAILURE'
}
}
}
}
stage('clean') {
steps {
echo 'clean unused image'
sh 'docker image prune --force'
}
}
}
}
def publishOverSSH(serverName, imageName) {
sshPublisher(
failOnError: true,
publishers: [
sshPublisherDesc(
configName: serverName, // SSH server name
verbose: true,
transfers: [
sshTransfer(
cleanRemote: true, // clean remote dir
excludes: '',
execCommand: "/bin/bash /home/ubuntu/depl/deploy.sh ${imageName}",
execTimeout: 200000,
makeEmptyDirs: true,
noDefaultExcludes: false,
remoteDirectory: 'depl',
remoteDirectorySDF: false,
removePrefix: 'scripts',
sourceFiles: 'scripts/deploy.sh'
)
]
)
]
)
}