This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Jenkinsfile
617 lines (526 loc) · 26.3 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
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#!groovy
import java.text.SimpleDateFormat;
@NonCPS
def getChangeDetail() {
def changeDetail = ""
def changes = currentBuild.changeSets
for (int i = 0; i < changes.size(); i++) {
def entries = changes[i].items
for (int j = 0; j < entries.length; j++) {
def entry = entries[j]
Date entry_timestamp = new Date(entry.timestamp)
SimpleDateFormat timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
changeDetail += "${entry.commitId.take(7)} by [${entry.author}] on ${timestamp.format(entry_timestamp)}: ${entry.msg}\n\n"
}
}
if (!changeDetail) {
changeDetail = "No new changes"
}
return changeDetail
}
IS_MASTER_BRANCH = env.BRANCH_NAME == "master"
IS_RELEASE_BRANCH = (env.BRANCH_NAME ==~ /\d+\.\d+\.\d+/)
def sendEmailNotification() {
if (!(IS_MASTER_BRANCH || IS_RELEASE_BRANCH)) {
println "Skipping email step since branch condition was not met"
return
}
final def EXTRECIPIENTS = emailextrecipients([
[$class: 'CulpritsRecipientProvider'],
[$class: 'RequesterRecipientProvider'],
[$class: 'DevelopersRecipientProvider']
])
def RECIPIENTS = EXTRECIPIENTS != null ? EXTRECIPIENTS : env.CHANGE_AUTHOR_EMAIL;
def SUBJECT = "${env.JOB_NAME} - Build #${env.BUILD_NUMBER} - ${currentBuild.currentResult}!"
def CHANGE = getChangeDetail();
emailext(
to: RECIPIENTS,
subject: "${SUBJECT}",
body: """
<b>Build result:</b>
<br><br>
${currentBuild.currentResult}
<br><br>
<b>Project name - Build number:</b>
<br><br>
${currentBuild.fullProjectName} - Build #${env.BUILD_NUMBER}
<br><br>
<b>Check console output for more detail:</b>
<br><br>
<a href="${currentBuild.absoluteUrl}console">${currentBuild.absoluteUrl}console</a>
<br><br>
<b>Changes:</b>
<br><br>
${CHANGE}
<br><br>
"""
);
}
pipeline {
agent {
label "docker-build"
}
triggers {
issueCommentTrigger('trigger_build')
upstream(upstreamProjects: "Codewind/codewind-odo-extension/${env.BRANCH_NAME},Codewind/codewind-appsody-extension/${env.BRANCH_NAME}", threshold: hudson.model.Result.SUCCESS)
}
options {
quietPeriod(300)
timestamps()
skipStagesAfterUnstable()
}
environment {
CODECOV_TOKEN = credentials('codecov-token-codewind-repository')
INSECURE_KEYRING = 'true'
}
stages {
stage('Run Portal eslint and unit tests') {
options {
timeout(time: 30, unit: 'MINUTES')
}
steps {
withEnv(["PATH=$PATH:~/.local/bin;NOBUILD=true"]) {
sh '''#!/usr/bin/env bash
DIR=`pwd`;
echo "Starting unit tests for Portal..."
export PATH=$PATH:/home/jenkins/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node_js/bin/
# Install nvm to easily set version of node to use
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
export NVM_DIR="$HOME/.nvm"
. $NVM_DIR/nvm.sh
nvm i 10
# Run eslint on portal code
cd $DIR/src/pfe/portal
npm install
if [ $? -ne 0 ]; then
exit 1
fi
npm run eslint
if [ $? -ne 0 ]; then
exit 1
fi
# Run eslint on portal tests
cd $DIR/test
npm install
if [ $? -ne 0 ]; then
exit 1
fi
npm run eslint
if [ $? -ne 0 ]; then
exit 1
fi
# Run the unit test suite
echo "Portal unit tests"
npm run unittest
if [ $? -eq 0 ]; then
echo "+++ PORTAL UNIT TESTS COMPLETED SUCCESSFULLY +++";
else
echo "+++ PORTAL UNIT TESTS FAILED +++";
exit 1;
fi
'''
}
}
}
stage('Run Turbine unit test suite') {
options {
timeout(time: 30, unit: 'MINUTES')
}
steps {
withEnv(["PATH=$PATH:~/.local/bin;NOBUILD=true"]) {
withDockerRegistry([url: 'https://index.docker.io/v1/', credentialsId: 'docker.com-bot']) {
sh '''#!/usr/bin/env bash
DIR=`pwd`;
echo "Starting unit tests for Turbine..."
export PATH=$PATH:/home/jenkins/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node_js/bin/
ARCH=`uname -m`;
printf "\n\n${MAGENTA}Platform: $ARCH ${RESET}\n"
# Install nvm to easily set version of node to use
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
export NVM_DIR="$HOME/.nvm"
. $NVM_DIR/nvm.sh
nvm i 10
# Run eslint on turbine code
cd $DIR/src/pfe/file-watcher/server
npm install
if [ $? -ne 0 ]; then
exit 1
fi
# Run the unit test suite
echo "Started running Turbine Unit Test Suite"
npm run unit:test
if [ $? -eq 0 ]; then
echo "+++ TURBINE UNIT TESTS COMPLETED SUCCESSFULLY +++";
else
echo "+++ TURBINE UNIT TESTS FAILED +++";
exit 1;
fi
'''
}
}
}
}
stage('Build Docker images') {
steps {
withDockerRegistry([url: 'https://index.docker.io/v1/', credentialsId: 'docker.com-bot']) {
// NOTE: change of this sh call should be in sync with './script/build.sh'.
sh '''#!/usr/bin/env bash
# Docker system prune
echo "Docker system prune ..."
docker system df
docker system prune -a --volumes -f
docker builder prune -a -f
docker system df
df -lh
echo "Starting build for Eclipse Codewind ..."
DIR=`pwd`;
SRC_DIR=$DIR/src;
PFE=pfe
PERFORMANCE=performance;
KEYCLOAK=keycloak;
GATEKEEPER=gatekeeper;
ARCH=`uname -m`;
TAG=latest;
REGISTRY=eclipse
# On intel, uname -m returns "x86_64", but the convention for our docker images is "amd64"
if [ "$ARCH" == "x86_64" ]; then
IMAGE_ARCH="amd64"
else
IMAGE_ARCH=$ARCH
fi
ALL_IMAGES="$PFE $PERFORMANCE $KEYCLOAK $GATEKEEPER";
# Copy .env over to file-watcher
if [ -f $DIR/.env ]; then
echo -e "\nCopying $DIR/.env to ${SRC_DIR}/${PFE}/file-watcher/scripts/.env\n"
cp $DIR/.env ${SRC_DIR}/${PFE}/file-watcher/scripts/.env
fi
# Copy the license files to the portal, performance, keycloak and gatekeeper
cp -r $DIR/LICENSE ${SRC_DIR}/pfe/portal/
cp -r $DIR/NOTICE.md ${SRC_DIR}/pfe/portal/
cp -r $DIR/LICENSE ${SRC_DIR}/performance/
cp -r $DIR/NOTICE.md ${SRC_DIR}/performance/
cp -r $DIR/LICENSE ${SRC_DIR}/keycloak/
cp -r $DIR/NOTICE.md ${SRC_DIR}/keycloak/
cp -r $DIR/LICENSE ${SRC_DIR}/gatekeeper/
cp -r $DIR/NOTICE.md ${SRC_DIR}/gatekeeper/
# Copy the docs into portal
cp -r $DIR/docs ${SRC_DIR}/pfe/portal/
echo -e "\n+++ DOWNLOADING EXTENSIONS +++\n";
# If CHANGE_TARGET is set then this is a PR so use the target branch (usually master)
VERSION=""
BRANCH=""
if [ -z "$CHANGE_TARGET" ]; then
BRANCH=$GIT_BRANCH
if [ $GIT_BRANCH == "master" ]; then
VERSION="9.9.9999"
else
VERSION="$GIT_BRANCH"
fi
else
BRANCH=$CHANGE_TARGET
if [ $CHANGE_TARGET == "master" ]; then
VERSION="9.9.9999"
else
VERSION="$CHANGE_TARGET"
fi
fi
echo "extension version to be downloaded: $VERSION"
echo "extension branch to be used: $BRANCH"
mkdir -p ${SRC_DIR}/pfe/extensions
rm -f ${SRC_DIR}/pfe/extensions/codewind-appsody-extension-*.zip
rm -f ${SRC_DIR}/pfe/extensions/codewind-odo-extension-*.zip
rm -f ${SRC_DIR}/pfe/extensions/codewind-odo-devfile-extension-*.zip
curl -Lfo ${SRC_DIR}/pfe/extensions/codewind-appsody-extension-$VERSION.zip http://archive.eclipse.org/codewind/codewind-appsody-extension/$BRANCH/latest/codewind-appsody-extension-$VERSION.zip
if [ $? -ne 0 ]; then
echo "Error downloading appsody extension"
exit 1
fi
curl -Lfo ${SRC_DIR}/pfe/extensions/codewind-odo-extension-$VERSION.zip http://archive.eclipse.org/codewind/codewind-odo-extension/$BRANCH/latest/codewind-odo-extension-$VERSION.zip
if [ $? -ne 0 ]; then
echo "Error downloading odo extension"
exit 1
fi
ODO_DEVFILE_BRANCH="$BRANCH-devfile"
ODO_DEVFILE_VERSION=$ODO_DEVFILE_BRANCH
if [ $BRANCH == "master" ]; then
ODO_DEVFILE_VERSION="devfile-9.9.9999"
fi
curl -Lfo ${SRC_DIR}/pfe/extensions/codewind-odo-extension-devfile-$VERSION.zip http://archive.eclipse.org/codewind/codewind-odo-extension/$ODO_DEVFILE_BRANCH/latest/codewind-odo-extension-$ODO_DEVFILE_VERSION.zip
if [ $? -ne 0 ]; then
echo "Error downloading odo-devfile extension"
exit 1
fi
# BUILD IMAGES
# Uses a build file in each of the directories that we want to use
echo -e "\n+++ BUILDING DOCKER IMAGES +++\n";
for image in $ALL_IMAGES
do
export IMAGE_NAME=codewind-$image-$IMAGE_ARCH
echo Building image $IMAGE_NAME;
cd ${SRC_DIR}/${image};
time sh build Dockerfile_${ARCH};
if [ $? -eq 0 ]; then
echo "+++ SUCCESSFULLY BUILT $IMAGE_NAME +++";
echo -e "\n+++ RETAGGING $IMAGE_NAME TO HAVE TAG:':test' FOR TEST ONLY MODIFICATIONS +++\n";
# Allows us to modify the Docker images for test only modifications
# Such as telling PFE to run with Code Coverage generation enabled
# The original image (latest) will be the one that is pushed to Dockerhub
if [ "$image" = "$PFE" ]; then
# Now overwrite the PFE image ':test' tag with new changes
echo -e "FROM eclipse/codewind-pfe-amd64:latest\nENV ENABLE_CODE_COVERAGE=true" >> pfe-test-dockerfile
docker build -t eclipse/codewind-pfe-amd64:test -f pfe-test-dockerfile .
rm pfe-test-dockerfile
else
docker tag eclipse/codewind-${image}-${IMAGE_ARCH}:latest eclipse/codewind-${image}-${IMAGE_ARCH}:test
fi
else
echo "+++ FAILED TO BUILD $IMAGE_NAME - exiting. +++";
exit 12;
fi
done;
echo -e "\n+++ ALL DOCKER IMAGES SUCCESSFULLY BUILT +++\n";
docker images | grep codewind;
'''
}
}
}
stage('Start Codewind and run the API tests') {
options {
timeout(time: 2, unit: 'HOURS')
}
steps {
withEnv(["PATH=$PATH:~/.local/bin;NOBUILD=true"]){
withDockerRegistry([url: 'https://index.docker.io/v1/', credentialsId: 'docker.com-bot']) {
sh '''#!/usr/bin/env bash
echo "Starting tests for Eclipse Codewind ..."
export PATH=$PATH:/home/jenkins/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node_js/bin/
mkdir -p $HOME/dc
export PATH=$PATH:$HOME/dc/
ARCH=`uname -m`;
printf "\n\n${MAGENTA}Platform: $ARCH ${RESET}\n"
DIR=`pwd`;
# Install nvm to easily set version of node to use
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
export NVM_DIR="$HOME/.nvm"
. $NVM_DIR/nvm.sh
nvm i 10
# Install docker-compose
curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o $HOME/dc/docker-compose
if [ $? -ne 0 ]; then
echo "Error downloading docker-compose"
exit 1
fi
chmod +x $HOME/dc/docker-compose
# Create codewind-workspace if it does not exist
printf "\n\nCreating codewind-workspace\n"
mkdir -m 777 -p $DIR/codewind-workspace
# Save Docker image ID of PFE to ensure we're not using the image from Dockerhub
BUILT_PFE_IMAGE_ID=$(docker images --filter=reference=eclipse/codewind-pfe-amd64:latest --format "{{.ID}}")
echo "PFE Image: $BUILT_PFE_IMAGE_ID"
# Set image tag to test
export CWCTL_IMAGE_TAG=test
# If CHANGE_TARGET is set then this is a PR so use the target branch (usually master)
if [ -z "$CHANGE_TARGET" ]; then
export CW_CLI_BRANCH=$GIT_BRANCH
else
export CW_CLI_BRANCH=$CHANGE_TARGET
fi
echo "cwctl branch to be used: $CW_CLI_BRANCH"
# Start Codewind
sh $DIR/start.sh
if [ $? -ne 0 ]; then
echo "Error starting Codewind"
exit 1
fi
# Check that cwctl has not pulled down a new PFE image
POST_START_IMAGE_ID=$(docker images --filter=reference=eclipse/codewind-pfe-amd64:latest --format "{{.ID}}")
echo "PFE Container image: $POST_START_IMAGE_ID"
if [ "$BUILT_PFE_IMAGE_ID" != "$POST_START_IMAGE_ID" ]; then
echo "Error a new PFE image has been downloaded"
echo "Built PFE image ID: $BUILT_PFE_IMAGE_ID"
echo "Downloaded PFE image ID: $POST_START_IMAGE_ID"
echo "Docker images"
docker images
echo "Docker ps"
docker ps
exit 1
fi
# Run the API tests now Portal has started
cd $DIR/test/
npm install
if [ $? -ne 0 ]; then
exit 1
fi
npm run apitest
if [ $? -ne 0 ]; then
exit 1
fi
'''
}
}
}
}
stage('Output Code Coverage for PFE (Portal API and Unit tests, File-watcher Unit tests only)') {
options {
timeout(time: 30, unit: 'MINUTES')
}
steps {
withEnv(["PATH=$PATH:~/.local/bin;NOBUILD=true"]) {
withDockerRegistry([url: 'https://index.docker.io/v1/', credentialsId: 'docker.com-bot']) {
sh '''#!/usr/bin/env bash
DIR=`pwd`;
export PATH=$PATH:/home/jenkins/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node_js/bin/
# Install nvm to easily set version of node to use
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
export NVM_DIR="$HOME/.nvm"
. $NVM_DIR/nvm.sh
nvm i 10
docker image inspect eclipse/codewind-pfe-amd64:test -f '{{.Config.Env}}' | grep ENABLE_CODE_COVERAGE=true
if [ $? -eq 0 ]; then
echo "Code coverage enabled in eclipse/codewind-pfe-amd64:test (image)"
fi
docker inspect -f '{{.Config.Env}}' codewind-pfe | grep ENABLE_CODE_COVERAGE=true
if [ $? -eq 0 ]; then
echo "Code coverage enabled in codewind-pfe (container)"
fi
CODECOV_ENABLED=true FW_COVERAGE_ENABLED=true node $DIR/test/scripts/generate_complete_coverage.js
'''
}
}
}
}
stage('Publish Docker images') {
// This when clause disables PR build uploads; you may comment this out if you want your build uploaded.
when {
beforeAgent true
not {
changeRequest()
}
}
options {
timeout(time: 30, unit: 'MINUTES')
retry(3)
}
steps {
withDockerRegistry([url: 'https://index.docker.io/v1/', credentialsId: 'docker.com-bot']) {
sh '''#!/usr/bin/env bash
echo "Publishing docker images for Eclipse Codewind ..."
echo "Branch name is $GIT_BRANCH"
if [[ $GIT_BRANCH == "master" ]]; then
TAG="latest"
else
TAG=$GIT_BRANCH
fi
# Publish docker images with a filter for branch name
# Acceptable branch names: master, start with '<number>.<number>'
if [[ $GIT_BRANCH == "master" ]] || [[ $GIT_BRANCH =~ ^([0-9]+\\.[0-9]+) ]]; then
declare -a DOCKER_IMAGE_ARRAY=("eclipse/codewind-performance-amd64"
"eclipse/codewind-pfe-amd64"
"eclipse/codewind-keycloak-amd64"
"eclipse/codewind-gatekeeper-amd64")
for i in "${DOCKER_IMAGE_ARRAY[@]}"
do
if [[ $GIT_BRANCH =~ ^([0-9]+\\.[0-9]+) ]]; then
docker tag $i $i:${TAG:-latest}
fi
echo "Publishing $i:$TAG"
docker push $i:${TAG:-latest}
done
if [[ $GIT_BRANCH =~ ^([0-9]+\\.[0-9]+) ]]; then
IFS='.' # set '.' as delimiter
read -ra TOKENS <<< "$GIT_BRANCH"
IFS=' ' # reset delimiter
export TAG_CUMULATIVE=${TOKENS[0]}.${TOKENS[1]}
for i in "${DOCKER_IMAGE_ARRAY[@]}"
do
docker tag $i $i:${TAG_CUMULATIVE:-latest}
echo "Publishing $i:$TAG_CUMULATIVE"
docker push $i:${TAG_CUMULATIVE:-latest}
done
fi
else
echo "Skip publishing docker images for $GIT_BRANCH branch"
fi
'''
}
}
}
}
post {
always {
sh '''#!/usr/bin/env bash
# Output portal logs
printf "\n********** codewind-pfe logs **********\n\n"
docker logs codewind-pfe
# Shutdown and cleanup.
printf "\nStopping and removing Codewind Docker containers.\n"
DOCKER_PS="docker ps -a -q --filter name=codewind";
DOCKER_IMAGES="docker images -q --filter reference=codewind*";
DOCKER_PS_APPS="docker ps -a -q --filter name=cw";
DOCKER_IMAGES_APPS="docker images -q --filter reference=cw*";
# Check to make sure that there are actually proceses to remove
NUMBER_OF_PROCESSES=$($DOCKER_PS | wc -l)
if [ $NUMBER_OF_PROCESSES -gt 0 ]; then
# Removing containers
printf "\nStopping all running containers\n";
docker rm -f $($DOCKER_PS)
if [ $? -eq 0 ]; then
printf "\nSuccessfully removed containers\n";
else
printf "\nError removing containers\n";
exit;
fi
printf "\nSUCCESSFULLY REMOVED CONTAINERS\n";
else
printf "\nERROR: THERE ARE NO CONTAINERS TO REMOVE\n";
fi
# Check to make sure that there are actually proceses to remove
NUMBER_OF_PROCESSES=$($DOCKER_PS_APPS | wc -l)
if [ $NUMBER_OF_PROCESSES -gt 0 ]; then
# Removing containers
docker rm -f $($DOCKER_PS_APPS)
if [ $? -eq 0 ]; then
printf "\n${GREEN}Successfully removed containers $RESET\n";
else
printf "\n${RED}Error removing containers $RESET\n";
exit;
fi
printf "\n${GREEN}SUCCESSFULLY REMOVED CONTAINERS $RESET\n";
else
printf "\n${RED}ERROR: THERE ARE NO CONTAINERS TO REMOVE $RESET\n";
fi
# Remove the codewind network
printf "\nRemoving docker network\n";
docker network rm codewind_network
if [ $? -eq 0 ]; then
printf "\n${GREEN}Successfully removed docker network $RESET\n";
else
printf "\n${RED}Error removing docker network $RESET\n";
fi
# Remove the default network
printf "\nRemoving docker network\n";
docker network rm codewind_default
if [ $? -eq 0 ]; then
printf "\n${GREEN}Successfully removed docker network $RESET\n";
else
printf "\n${RED}Error removing docker network $RESET\n";
fi
# Docker system prune
echo "Docker system prune ..."
docker system df
docker system prune -a --volumes -f
docker builder prune -a -f
docker system df
df -lh
# Remove docker-compose
echo "Removing docker-compose"
rm -rf $HOME/dc
'''
echo 'Clean up workspace'
deleteDir() /* clean up our workspace */
}
failure {
sendEmailNotification()
}
}
}