-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile
532 lines (422 loc) · 25.5 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#!groovy
////////////////////////////////
// general config values
////////////////////////////////
//// project build dir order
//// this list contains the build order
//// normally it *should* start with the project under investigation
//// but if this projects depends on some of our other projects, you have to *build the dependencies first*!
//// *IMPORTANT:* you *MUST* use exact repo names as this will used for checkout!
//// *IMPORTANT2:* you must provide exact 4 elements!
projects = ['dwdweathertools']
orgNames = ['ie3-institute']
urls = ['[email protected]:' + orgNames.get(0)]
def sonarqubeProjectKey = "edu.ie3:dwdtools"
//// git webhook trigger token
//// http://JENKINS_URL/generic-webhook-trigger/invoke?token=<webhookTriggerToken>
webhookTriggerToken = "3b7bc553-d596-48d1-beb1-7b07d2b00bd9"
//// internal jenkins credentials link for git ssh keys
//// requires the ssh key to be stored in the internal jenkins credentials keystore
def sshCredentialsId = "19f16959-8a0d-4a60-bd1f-5adb4572b702"
//// internal maven central credentials
def mavenCentralCredentialsId = "87bfb2d4-7613-4816-9fe1-70dfd7e6dec2"
def mavenCentralSignKeyFileId = "dc96216c-d20a-48ff-98c0-1c7ba096d08d"
def mavenCentralSignKeyId = "a1357827-1516-4fa2-ab8e-72cdea07a692"
//// define and setjava version ////
//// requires the java version to be set in the internal jenkins java version management
//// use identifier accordingly
def javaVersionId = 'jdk-17'
//// set java version method (needs node{} for execution)
def setJavaVersion(javaVersionId) {
env.JAVA_HOME = "${tool javaVersionId}"
env.PATH = "${env.JAVA_HOME}/bin:${env.PATH}"
}
/// global config variables that should be available during runtime
/// and will be overwritten during runtime -> DO NOT CHANGE THEM
String featureBranchName = ""
//// gradle tasks that are executed
def gradleTasks = "--refresh-dependencies clean spotlessCheck pmdMain pmdTest spotbugsMain spotbugsTest allTests" // the gradle tasks that are executed on ALL projects
def mainProjectGradleTasks = "jacocoTestReport jacocoTestCoverageVerification" // additional tasks that are only executed on project 0 (== main project)
// if you need additional tasks for deployment add them here
// NOTE: artifactory task with credentials will be added below
def deployGradleTasks = ""
//// error message catch variable
String stageErrorMessage = "Caught error without setting the errorMessage -> new error!"
/// prepare debugging info about deployed artifacts
String deployedArtifacts = "none"
//////////////////
// logger values
//////////////////
def w = "[WARN]"
def e = "[ERROR]"
def i = "[INFO]"
Date date = new Date()
datePart = date.format("dd/MM/yyyy")
timePart = date.format("HH:mm:ss")
def p(String logLevel) {
"${logLevel} [${datePart} - ${timePart}] "
}
def log(String level, String message) {
println(p(level) + message)
}
/////////////////////////
// master branch script
/////////////////////////
if (env.BRANCH_NAME == "master") {
getMasterBranchProps()
// deployment only
if (params.triggered == "true") {
node {
// This displays colors using the 'xterm' ansi color map.
ansiColor('xterm') {
try {
// set java version
setJavaVersion(javaVersionId)
// get the maven central credentials stored in the jenkins secure keychain
withCredentials([usernamePassword(credentialsId: mavenCentralCredentialsId, usernameVariable: 'mavencentral_username', passwordVariable: 'mavencentral_password'),
file(credentialsId: mavenCentralSignKeyFileId, variable: 'mavenCentralKeyFile'),
usernamePassword(credentialsId: mavenCentralSignKeyId, passwordVariable: 'signingPassword', usernameVariable: 'signingKeyId')]) {
deployGradleTasks = "--refresh-dependencies clean allTests " + deployGradleTasks + "publish -Puser=${env.mavencentral_username} -Ppassword=${env.mavencentral_password} -Psigning.keyId=${env.signingKeyId} -Psigning.password=${env.signingPassword} -Psigning.secretKeyRingFile=${env.mavenCentralKeyFile}"
stage('checkout from scm') {
// first we try to get branches from each repo
// the checkout is done in parallel to speed up things a bit
log(i, "getting ${projects.get(0)} ...")
try {
log(i, "Deploy mode. Trying to get ${projects.get(0)} master branch ...")
gitCheckout(projects.get(0), urls.get(0), 'refs/heads/master', sshCredentialsId)
} catch (exc) {
// our target repo failed during checkout
stageErrorMessage = "checkout of master branch of ${projects.get(0)} repo failed!"
sh 'exit 1' // failure due to not found master branch
}
}
stage('deploy') {
log(i, "Deploying ${projects.get(0)} to maven central ...")
gradle("${deployGradleTasks}", projects.get(0))
deployedArtifacts = "${projects.get(0)}, "
}
/**
* Post processing
* Publish reports
* Future clean workspace processes should be declared here
*/
stage('post processing') {
// publish reports
// publishReports()
// set build to successfull
currentBuild.result = 'SUCCESS'
}
}
} catch (Exception exc) {
currentBuild.result = 'FAILURE'
String buildMode = "deploy"
String branchName = params.pull_request_head_label
// publish reports even on failure
publishReports()
// print the error message that might be thrown during one of the build stages
error(stageErrorMessage)
}
}
}
} else {
// merge into master
node {
// This displays colors using the 'xterm' ansi color map.
ansiColor('xterm') {
try {
// set java version
setJavaVersion(javaVersionId)
/// set build name
currentBuild.displayName = "merge pr ${params.pull_request_title}"
/// set the the branch name
/// this is necessary even in merged mode as we might depend on other unmerged branches in other dependency repos with the to-develop feature(s)
featureBranchName = params.pull_request_title
stage('checkout from scm') {
// first we try to get branches from each repo
// the checkout is done in parallel to speed up things a bit
log(i, "getting ${projects.get(0)} ...")
try {
// merged mode
log(i, "Merged mode. Trying to get ${projects.get(0)} master branch ...")
gitCheckout(projects.get(0), urls.get(0), 'refs/heads/master', sshCredentialsId)
} catch (exc) {
// our target repo failed during checkout
stageErrorMessage = "checkout of master branch of ${projects.get(0)} repo failed!"
sh 'exit 1' // failure due to not found master branch
}
}
// the first stage should always be the mainProject -> if it fails we can skip the rest!
stage("gradle allTests ${projects.get(0)}") {
// display java version
sh "java -version"
// build and test the first project
log(i, "building and testing ${projects.get(0)}")
gradle("${gradleTasks} ${mainProjectGradleTasks}", projects.get(0))
}
stage('SonarQube analysis') {
withSonarQubeEnv() { // Will pick the global server connection from jenkins for sonarqube
gradle("sonarqube -Dsonar.branch.name=master -Dsonar.projectKey=$sonarqubeProjectKey ", projects.get(0))
}
}
stage("Quality Gate") {
timeout(time: 1, unit: 'HOURS') {
// Just in case something goes wrong, pipeline will be killed after a timeout
def qg = waitForQualityGate() // Reuse taskId previously collected by withSonarQubeEnv
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
/**
* Deploy to ie3 artifactory making use of the artifactory credentials stored in jenkins secure keychain.
*/
stage('deploy') {
// get the artifactory credentials stored in the jenkins secure keychain
withCredentials([usernamePassword(credentialsId: mavenCentralCredentialsId, usernameVariable: 'mavencentral_username', passwordVariable: 'mavencentral_password'),
file(credentialsId: mavenCentralSignKeyFileId, variable: 'mavenCentralKeyFile'),
usernamePassword(credentialsId: mavenCentralSignKeyId, passwordVariable: 'signingPassword', usernameVariable: 'signingKeyId')]) {
deployGradleTasks = "--refresh-dependencies clean allTests " + deployGradleTasks + "publish -Puser=${env.mavencentral_username} -Ppassword=${env.mavencentral_password} -Psigning.keyId=${env.signingKeyId} -Psigning.password=${env.signingPassword} -Psigning.secretKeyRingFile=${env.mavenCentralKeyFile}"
log(i, "Deploying ${projects.get(0)} to maven central ...")
gradle("--parallel ${deployGradleTasks}", projects.get(0))
deployedArtifacts = "${projects.get(0)}, "
}
}
/**
* Post processing
* Publish reports
* Future clean workspace processes should be declared here
*/
stage('post processing') {
// publish reports
publishReports()
}
} catch (exc) {
currentBuild.result = 'FAILURE'
String buildMode = "merge"
String branchName = params.pull_request_head_label
if (stageErrorMessage == 'Not a merge. Abort!') {
buildMode = ""
}
// publish reports even on failure
publishReports()
// print the error message that might be thrown during one of the build stages
error(stageErrorMessage)
// print the error message that might be thrown during one of the build stages
error(exc)
}
}
}
}
} else {
/////////////////////////
// feature branch script
/////////////////////////
node {
// resolve branch name and configure the project
// which requires a node
getFeatureBranchProps(resolveBranchNo(env.BRANCH_NAME))
// This displays colors using the 'xterm' ansi color map.
ansiColor('xterm') {
try {
// set java version
setJavaVersion(javaVersionId)
/// set the the branch name
featureBranchName = resolveBranchName(env.BRANCH_NAME, orgNames.get(0), projects.get(0))
/// set build name
currentBuild.displayName = featureBranchName + "_${params.sender_login}"
/// if the pipeline is scanned we don't want to execute anything
if (params.triggered == "true") {
log(i, "PR test manually triggered by user.")
} else {
log(i, "forced PR triggered by webhook. Parameters are:")
log(i, "issue_title: ${params.issue_title}")
log(i, "comment_body: ${params.comment_body}")
log(i, "action: ${params.action}")
}
stage('checkout from scm') {
// first we try to get branches from each repo
// we try to get the PR branch name from our target repo if this fails we abort, for all other repos we try to get either the forcedPR branch OR the master branch
// the checkout is done in parallel to speed up things a bit
log(i, "getting ${projects.get(0)} ...")
try {
log(i, "ForcedPR mode. Try to get ${featureBranchName} branch ...")
gitCheckout(projects.get(0), urls.get(0), featureBranchName, sshCredentialsId)
} catch (exc) {
// our target repo failed during checkout
stageErrorMessage = "branch ${featureBranchName} not found in ${projects.get(0)} repo!"
sh 'exit 1' // failure due to not found forcedPR branch
}
}
// the first stage should always be the mainProject -> if it fails we can skip the rest!
stage("gradle allTests ${projects.get(0)}") {
// display java version
sh "java -version"
// build and test the first project
log(i, "building and testing ${projects.get(0)}")
gradle("${gradleTasks} ${mainProjectGradleTasks}", projects.get(0))
}
stage('SonarQube analysis') {
withSonarQubeEnv() { // Will pick the global server connection from jenkins for sonarqube
gradle("sonarqube -Dsonar.projectKey=$sonarqubeProjectKey -Dsonar.pullrequest.branch=${featureBranchName} -Dsonar.pullrequest.key=${resolveBranchNo(env.BRANCH_NAME)} -Dsonar.pullrequest.base=master -Dsonar.pullrequest.github.repository=${orgNames.get(0)}/${projects.get(0)} -Dsonar.pullrequest.provider=Github", projects.get(0))
}
}
stage("Quality Gate") {
timeout(time: 1, unit: 'HOURS') {
// Just in case something goes wrong, pipeline will be killed after a timeout
def qg = waitForQualityGate() // Reuse taskId previously collected by withSonarQubeEnv
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
/**
* Post processing
* Publish reports
* Future clean workspace processes should be declared here
*/
stage('post processing') {
// publish reports
publishReports()
}
} catch (Exception exc) {
currentBuild.result = 'FAILURE'
String buildMode = "forcedPR"
String branchName = resolveBranchName(env.BRANCH_NAME, orgNames.get(0), projects.get(0))
if (stageErrorMessage == 'Not a forcedPR. Abort!') {
buildMode = ""
}
println("[ERROR] [${date.format("dd/MM/yyyy")} - ${date.format("HH:mm:ss")}]" + exc)
// publish reports even on failure
publishReports()
// print the error message that might be thrown during one of the build stages
error(stageErrorMessage)
}
}
}
}
//////////////////////
// jenkins properties
/////////////////////
def getFeatureBranchProps(String prNo) {
properties(
[parameters(
[string(defaultValue: '', description: '', name: 'triggered', trim: true),
string(defaultValue: '', description: '', name: 'issue_title', trim: true),
string(defaultValue: '', description: '', name: 'comment_body', trim: true),
string(defaultValue: '', description: '', name: 'action', trim: true),
string(defaultValue: '', description: '', name: 'issue_url', trim: true),
string(defaultValue: '', description: '', name: 'sender_login', trim: true),
string(defaultValue: '', description: '', name: 'repository_name', trim: true)
]),
[$class: 'RebuildSettings', autoRebuild: false, rebuildDisabled: false],
[$class: 'ThrottleJobProperty', categories: [], limitOneJobWithMatchingParams: false, maxConcurrentPerNode: 0, maxConcurrentTotal: 0, paramsToUseForLimit: '', throttleEnabled: true, throttleOption: 'project'],
[$class: 'JiraProjectProperty'],
pipelineTriggers(
[GenericTrigger(causeString: '$issue_title',
genericVariables:
[[defaultValue: '', key: 'comment_body', regexpFilter: '', value: '$.comment.body'],
[defaultValue: '', key: 'action', regexpFilter: '', value: '$.action'],
[defaultValue: '', key: 'issue_title', regexpFilter: '(WIP:\\s?)', value: '$.issue.title'],
[defaultValue: '', key: 'issue_url', regexpFilter: '', value: '$.issue.url'],
[defaultValue: '', key: 'sender_login', regexpFilter: '', value: '$.sender.login'],
[defaultValue: '', key: 'repository_name', regexpFilter: '', value: '$.repository.name']
],
printContributedVariables: false,
printPostContent: false,
regexpFilterExpression: '^(!test created https:\\/\\/api\\.github\\.com\\/repos\\/.*\\/issues\\/' + prNo + ')$',
regexpFilterText: '$comment_body $action $issue_url',
silentResponse: true,
token: webhookTriggerToken)])])
}
def getMasterBranchProps() {
properties(
[parameters(
[string(defaultValue: '', description: '', name: 'triggered', trim: true),
string(defaultValue: '', description: '', name: 'action', trim: true),
string(defaultValue: '', description: '', name: 'pull_request_title', trim: true),
string(defaultValue: '', description: '', name: 'pull_request_merged', trim: true),
string(defaultValue: '', description: '', name: 'pull_request_state', trim: true),
string(defaultValue: '', description: '', name: 'pull_request_base_ref', trim: true),
string(defaultValue: '', description: '', name: 'pull_request_head_label', trim: true),
string(defaultValue: '', description: '', name: 'repository_name', trim: true)
]),
[$class: 'RebuildSettings', autoRebuild: false, rebuildDisabled: false],
[$class: 'ThrottleJobProperty', categories: [], limitOneJobWithMatchingParams: false, maxConcurrentPerNode: 0, maxConcurrentTotal: 0, paramsToUseForLimit: '', throttleEnabled: true, throttleOption: 'project'],
[$class: 'JiraProjectProperty'],
pipelineTriggers(
[GenericTrigger(causeString: '$issue_title',
genericVariables:
[[defaultValue: '', key: 'action', regexpFilter: '', value: '$.action'],
[defaultValue: '', key: 'pull_request_merged', regexpFilter: '', value: '$.pull_request.merged'],
[defaultValue: '', key: 'pull_request_state', regexpFilter: '', value: '$.pull_request.state'],
[defaultValue: '', key: 'pull_request_base_ref', regexpFilter: '', value: '$.pull_request.base.ref'],
[defaultValue: '', key: 'pull_request_title', regexpFilter: '', value: '$.pull_request.title'],
[defaultValue: '', key: 'pull_request_head_label', regexpFilter: '', value: ' $.pull_request.head.label'],
[defaultValue: '', key: 'repository_name', regexpFilter: '', value: '$.repository.name']],
printContributedVariables: true,
printPostContent: true,
regexpFilterExpression: '^(closed true closed master)$',
regexpFilterText: '$action $pull_request_merged $pull_request_state $pull_request_base_ref',
silentResponse: true,
token: webhookTriggerToken)])])
}
// gradle wrapper for easy execution
// requires the gradle version to be configured with the same name under tools in jenkins configuration
def gradle(String command, String relativeProjectDir) {
env.JENKINS_NODE_COOKIE = 'dontKillMe' // this is necessary for the Gradle daemon to be kept alive
// switch directory to be able to use gradle wrapper
sh(script: """set +x && cd $relativeProjectDir""" + ''' set +x; ./gradlew ''' + """$command""", returnStdout: false)
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// publish reports
// IMPORTANT: has to be called inside the same node{} as where the build process (report generation) took place!
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
def publishReports() {
// publish test reports
publishHTML([allowMissing: true, alwaysLinkToLastBuild: true, escapeUnderscores: false, keepAll: true, reportDir: projects.get(0) + '/build/reports/tests/allTests', reportFiles: 'index.html', reportName: "${projects.get(0)}_java_tests_report", reportTitles: ''])
// publish jacoco report for main project only
publishHTML([allowMissing: true, alwaysLinkToLastBuild: true, escapeUnderscores: false, keepAll: true, reportDir: projects.get(0) + '/build/reports/jacoco', reportFiles: 'index.html', reportName: "${projects.get(0)}_jacoco_report", reportTitles: ''])
// publish pmd report for main project only
publishHTML([allowMissing: true, alwaysLinkToLastBuild: true, escapeUnderscores: false, keepAll: true, reportDir: projects.get(0) + '/build/reports/pmd', reportFiles: 'main.html', reportName: "${projects.get(0)}_pmd_report", reportTitles: ''])
// publish spotbugs report for main project only
publishHTML([allowMissing: true, alwaysLinkToLastBuild: true, escapeUnderscores: false, keepAll: true, reportDir: projects.get(0) + '/build/reports/spotbugs', reportFiles: 'main.html', reportName: "${projects.get(0)}_spotbugs_report", reportTitles: ''])
}
////////////////////////////////////
// git checkout
// NOTE: requires node {}
////////////////////////////////////
def gitCheckout(String relativeTargetDir, String baseUrl, String branch, String sshCredentialsId) {
checkout([
$class : 'GitSCM',
branches : [[name: branch]],
doGenerateSubmoduleConfigurations: false,
extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: relativeTargetDir]],
submoduleCfg : [],
userRemoteConfigs : [[credentialsId: sshCredentialsId, url: baseUrl + "/" + relativeTargetDir + ".git"]]
])
}
def resolveBranchNo(String featureBranchPRMinusNo) {
// get pull request number
def branchNoMatcher = featureBranchPRMinusNo =~ /PR-(.*)/
assert branchNoMatcher.find()
def prNo = branchNoMatcher[0][1]
return prNo
}
def resolveBranchName(String featureBranchPRMinusNo, String orgName, String repoName) {
// get pull request number
def branchNoMatcher = featureBranchPRMinusNo =~ /PR-(.*)/
assert branchNoMatcher.find()
def prNo = branchNoMatcher[0][1]
// curl the repo based on the feature branch no to get the branch information
/// Note: only works for public repos! Otherwise credentials needs to be passed
def curlUrl = "curl https://api.github.com/repos/" + orgName + "/" + repoName + "/pulls/" + prNo
def response = curlUrl.execute().text
def matcher = response =~ /\"label\":\s\"(.+)\"/
assert matcher.find()
// get split the label to account for PRs from forks
def split = matcher[0][1] =~ /(.*):(.*)/
assert matcher.find()
def username = split[0][1]
def branch = split[0][2]
return branch
}