-
Notifications
You must be signed in to change notification settings - Fork 3
/
Jenkinsfile
530 lines (467 loc) · 18.7 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
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import java.util.regex.Pattern
@NonCPS
String getUrlFromRoute(String routeName, String projectNameSpace = '') {
def nameSpaceFlag = ''
if(projectNameSpace?.trim()) {
nameSpaceFlag = "-n ${projectNameSpace}"
}
def url = sh (
script: "oc get routes ${nameSpaceFlag} -o wide --no-headers | awk \'/${routeName}/{ print match(\$0,/edge/) ? \"https://\"\$2 : \"http://\"\$2 }\'",
returnStdout: true
).trim()
return url
}
/*
* Sends a rocket chat notification
*/
def notifyRocketChat(text, url) {
def rocketChatURL = url
def message = text.replaceAll(~/\'/, "")
def payload = JsonOutput.toJson([
"username":"Jenkins",
"icon_url":"https://wiki.jenkins.io/download/attachments/2916393/headshot.png",
"text": message
])
sh("curl -X POST -H 'Content-Type: application/json' --data \'${payload}\' ${rocketChatURL}")
}
/*
* takes in a sonarqube status json payload
* and returns the status string
*/
def sonarGetStatus (jsonPayload) {
def jsonSlurper = new JsonSlurper()
return jsonSlurper.parseText(jsonPayload).projectStatus.status
}
/*
* takes in a sonarqube status json payload
* and returns the date string
*/
def sonarGetDate (jsonPayload) {
def jsonSlurper = new JsonSlurper()
return jsonSlurper.parseText(jsonPayload).projectStatus.periods[0].date
}
boolean imageTaggingComplete ( String sourceTag, String destinationTag, String action, def iterations = 6 ) {
def sourceImageName = sh returnStdout: true, script: "oc describe istag/lup-admin-static:${sourceTag} | head -n 1".trim()
def destinationImageName = sh returnStdout: true, script: "oc describe istag/lup-admin-static:${destinationTag} | head -n 1".trim()
int delay = 0
for (int i=0; i<iterations; i++){
echo "waiting to ${action}, iterator is: ${i}, the max iterator is: ${iterations} \n ${sourceTag}: ${sourceImageName} ${destinationTag}: ${destinationImageName}"
if(sourceImageName == destinationImageName){
echo "${action} complete"
return true
} else {
delay = (1<<i) // exponential backoff
sleep(delay)
destinationImageName = sh returnStdout: true, script: "oc describe istag/lup-admin-static:${destinationTag} | head -n 1".trim()
}
}
return false
}
boolean sonarqubeReportComplete ( String oldDate, String sonarqubeStatusUrl, def iterations = 6 ) {
def oldSonarqubeReportDate = oldDate
def newSonarqubeReportDate = sonarGetDate ( sh ( returnStdout: true, script: "curl -w '%{http_code}' '${sonarqubeStatusUrl}'" ) )
int delay = 0
for (int i=0; i<iterations; i++) {
echo "waiting for sonarqube report, iterator is: ${i}, max iterator is: ${iterations} \n Old Date: ${oldSonarqubeReportDate} \n New Date: ${newSonarqubeReportDate}"
if (oldSonarqubeReportDate != newSonarqubeReportDate) {
echo "sonarqube report complete"
return true
} else {
delay = (1<<i) // exponential backoff
sleep(delay)
newSonarqubeReportDate = sonarGetDate ( sh ( returnStdout: true, script: "curl -w '%{http_code}' '${sonarqubeStatusUrl}'" ) )
}
}
return false
}
/*
* Updates the global pastBuilds array: it will iterate recursively
* and add all the builds prior to the current one that had a result
* different than 'SUCCESS'.
*/
def buildsSinceLastSuccess(previousBuild, build) {
if ((build != null) && (build.result != 'SUCCESS')) {
pastBuilds.add(build)
buildsSinceLastSuccess(pastBuilds, build.getPreviousBuild())
}
}
/*
* Generates a string containing all the commit messages from
* the builds in pastBuilds.
*/
@NonCPS
def getChangeLog(pastBuilds) {
def log = ""
for (int x = 0; x < pastBuilds.size(); x++) {
for (int i = 0; i < pastBuilds[x].changeSets.size(); i++) {
def entries = pastBuilds[x].changeSets[i].items
for (int j = 0; j < entries.length; j++) {
def entry = entries[j]
log += "* ${entry.msg} by ${entry.author} \n"
}
}
}
return log;
}
def nodejsTester () {
openshift.withCluster() {
openshift.withProject() {
podTemplate(
label: 'node-tester',
name: 'node-tester',
serviceAccount: 'jenkins',
cloud: 'openshift',
slaveConnectTimeout: 300,
containers: [
containerTemplate(
name: 'jnlp',
image: 'registry.access.redhat.com/openshift3/jenkins-agent-nodejs-8-rhel7',
resourceRequestCpu: '500m',
resourceLimitCpu: '1000m',
resourceRequestMemory: '2Gi',
resourceLimitMemory: '4Gi',
workingDir: '/tmp',
command: '',
)
]
) {
node("node-tester") {
checkout scm
sh 'npm i'
try {
sh 'npm run tests'
} finally {
echo "Unit Tests Passed"
}
}
}
return true
}
}
}
// todo templates can be pulled from a repository, instead of declared here
def nodejsSonarqube () {
openshift.withCluster() {
openshift.withProject() {
podTemplate(
label: 'node-sonarqube',
name: 'node-sonarqube',
serviceAccount: 'jenkins',
cloud: 'openshift',
slaveConnectTimeout: 300,
containers: [
containerTemplate(
name: 'jnlp',
image: 'registry.access.redhat.com/openshift3/jenkins-agent-nodejs-8-rhel7',
resourceRequestCpu: '500m',
resourceLimitCpu: '1000m',
resourceRequestMemory: '2Gi',
resourceLimitMemory: '4Gi',
workingDir: '/tmp',
command: '',
args: '${computer.jnlpmac} ${computer.name}',
)
]
) {
node("node-sonarqube") {
checkout scm
dir('sonar-runner') {
try {
// get sonarqube url
def SONARQUBE_URL = getUrlFromRoute('sonarqube').trim()
echo "${SONARQUBE_URL}"
// sonarqube report link
def SONARQUBE_STATUS_URL = "${SONARQUBE_URL}/api/qualitygates/project_status?projectKey=lup-api"
// The name of your SonarQube project
def SONAR_PROJECT_NAME = 'lup-api'
// The project key of your SonarQube project
def SONAR_PROJECT_KEY = 'lup-api'
// The base directory of your project.
// This is relative to the location of the `sonar-runner` directory within your project.
// More accurately this is relative to the Gradle build script(s) that manage the SonarQube Scanning
def SONAR_PROJECT_BASE_DIR = '../'
// The source code directory you want to scan.
// This is relative to the project base directory.
def SONAR_SOURCES = './api'
boolean firstScan = false;
def OLD_SONAR_DATE
try {
// get old sonar report date
def OLD_SONAR_DATE_JSON = sh(returnStdout: true, script: "curl -w '%{http_code}' '${SONARQUBE_STATUS_URL}'")
OLD_SONAR_DATE = sonarGetDate (OLD_SONAR_DATE_JSON)
} catch (error) {
firstScan = true
}
// run scan
//sh("oc extract secret/sonarqube-secrets --to=${env.WORKSPACE}/sonar-runner --confirm")
//SONARQUBE_URL = sh(returnStdout: true, script: 'cat sonarqube-route-url')
sh "npm install [email protected]"
sh returnStdout: true, script: "./gradlew sonarqube --stacktrace --info --debug \
-Dsonar.host.url=${SONARQUBE_URL} \
-Dsonar. \
-Dsonar.verbose=true \
-Dsonar.projectName='${SONAR_PROJECT_NAME}' \
-Dsonar.projectKey=${SONAR_PROJECT_KEY} \
-Dsonar.projectBaseDir=${SONAR_PROJECT_BASE_DIR} \
-Dsonar.sources=${SONAR_SOURCES}"
if ( !firstScan ) {
// wiat for report to be updated
if ( !sonarqubeReportComplete ( OLD_SONAR_DATE, SONARQUBE_STATUS_URL ) ) {
echo "sonarqube report failed to complete, or timed out"
notifyRocketChat(
"@all *[API][ERROR]* \n The latest build, ${env.BUILD_DISPLAY_NAME} of landuseplanning-admin seems to be broken. \n ${env.BUILD_URL}\n Error: \n sonarqube report failed to complete, or timed out : ${SONARQUBE_URL}",
ROCKET_DEPLOY_WEBHOOK
)
currentBuild.result = "FAILURE"
exit 1
}
} else {
sleep (30)
}
// check if sonarqube passed
//sh("oc extract secret/sonarqube-status-urls --to=${env.WORKSPACE}/sonar-runner --confirm")
//SONARQUBE_STATUS_URL = sh(returnStdout: true, script: 'cat sonarqube-status-api')
SONARQUBE_STATUS_JSON = sh(returnStdout: true, script: "curl -w '%{http_code}' '${SONARQUBE_STATUS_URL}'")
SONARQUBE_STATUS = sonarGetStatus (SONARQUBE_STATUS_JSON)
if ( "${SONARQUBE_STATUS}" == "ERROR") {
echo "Scan Failed"
notifyRocketChat(
"@all *[API][ERROR]* \n The latest build ${env.BUILD_DISPLAY_NAME} of lup-api seems to be broken. \n ${env.BUILD_URL}\n Error: \n Sonarqube scan failed",
ROCKET_DEPLOY_WEBHOOK
)
currentBuild.result = 'FAILURE'
exit 1
} else {
echo "Scan Passed"
}
} catch (error) {
notifyRocketChat(
"@all *[API][ERROR]* \n The latest build ${env.BUILD_DISPLAY_NAME} of lup-api seems to be broken. \n ${env.BUILD_URL}\n Error: \n ${error}",
ROCKET_DEPLOY_WEBHOOK
)
throw error
} finally {
echo "Scan Complete"
}
}
}
}
return true
}
}
}
def zapScanner () {
openshift.withCluster() {
openshift.withProject() {
// The jenkins-slave-zap image has been purpose built for supporting ZAP scanning.
podTemplate(
label: 'owasp-zap',
name: 'owasp-zap',
serviceAccount: 'jenkins',
cloud: 'openshift',
slaveConnectTimeout: 300,
containers: [
containerTemplate(
name: 'jnlp',
image: '172.50.0.2:5000/openshift/jenkins-slave-zap',
//image: '172.50.0.2:5000/owasp/zap2docker-weekly',
resourceRequestCpu: '500m',
resourceLimitCpu: '1000m',
resourceRequestMemory: '3Gi',
resourceLimitMemory: '4Gi',
workingDir: '/home/jenkins',
command: '',
args: '${computer.jnlpmac} ${computer.name}'
)
]
){
node('owasp-zap') {
// The name of the ZAP report
def ZAP_REPORT_NAME = "zap-report.xml"
// The location of the ZAP reports
def ZAP_REPORT_PATH = "/zap/wrk/${ZAP_REPORT_NAME}"
// The name of the "stash" containing the ZAP report
def ZAP_REPORT_STASH = "zap-report"
// Dynamicaly determine the target URL for the ZAP scan ...
def TARGET_URL = getUrlFromRoute('lup-public-dev', 'xti26n-dev').trim() + "/api/docs/"
echo "Target URL: ${TARGET_URL}"
dir('zap') {
try {
// The ZAP scripts are installed on the root of the jenkins-slave-zap image.
// When running ZAP from there the reports will be created in /zap/wrk/ by default.
// ZAP has problems with creating the reports directly in the Jenkins
// working directory, so they have to be copied over after the fact.
def retVal = sh (
returnStatus: true,
script: "/zap/zap-api-scan.py -d -x ${ZAP_REPORT_NAME} -t ${TARGET_URL} -f openapi"
)
echo "Return value is: ${retVal}"
// Copy the ZAP report into the Jenkins working directory so the Jenkins tools can access it.
sh (
returnStdout: true,
script: "mkdir -p ./wrk/ && cp /zap/wrk/${ZAP_REPORT_NAME} ./wrk/"
)
} catch (error) {
/*
// revert dev from backup
echo "Reverting dev image form backup..."
openshiftTag destStream: 'lup-public-static', verbose: 'false', destTag: 'dev', srcStream: 'lup-public-static', srcTag: 'dev-backup'
// wait for revert to complete
if(!imageTaggingComplete ('dev-backup', 'dev', 'revert')) {
echo "Failed to revert dev image after Zap scan failed, please revert the dev image manually from dev-backup"
notifyRocketChat(
"@all The latest build, ${env.BUILD_DISPLAY_NAME} of landuseplanning-public seems to be broken. \n ${env.BUILD_URL}\n Error: \n Zap scan failed: ${SONARQUBE_URL} \n Automatic revert of the deployment also failed, please revert the dev image manually from dev-backup",
ROCKET_DEPLOY_WEBHOOK
)
currentBuild.result = "FAILURE"
exit 1
}
notifyRocketChat(
"@all *[API][ERROR]* \n The latest build, ${env.BUILD_DISPLAY_NAME} of landuseplanning-api seems to be broken. \n ${env.BUILD_URL}\n Error: \n Zap scan failed: \n dev image was reverted",
ROCKET_DEPLOY_WEBHOOK
)*/
/* still allow a pass while working on the zap scan issues
currentBuild.result = "FAILURE"
exit 1*/
return
}
}
// Stash the ZAP report for publishing in a different stage (which will run on a different pod).
//echo "Stash the report for the publishing stage ..."
//stash name: "${ZAP_REPORT_STASH}", includes: "zap/wrk/*.xml"
}
}
}
}
}
def CHANGELOG = "No new changes"
def IMAGE_HASH = "latest"
pipeline {
agent any
options {
disableResume()
}
stages {
stage('Parallel Build Steps') {
failFast true
parallel {
stage('Build') {
agent any
steps {
script {
pastBuilds = []
buildsSinceLastSuccess(pastBuilds, currentBuild);
CHANGELOG = getChangeLog(pastBuilds);
echo ">>>>>>Changelog: \n ${CHANGELOG}"
try {
sh("oc extract secret/rocket-chat-secrets --to=${env.WORKSPACE} --confirm")
ROCKET_DEPLOY_WEBHOOK = sh(returnStdout: true, script: 'cat rocket-deploy-webhook')
echo "Building lup-api develop branch"
openshiftBuild bldCfg: 'lup-api', showBuildLogs: 'true'
echo "Build done"
echo ">>> Get Image Hash"
// Don't tag with BUILD_ID so the pruner can do it's job; it won't delete tagged images.
// Tag the images for deployment based on the image's hash
IMAGE_HASH = sh (
script: """oc get istag lup-api:latest -o template --template=\"{{.image.dockerImageReference}}\"|awk -F \":\" \'{print \$3}\'""",
returnStdout: true).trim()
echo ">> IMAGE_HASH: ${IMAGE_HASH}"
} catch (error) {
notifyRocketChat(
"@all *[API][ERROR]* \n The build ${env.BUILD_DISPLAY_NAME} of lup-api, seems to be broken.\n ${env.BUILD_URL}\n Error: \n ${error.message}",
ROCKET_DEPLOY_WEBHOOK
)
throw error
}
}
}
}
/*
stage('Unit Tests') {
steps {
script {
echo "Running Unit Tests"
def result = nodejsTester()
}
}
}*/
// stage('Sonarqube') {
// steps {
// script {
// echo "Running Sonarqube"
// def result = nodejsSonarqube()
// }
// }
// }
}
}
stage('Deploy to dev'){
steps {
script {
try {
echo "Deploying to dev..."
openshiftTag destStream: 'lup-api', verbose: 'false', destTag: 'dev', srcStream: 'lup-api', srcTag: "${IMAGE_HASH}"
sleep 5
// todo lup-test? what depCfg?
openshiftVerifyDeployment depCfg: 'gcpe-lup-api-dev', namespace: 'xti26n-dev', replicaCount: 1, verbose: 'false', verifyReplicaCount: 'false', waitTime: 600000
echo ">>>> Deployment Complete"
} catch (error) {
notifyRocketChat(
"@all *[API][ERROR]* \n The build ${env.BUILD_DISPLAY_NAME} of lup-api, seems to be broken.\n ${env.BUILD_URL}\n Error: \n ${error.message}",
ROCKET_DEPLOY_WEBHOOK
)
currentBuild.result = "FAILURE"
throw new Exception("Dev Deploy failed")
}
}
}
}
stage('Zap') {
steps {
script {
echo "Running Zap Scan"
def result = zapScanner()
}
}
}
stage('Deploy to test'){
steps {
script {
try {
notifyRocketChat(
"@all *[API][DEV][INPUT]* \n A new version of lup-api is now in Dev, build: *${env.BUILD_DISPLAY_NAME}* \n Changes: \n ${CHANGELOG} \n\n _*Input is required to move this build to TEST*_\n${env.BUILD_URL}input",
ROCKET_DEPLOY_WEBHOOK
)
input "Deploy to test?"
} catch (error) {
echo "User has chosen to not deploy to test"
echo ">>>> Pipeline Complete"
currentBuild.result = 'SUCCESS'
return
}
try {
echo "Deploying to test..."
openshiftTag destStream: 'lup-api', verbose: 'false', destTag: 'test', srcStream: 'lup-api', srcTag: "${IMAGE_HASH}"
sleep 5
// todo lup-test? what depCfg?
openshiftVerifyDeployment depCfg: 'lup-api-test', namespace: 'xti26n-test', replicaCount: 1, verbose: 'false', verifyReplicaCount: 'false', waitTime: 600000
echo ">>>> Deployment Complete"
notifyRocketChat(
"*[API][TEST]* \n A new version of lup-api is now in test, build: *${env.BUILD_DISPLAY_NAME}* \n Changes: \n ${CHANGELOG}",
ROCKET_DEPLOY_WEBHOOK
)
} catch (error) {
notifyRocketChat(
"@all *[API][ERROR]* \n The build ${env.BUILD_DISPLAY_NAME} of lup-api, seems to be broken.\n ${env.BUILD_URL}\n Error: \n ${error.message}",
ROCKET_DEPLOY_WEBHOOK
)
currentBuild.result = "FAILURE"
throw new Exception("Test Deploy failed")
}
}
}
}
}
}