forked from bcgov/eagle-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile-develop
581 lines (536 loc) · 20 KB
/
Jenkinsfile-develop
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#!/usr/bin/env groovy
import bcgov.GitHubHelper
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import java.util.regex.Pattern
// todo github status helper?
/*
* Sends a rocket chat notification
*/
def notifyRocketChat(text, target) {
def message = text.replaceAll(~/\'/, "")
def payload = JsonOutput.toJson([
"username":"Jenkins",
"icon_url":"https://wiki.jenkins.io/download/attachments/2916393/headshot.png",
"text": message
])
try {
def rocketChatURL;
if (target == 'deploy') {
rocketChatURL = ROCKET_DEPLOY_WEBHOOK
} else if (target == 'qa') {
rocketChatURL = ROCKET_QA_WEBHOOK
}
sh("curl -X POST -H 'Content-Type: application/json' --data \'${payload}\' ${rocketChatURL}")
} catch (error) {
echo "Error posting Rocket Chat message: ${error}"
}
}
// Create deployment status and pass to Jenkins-GitHub library
void createDeploymentStatus (String suffix, String status) {
try {
def ghDeploymentId = new GitHubHelper().createDeployment(
this,
"pull/${env.CHANGE_ID}/head",
[
'environment':"${suffix}",
'task':"deploy:pull:${env.CHANGE_ID}"
]
)
new GitHubHelper().createDeploymentStatus(
this,
ghDeploymentId,
"${status}",
['targetUrl':"https://eagle-${PR_NAME}-dev.${OCPHOST}/"]
)
if ('SUCCESS'.equalsIgnoreCase("${status}")) {
echo "${suffix} deployment successful!"
} else if ('PENDING'.equalsIgnoreCase("${status}")){
echo "${suffix} deployment pending."
}
} catch (error) {
echo "Unable to update Github deployment status: ${error}"
}
}
// Print stack trace of error
@NonCPS
private static String stackTraceAsString(Throwable t) {
StringWriter sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw));
return sw.toString()
}
def _openshift(String name, String project, Closure body) {
script {
openshift.withCluster() {
openshift.withProject(project) {
echo "Running Stage '${name}'"
waitUntil {
boolean isDone=false
try {
body()
isDone=true
echo "Completed Stage '${name}'"
} catch (error) {
echo "${stackTraceAsString(error)}"
def inputAction = input(
message: "This step (${name}) has failed. See related messages:",
ok: 'Confirm',
parameters: [
choice(
name: 'action',
choices: 'Re-run\nIgnore',
description: 'What would you like to do?'
)
]
)
if ('Ignore'.equalsIgnoreCase(inputAction)) {
notifyRocketChat(
"@all The build ${env.BUILD_DISPLAY_NAME} of eagle-public-pr, seems to be broken.\n ${env.RUN_DISPLAY_URL}\n Error: \n ${error.message}",
'deploy'
)
isDone=true
}
}
return isDone
}
}
}
}
}
/*
* 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 checkForPRDeployment() {
def dcCount = 999;
_openshift(env.STAGE_NAME, TOOLSPROJECT) {
try {
dcCount = openshift.selector('bc', "${PR_NAME}-eagle-public-build").count()
} catch (error) {
echo "${stackTraceAsString(error)}"
dcCount = 0
}
echo "count: ${dcCount}"
}
if (dcCount == 0) {
echo "no deployments, return false"
return false
} else {
return true
}
}
def createFullPRDeployment() {
openshift.withCluster() {
echo "Configuring deployment scripts"
try {
sh '''
if [ -d "eagle-helper-pods" ]
then
echo "stale helper pods checkout, removing it"
rm -rf eagle-helper-pods
fi
git clone https://github.com/bcgov/eagle-helper-pods
cd eagle-helper-pods/openshift/setup-teardown/
# set project to PR deployment template folder
find . -name "projectset.config" -exec sed -i -e "s/^\\( *TARGET_PROJECT_SET=*\\)[^ ]*\\(.*\\)*$/\\1${PROJECTSET}\\2/" {} \\;
# set unique name (ie. pr-branchName) for pr builds & deployments
cd params/CUSTOM_SETTINGS/${PROJECTSET}
find . -name "*.config" -exec sed -i -e "s/pr-placeholder/${PR_NAME}/g" {} \\;
# set public vars to pr fork
cd public
find . -name "*.config" -exec sed -i -e "s/branch-placeholder/${CHANGE_BRANCH}/g" {} \\;
find . -name "*.config" -exec sed -i -e "s/fork-placeholder/${PR_FORK}/g" {} \\;
find . -name "*.params" -exec sed -i -e "s/fork-placeholder/${PR_FORK}/g" {} \\;
find . -name "*.params" -exec sed -i -e "s/pr-placeholder/${PR_NAME}/g" {} \\;
find . -name "*.params" -exec sed -i -e "s/branch-placeholder/${CHANGE_BRANCH}/g" {} \\;
# set api, admin to bcgov/develop
cd ../api
find . -name "*.config" -exec sed -i -e "s/branch-placeholder/develop/g" {} \\;
find . -name "*.config" -exec sed -i -e "s/fork-placeholder/bcgov/g" {} \\;
find . -name "*.params" -exec sed -i -e "s/fork-placeholder/bcgov/g" {} \\;
find . -name "*.params" -exec sed -i -e "s/pr-placeholder/${PR_NAME}/g" {} \\;
find . -name "*.params" -exec sed -i -e "s/branch-placeholder/develop/g" {} \\;
cd ../admin
find . -name "*.config" -exec sed -i -e "s/branch-placeholder/develop/g" {} \\;
find . -name "*.config" -exec sed -i -e "s/fork-placeholder/bcgov/g" {} \\;
find . -name "*.params" -exec sed -i -e "s/fork-placeholder/bcgov/g" {} \\;
find . -name "*.params" -exec sed -i -e "s/pr-placeholder/${PR_NAME}/g" {} \\;
find . -name "*.params" -exec sed -i -e "s/branch-placeholder/develop/g" {} \\;
# create builds and deploys
cd ../../../..
./setup-all.sh
# switch back to tools project,
oc project ${TOOLSPROJECT}
'''
echo "deployment scripts configured and PR env created"
} catch (error) {
notifyRocketChat(
"@all The build ${env.BUILD_DISPLAY_NAME} of eagle-public dev-pr, seems to be broken.\n ${env.RUN_DISPLAY_URL}\n Error: \n ${error.message}",
'deploy'
)
currentBuild.result = 'FAILURE'
throw new Exception("Full PR env deployment failed")
}
}
}
def cleanUpPR() {
openshift.withCluster() {
echo "Configuring deployment scripts for teardown"
try {
sh '''
if [ -d "eagle-helper-pods" ]
then
echo "stale helper pods checkout, removing it"
rm -rf eagle-helper-pods
fi
git clone https://github.com/bcgov/eagle-helper-pods
cd eagle-helper-pods/openshift/setup-teardown/
# set project to PR deployment template folder
find . -name "projectset.config" -exec sed -i -e "s/^\\( *TARGET_PROJECT_SET=*\\)[^ ]*\\(.*\\)*$/\\1${PROJECTSET}\\2/" {} \\;
# set to unique name (ie. branchName-appName) for pr builds & deployments
cd params/CUSTOM_SETTINGS/${PROJECTSET}
find . -name "*.params" -exec sed -i -e "s/pr-placeholder/${PR_NAME}/g" {} \\;
find . -name "*.config" -exec sed -i -e "s/pr-placeholder/${PR_NAME}/g" {} \\;
cd ../../../
./teardown-all.sh
# switch back to tools project,
oc project ${TOOLSPROJECT}
'''
echo "Template scripts configured and PR env removed"
} catch (error) {
notifyRocketChat(
"@all The cleanup ${env.BUILD_DISPLAY_NAME} of eagle ${PR_NAME} deployment may have failed.\n ${env.RUN_DISPLAY_URL}\n Error: \n ${error.message}",
'deploy'
)
throw new Exception("Full PR env teardown failed: ${error}")
}
}
}
def cleanUpLock(lockName) {
def manager = org.jenkins.plugins.lockableresources.LockableResourcesManager.get()
def resources = manager.getResources().findAll{
!it.locked && it.name.equalsIgnoreCase(lockName)
}
resources.each{
manager.getResources().remove(it)
echo "Removing lock: ${lockName}"
}
manager.save()
}
def CHANGELOG = "No new changes"
def IMAGE_HASH = "latest"
def BUILD_DONE_STATUSES = ['Complete', 'Failed', 'Cancelled']
def lockName = "eagle-public-${env.JOB_NAME}-${env.BUILD_NUMBER}"
pipeline {
environment {
TOOLSPROJECT = "${TOOLS_NAMESPACE ?: '6cdc9e-tools'}"
DEVPROJECT = "${DEV_NAMESPACE ?: '6cdc9e-dev'}"
PROJECTSET = "${PR_SET ?: 'OCP4_PR'}"
OCPHOST = "${OCP_HOST ?: 'apps.silver.devops.gov.bc.ca'}"
PR_NAME = "${env.CHANGE_BRANCH}".toLowerCase()
PR_FORK = "${env.CHANGE_FORK}"
}
agent any
stages {
stage('Build Init') {
when {
expression {
env.PR_NAME != 'develop' || env.PR_NAME != 'test'
}
}
steps {
script {
openshift.setLockName(lockName)
// setup vars for keycloak client access
try{
sh("oc extract secret/pr-kc-secret --to=${env.WORKSPACE} --confirm")
KC_CLIENT_ID = sh(returnStdout: true, script: 'cat CLIENT_SSO_CLIENTID')
KC_CLIENT_SECRET = sh(returnStdout: true, script: 'cat CLIENT_SSO_SECRET')
CLIENT_PUBLIC = sh(returnStdout: true, script: 'cat CLIENT_PUBLIC')
KC_REALM_ID = sh(returnStdout: true, script: 'cat KC_REALM')
SSO_HOST = sh(returnStdout: true, script: 'cat KC_HOST')
APP_HOST="eagle-${PR_NAME}-dev.${OCPHOST}"
} catch (error) {
echo "Error retrieving keycloak parameters, message: ${error}"
}
sh "npm i axios@^0.19.2 lodash@^4.17.19"
}
}
}
stage("Add URI to KC"){
when {
expression {
env.PR_NAME != 'develop' || env.PR_NAME != 'test'
}
}
steps {
script {
try {
sh '''
curl -L -O https://raw.githubusercontent.com/bcgov/eagle-helper-pods/master/openshift/jenkins/scripts/keycloak.js
curl -L -O https://raw.githubusercontent.com/bcgov/eagle-helper-pods/master/openshift/jenkins/scripts/addURI.js
curl -L -O https://raw.githubusercontent.com/bcgov/eagle-helper-pods/master/openshift/jenkins/scripts/removeURI.js
chmod +x addURI.js removeURI.js
'''
} catch (error) {
echo "Error retrieving keycloak util: ${error}"
}
//set env vars for stage
env.KC_CLIENT_ID="${KC_CLIENT_ID}"
env.KC_CLIENT_SECRET="${KC_CLIENT_SECRET}"
env.APP_CLIENT_ID="${CLIENT_PUBLIC}"
env.KC_REALM_ID="${KC_REALM_ID}"
env.SSO_HOST="${SSO_HOST}"
env.APP_HOST="${APP_HOST}"
sh "node addURI.js"
}
}
}
stage('Setup PR env') {
when {
expression {
env.PR_NAME != 'develop' || env.PR_NAME != 'test'
}
}
steps {
script {
try {
sh("oc extract secret/rocket-chat-secrets --to=${env.WORKSPACE} --confirm")
} catch (error) {
echo "Error retrieving Rocket Chat tokens: ${error}"
}
createDeploymentStatus('dev', "PENDING")
echo "Branch to build: ${PR_NAME}"
echo "Source fork: ${PR_FORK}"
if (!checkForPRDeployment()) {
// env hasn't been created, call setup scripts
echo "Deploying PR Environment"
createFullPRDeployment()
} else {
echo "Deployment for this PR already exists, skipping deploy all"
// modify bc to point at proper fork-branch, then continue to build step
_openshift(env.STAGE_NAME, TOOLSPROJECT) {
openshift.patch(
"bc/${PR_NAME}-eagle-public-angular-builder",
'\'{"spec":{"source":{"git":{"ref": "${CHANGE_BRANCH}", "uri": "https://github.com/${PR_FORK}/eagle-public"}}}}\''
)
}
}
}
}
}
stage('Build') {
when {
expression {
env.PR_NAME != 'develop' || env.PR_NAME != 'test'
}
}
agent any
steps {
milestone 1
script {
pastBuilds = []
buildsSinceLastSuccess(pastBuilds, currentBuild);
CHANGELOG = getChangeLog(pastBuilds);
echo ">>>>>>Changelog: \n ${CHANGELOG}"
_openshift(env.STAGE_NAME, TOOLSPROJECT) {
timeout(20) {
try {
sh("oc extract secret/rocket-chat-secrets --to=${env.WORKSPACE} --confirm")
ROCKET_DEPLOY_WEBHOOK = sh(returnStdout: true, script: 'cat rocket-deploy-webhook')
ROCKET_QA_WEBHOOK = sh(returnStdout: true, script: 'cat rocket-qa-webhook')
} catch (error) {
echo "Error retrieving Rocket Chat token"
}
echo "Building eagle-public ${PR_NAME} branch"
// trigger and wait for s2i build to complete
def bcObj1 = openshift.selector('bc', "${PR_NAME}-eagle-public-angular-builder")
bcObj1.startBuild()
def buildName1 = "${PR_NAME}-eagle-public-angular-builder-${bcObj1.object().status.lastVersion}"
echo "Angular build name: ${buildName1}"
def buildSelector1 = openshift.selector('build', buildName1)
buildSelector1.untilEach(1) {
def phase = it.object().status.phase
if (phase == 'Failed') {
currentBuild.result = "FAILURE"
}
return ( BUILD_DONE_STATUSES.contains(phase) )
}
// trigger and wait for nginx image build to complete
def bcObj2 = openshift.selector('bc',"${PR_NAME}-eagle-public-build" )
bcObj2.startBuild()
def buildName2 = "${PR_NAME}-eagle-public-build-${bcObj2.object().status.lastVersion}"
echo "Ngix build name: ${buildName2}"
def buildSelector2 = openshift.selector('build', buildName2)
buildSelector2.untilEach(1) {
def phase = it.object().status.phase
if (phase == 'Failed') {
currentBuild.result = "FAILURE"
}
return ( BUILD_DONE_STATUSES.contains(phase) )
}
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 ${PR_NAME}-eagle-public:latest -o template --template=\"{{.image.dockerImageReference}}\"|awk -F \":\" \'{print \$3}\'""",
returnStdout: true).trim()
echo ">> IMAGE_HASH: ${IMAGE_HASH}"
}
}
}
}
}
stage('Deploy'){
when {
expression {
env.PR_NAME != 'develop' || env.PR_NAME != 'test'
}
}
agent any
steps {
script {
try {
openshift.withCluster() {
openshift.withProject(TOOLSPROJECT) {
echo "Tagging image, latest hash: ${IMAGE_HASH}"
openshift.tag("${TOOLSPROJECT}/${PR_NAME}-eagle-public:latest", "${TOOLSPROJECT}/${PR_NAME}-eagle-public:${PR_NAME}")
echo "Tagged ${TOOLSPROJECT}/${PR_NAME}-eagle-public:latest as ${TOOLSPROJECT}/${PR_NAME}-eagle-public:${PR_NAME}"
}
openshift.withProject(DEVPROJECT) {
echo "Deploying to dev env..."
def dcObj = openshift.selector('dc', "${PR_NAME}-eagle-public")
dcObj.rollout().status()
echo ">>>> Deployment Complete"
createDeploymentStatus('dev', "SUCCESS")
notifyRocketChat(
"A new version of eagle-public ${PR_NAME} is now in Dev, build: ${env.BUILD_DISPLAY_NAME} \n https://eagle-${PR_NAME}-dev.${OCPHOST}/ \nChanges: \n ${CHANGELOG}",
'deploy'
)
notifyRocketChat(
"@all A new version of eagle-public is now in Dev and ready for QA. \n https://eagle-${PR_NAME}-dev.${OCPHOST}/ \nChanges to Dev: \n ${CHANGELOG}",
'qa'
)
}
}
} catch (error) {
notifyRocketChat(
"@all The build ${env.BUILD_DISPLAY_NAME} of eagle-public, seems to be broken.\n ${env.RUN_DISPLAY_URL}\n Error: \n ${error.message}",
'deploy'
)
currentBuild.result = "FAILURE"
throw new Exception("Deploy failed: ${error}")
}
}
}
}
stage("Populate Data"){
when {
expression {
env.PR_NAME != 'develop' || env.PR_NAME != 'test'
}
}
steps {
script {
try {
openshift.withCluster() {
openshift.withProject(DEVPROJECT) {
echo "Running data generators"
/* Get the Deployment Config (DC) Object */
def dcObj = openshift.selector("dc", "${PR_NAME}-eagle-api").object()
/* Use the DC to find the pods */
def podSelector = openshift.selector("pod", [deployment: "${PR_NAME}-eagle-api-${dcObj.status.latestVersion}"])
podSelector.withEach {
// obtain the pod name and remove "pods/" from the front.
def podName = it.name()
podName = podName.replaceFirst("pod/", "")
// todo add pod ready check
// Run a command in the container
openshift.exec("-n", "${DEVPROJECT}", podName, "scl", "enable", "rh-nodejs10", '"/opt/app-root/src/generate.sh 20 Static Saved Single"')
}
}
}
} catch (error) {
notifyRocketChat(
"@all Data generation failed for the build ${env.BUILD_DISPLAY_NAME} of eagle-api.\n ${env.RUN_DISPLAY_URL}\n Error: \n ${error.message}",
'deploy'
)
currentBuild.result = "FAILURE"
throw new Exception("Data generation failed")
}
}
}
}
stage('Cleanup') {
when {
expression {
env.PR_NAME != 'develop' || env.PR_NAME != 'test'
}
}
steps {
milestone 2
script {
// set kc client env vars for stage
env.KC_CLIENT_ID="${KC_CLIENT_ID}"
env.KC_CLIENT_SECRET="${KC_CLIENT_SECRET}"
env.APP_CLIENT_ID="${CLIENT_PUBLIC}"
env.KC_REALM_ID="${KC_REALM_ID}"
env.SSO_HOST="${SSO_HOST}"
env.APP_HOST="${APP_HOST}"
echo "Removing pipeline lock and cleaning up deployment"
def inputAction = input(
message: "Are you ready to cleanup ${PR_NAME} deployment?",
ok: 'Confirm',
parameters: [
choice(
name: 'action',
choices: 'Clean-up\nIgnore',
description: 'What would you like to do?'
)
]
)
if ('Clean-up'.equalsIgnoreCase(inputAction)) {
sh "node removeURI.js"
cleanUpLock(lockName)
cleanUpPR()
echo "Cleanup of ${PR_NAME} deployment is complete"
}
}
}
}
}
post {
success {
echo 'Build and deploy complete'
}
failure {
echo 'Something went wrong, check deploy and builds'
}
}
}