-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile-Integration
486 lines (404 loc) · 16.7 KB
/
Jenkinsfile-Integration
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
/**
* Helper function for looping over Map object
*
*/
@NonCPS
def mapToList(depmap) {
def dlist = []
for (def entry in depmap) {
dlist.add(new java.util.AbstractMap.SimpleImmutableEntry(entry.key, entry.value))
}
dlist
}
/**
* Functions to validate image
*
*/
def helmLint(Map args) {
// lint helm chart
sh "/usr/local/bin/helm lint ${args.chart_dir} --set build=${args.commit_id},image.tag=${args.tag},image.app.repository=${args.appRepo},image.proxy.repository=${args.proxyRepo},image.app.tag=${args.tag},image.proxy.tag=${args.tag},version=${args.version},config.directory=config/${args.namespace},logging.env.cloud_id=${ELASTIC_ID},logging.env.username=${ELASTIC_USER},logging.env.password=${ELASTIC_PASSWORD},ingress.type=${INGRESS_VERSION},context=${args.context},ingress.entry=${args.entry},ingress.external=${args.external}"
}
/**
* Functions to deploy image
*
*/
def helmDeploy(Map args) {
try {
// Configure helm client and confirm tiller process is installed
if (args.dry_run) {
println "Running dry-run deployment"
sh "/usr/local/bin/helm install --dry-run --debug ${args.name} ${args.chart_dir} --set build=${args.commit_id},image.tag=${args.tag},image.app.repository=${args.appRepo},image.proxy.repository=${args.proxyRepo},image.app.tag=${args.tag},image.proxy.tag=${args.tag},version=${args.version},config.directory=config/${args.namespace},logging.env.cloud_id=${ELASTIC_ID},logging.env.username=${ELASTIC_USER},logging.env.password=${ELASTIC_PASSWORD},ingress.type=${INGRESS_VERSION},context=${args.context},ingress.entry=${args.entry},ingress.external=${args.external} --namespace=${args.namespace}"
} else {
println "Running deployment"
sh "/usr/local/bin/helm upgrade --install ${args.name} ${args.chart_dir} --set build=${args.commit_id},image.tag=${args.tag},image.app.repository=${args.appRepo},image.proxy.repository=${args.proxyRepo},image.app.tag=${args.tag},image.proxy.tag=${args.tag},version=${args.version},config.directory=config/${args.namespace},logging.env.cloud_id=${ELASTIC_ID},logging.env.username=${ELASTIC_USER},logging.env.password=${ELASTIC_PASSWORD},ingress.type=${INGRESS_VERSION},context=${args.context},ingress.entry=${args.entry},ingress.external=${args.external} --namespace=${args.namespace}"
echo "Application ${args.name} successfully deployed. Use helm status ${args.name} to check."
}
notifyBuild('SUCCESS', null, null, "Image deployed to `${args.namespace}` Kubernetes successfully.")
}
catch(exception) {
echo "${exception}"
// Slack notification failure.
notifyBuild('FAILURE', null, null, "Error in pushing image to `${args.namespace}` Kubernetes - Retrying.", args.notify, args.slack_channel)
println "Error on Upgrade / Install"
}
}
/**
* Track Git logs
*
*/
def showChangeLogs() {
def authors = []
def changeLogSets = currentBuild.rawBuild.changeSets
for (int i = 0; i < changeLogSets.size(); i++) {
def entries = changeLogSets[i].items
for (int j = 0; j < entries.length; j++) {
def author = "${entries[j].author}"
def email = entries[j].author.getProperty(hudson.tasks.Mailer.UserProperty.class).getAddress()
authors.push("${author}:${email}")
}
}
return authors.unique();
}
/**
* TODO: Track Container Deployment Status
*
*/
def getDeploymentStatus(commitId, environment) {
sh "sleep 10s"
def status
def sleep = ['ContainerCreating', 'Pending', 'Succeeded']
def success = ['Running']
def failure = ['CrashLoopBackOff', 'Terminating', 'Error']
sh "kubectl get pods -l build=${commitId} --namespace=${environment} --sort-by=.status.startTime"
def deploymentVerify = sh (script: "kubectl get pods -l build=${commitId} --namespace=${environment} --sort-by=.status.startTime | awk 'NR==2{print \$3}'", returnStdout: true).trim()
def containerCount = sh (script: "kubectl get pods -l build=${commitId} --namespace=${environment} --sort-by=.status.startTime | awk 'NR==2{print \$4}'", returnStdout: true).trim().toInteger()
echo "Deployment Status: ${deploymentVerify}"
echo "Container Restart Count: ${containerCount}"
if (sleep.contains(deploymentVerify)) {
return getDeploymentStatus(commitId, environment)
}
if (success.contains(deploymentVerify)) {
if (containerCount == 0) {
return true
} else {
return false
}
}
if (failure.contains(deploymentVerify)) {
return false
}
}
def assertUniqueTag(Map args) {
withAWS(credentials: args.credentials) {
try {
sh "if echo \$(aws ecr describe-images --region ${env.AWS_REGION} --repository-name ${args.repo} --filter tagStatus=TAGGED --query 'imageDetails[*].imageTags[*]' --output text) | grep --line-regexp '.*[[:space:]]${args.tag}[[:space:]].*'; then exit 1; fi"
}
catch (exception) {
sh "echo ${exception}"
error "Tag ${args.tag} already found in repo ${args.repo}. Will not overwrite..."
}
}
}
/**
* Jenkins pipeline stages.
*
* A list of stages available in this pipeline Jenkins file will be executed
* sequentially. The name of this file should be matching with Pipeline block of
* Jenkins configuration.
*
*/
node {
/**
* Version details for tagging the image, refer build-scripts/versioning-image
*
* @final string tag
* Push image to Integration.
* @final string pullRepoTag
* Pull image from Integration.
*
*/
def tag
def pullRepoTag
/**
* Define script variables.
*
*/
def notify
def slackChannel = "#general"
// Docker image
def app
def proxy
// Config.json file
def config
// Component specific variables builds
def component = "insights-ui"
def cluster = "delivery"
def environment = "integration"
def chartDirectory
// Config File from S3
def configFile
// Git Credentials
def gitCredentials = "impinger-cicd-github"
// Helm Repository
def helmRepo = "[email protected]:InterMx/intermx-cicd-helm.git"
// Helm Branch
def helmBranch = "delivery"
// S3 Config Bucket
def configBucket = "${AWS_BUCKET_NAME}"
// AWS Credentials
def awsCredentials = "intermx-impinger-credentials"
// ECR Repository for application
def appEcrRepoName = "intermx-insights-ui"
// ECR Repository for proxy
def proxyEcrRepoName = "intermx-insights-proxy"
// ECR Repository
def ecrRepo = "${AWS_ACCOUNT_ID}.dkr.ecr.us-east-1.amazonaws.com"
// ECR FQDN
def ecrAppRepo = "${AWS_ACCOUNT_ID}.dkr.ecr.us-east-1.amazonaws.com/${appEcrRepoName}"
def ecrProxyRepo = "${AWS_ACCOUNT_ID}.dkr.ecr.us-east-1.amazonaws.com/${proxyEcrRepoName}"
// ECR Repository Endpoint
def ecrEndpoint = "https://${AWS_ACCOUNT_ID}.dkr.ecr.us-east-1.amazonaws.com"
// ECR Credentials
def ecrCredentials = "ecr:us-east-1:${awsCredentials}"
// Host to listen to for routing
def endpointHost = "gisdev.geopath.io"
// Context name to avoid collisions
def context = "ui"
// Helm deployment name to avoid collisions
def deploymentName = "intermx-insights-ui"
// Entry point for Traefik
def entryPoint = "https"
// Default externality for Traefik
def external = "true"
// Git commit message
def commitMessage
def commitId
def deployment
try {
/**
* Cloning the repository to workspace to build image.
*
*/
stage ('Clone Repository') {
checkout scm
def (origin, branch) = scm.branches[0].name.tokenize('/')
commitMessage = sh (script: 'git log --oneline -1 ${GIT_COMMIT}', returnStdout: true).trim().substring(8)
commitId = sh (script: 'git log -1 --format=%H', returnStdout: true).trim() // %h, for short hash
notify = showChangeLogs()
// Slack notification in progress.
notifyBuild('STARTED', branch, commitMessage)
}
/**
* Get dependencies for Kubernetes Deploy (Integration).
*
*/
stage('Get Dependencies for Integration') {
// Get helm charts from git repo
dir('helm') {
git url: helmRepo, credentialsId: gitCredentials, branch: helmBranch
}
// Download config.json from S3 into the insights-ui/config helm repo
final s3ConfigPath = "${cluster}/${environment}/${component}"
chartDirectory = "helm/${component}"
configFile = "${chartDirectory}/config/${environment}/config.json"
dir("${chartDirectory}/config/${environment}") {
withAWS(credentials:awsCredentials) {
s3Download(file: 'config.json', bucket: configBucket, path: "${s3ConfigPath}/config.json", force: true)
s3Download(file: 'filebeat.yaml', bucket: configBucket, path: "${s3ConfigPath}/filebeat.yaml", force: true)
s3Download(file: 'ilm_policy.json', bucket: configBucket, path: "${s3ConfigPath}/ilm_policy.json", force: true)
}
}
}
/**
* Environment variables for Kubernetes.
*
*/
stage('Build Kubernetes Env Variables for Integration From Config') {
config = readJSON file: "${configFile}"
for (entry in mapToList(config)) {
dir("${chartDirectory}/config/${environment}"){
writeFile file: "${entry.key}", text: "${entry.value}"
}
}
}
/**
* Version Automation.
*
*/
stage('Update Version') {
def json = readJSON file: "package.json"
def image_version = json.devops['image-version']
tag = "v${image_version}_integration"
pullRepoTag = "v${image_version}_development"
}
/**
* Add Required Tools to Run NodeJS.
*
*/
stage('Add NodeJS Tool') {
env.NODEJS_HOME = "${tool 'node'}"
env.PATH = "${env.NODEJS_HOME}/bin:${env.PATH}"
sh "mv common.package.json package.json && mv common.package-lock.json package-lock.json"
sh "npm install"
}
/**
* Running BrowserStack Test.
*
*/
stage('BrowserStack Test') {
// Disabled browserStack as a temp fix, the keys are expired.
// sh "./BrowserStackLocal --key ${BROWSERSTACK_KEY} --daemon start"
// sh "./node_modules/.bin/protractor browser.test.js"
}
/**
* Set Unique Tag.
*
*/
stage ('Assert Unique Tag') {
assertUniqueTag(
credentials : awsCredentials,
repo : appEcrRepoName,
tag : tag
)
assertUniqueTag(
credentials : awsCredentials,
repo : proxyEcrRepoName,
tag : tag
)
}
/**
* Pull the image from Integration, chnage tag and push the same to AWS ECR Integration repo.
*
*/
stage('Pull and Push the Image to ECR Repo') {
docker.withRegistry("${ecrEndpoint}", "${ecrCredentials}") {
docker.image("${appEcrRepoName}:${pullRepoTag}").pull()
docker.image("${proxyEcrRepoName}:${pullRepoTag}").pull()
docker.image("${ecrRepo}/${appEcrRepoName}:${pullRepoTag}").push("${tag}")
docker.image("${ecrRepo}/${proxyEcrRepoName}:${pullRepoTag}").push("${tag}")
}
}
/**
* Deploy Image to Kubernetes using Helm.
*
*/
stage('Deploy image to Integration Environment') {
entryPoint = "https"
external = "true"
// Run helm chart linter
helmLint(
chart_dir : chartDirectory,
chart_version : environment,
appRepo : ecrAppRepo,
proxyRepo : ecrProxyRepo,
tag : tag,
name : deploymentName,
version : environment,
namespace : environment,
context : context,
entry : entryPoint,
external : external,
commit_id : commitId
)
// Deploy using Helm chart
helmDeploy(
dry_run : false,
name : deploymentName,
appRepo : ecrAppRepo,
proxyRepo : ecrProxyRepo,
chart_dir : chartDirectory,
tag : tag,
version : environment,
namespace : environment,
notify : notify,
slack_channel : slackChannel,
context : context,
entry : entryPoint,
external : external,
git_cred : gitCredentials,
commit_id : commitId
)
}
/**
* Track Container Status for Integration
*
*/
stage('Track Container Status for Integration') {
// deployment = getDeploymentStatus(commitId, environment);
// Slack notification success.
/* if (deployment) {
notifyBuild('SUCCESS', null, null, "Image deployed to `${environment}` Kubernetes successfully.")
} else {
notifyBuild('FAILURE', null, null, "Error in creating container, please check the code for ${environment}", notify, slackChannel)
error ("Error in creating container, please check the code for ${environment}")
} */
// notifyBuild('SUCCESS', null, null, "Image deployed to `${environment}` Kubernetes successfully.")
}
}
catch(exception) {
echo "${exception}"
// Slack notification failure.
notifyBuild('FAILURE', null, null, 'Exception found in Jenkins.', notify, slackChannel)
}
finally {
// Clean up the workspace after finish the job.
deleteDir()
}
}
/**
* Sending notifications to Slack channel.
*
*/
def notifyBuild(buildStatus = 'FAILURE', branch = null, commitMessage = null, message = null, notify = null, channel = null) {
// Default values
def summary
def alert = false
def org = "${ORGANIZATION}"
def baseURL = "${SLACK_BASE_URL}"
org = org.toUpperCase()
if (buildStatus == 'STARTED') {
color = 'YELLOW'
colorCode = '#FFFF00'
summary = "${buildStatus}: ${org} - Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' is currently working on branch '${branch}' with a message: '${commitMessage}'"
} else if (buildStatus == 'SUCCESS') {
color = 'GREEN'
colorCode = '#00FF00'
summary = "${buildStatus}: ${org} - Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' | Message: ${message}"
} else {
color = 'RED'
colorCode = '#FF0000'
summary = "${buildStatus}: ${org} - Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' | Error: ${message}"
alert = true
}
// Send notifications to Slack.
slackSend (baseUrl: baseURL, color: colorCode, message: summary)
if (alert) {
for(int i = 0; i < notify.size(); i++) {
def notice = notify[i].split(":")
def author = notice[0]
def email = notice[1]
def domain = email.split("@")[1]
if (domain && domain == 'intermx.com') {
emailext (
to: "${email}",
subject: "${buildStatus}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
body: """
<html>
<body>
<p>Hi ${author},</p>
<br>
<p>The build job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' failed to complete.</p>
<p>The following error was reported:</p>
<br>
<p>${message}</p>
<br>
<p>Please review the error at <a href=\"${env.BUILD_URL}console\">${env.BUILD_URL}console</a> and resolve the issue causing the failure so that releases can continue.</p>
</body>
</html>
"""
)
}
}
slackSend (baseUrl: baseURL, channel: "${channel}", color: colorCode, message: summary)
}
}