-
Notifications
You must be signed in to change notification settings - Fork 4
/
Jenkinsfile
448 lines (391 loc) · 14.8 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
AWS_PROFILE = "mithril-jenkins"
BUILD_IMAGE = "hub.docker.hpecorp.net/sec-eng/ubuntu:pipeline"
DEVELOPMENT_IMAGE = "529024819027.dkr.ecr.us-east-1.amazonaws.com/mithril"
CHANNEL_NAME = "#notify-project-mithril"
ECR_REGION = "us-east-1"
ECR_REPOSITORY_PREFIX = "mithril"
HPE_REGISTRY = "hub.docker.hpecorp.net/sec-eng"
ISTIO_STABLE_BRANCH = "release-1.10" // the Istio branch to be distributed
PATCHSET_BUCKET = "mithril-poc-patchset"
CUSTOMER_BUCKET = "mithril-customer-assets"
MITHRIL_MAIN_BRANCH = "master"
PROXY = "http://proxy.houston.hpecorp.net:8080"
def SLACK_ERROR_MESSAGE
def SLACK_ERROR_COLOR
// Start of the pipeline
pipeline {
options {
timestamps()
ansiColor('xterm')
}
// Version of the Jenkins slave
agent {
label 'docker-v20.10'
}
environment {
BUILD_TAG = makeTag()
GOOS = "linux"
}
parameters {
string(name: 'ISTIO_BRANCH', defaultValue: ISTIO_STABLE_BRANCH, description: 'The Istio branch to run against')
}
triggers {
parameterizedCron(
BRANCH_NAME == MITHRIL_MAIN_BRANCH ? '''
H H(0-3) * * * %ISTIO_BRANCH=master
H H(0-3) * * * %ISTIO_BRANCH=release-1.10
H H(0-3) * * * %ISTIO_BRANCH=release-1.11
H H(0-3) * * * %ISTIO_BRANCH=release-1.12
''': ''
)
}
stages {
stage("notify-slack") {
steps {
script {
slackSend (
channel: CHANNEL_NAME,
message: "Hello. The pipeline ${currentBuild.fullDisplayName} started. (<${env.BUILD_URL}|See Job>)")
}
}
}
stage("setup-pipeline") {
steps {
script {
def secrets = vaultGetSecrets()
AWS_ACCOUNT_ID = "${secrets.awsAccountID}"
AWS_ACCESS_KEY_ID = "${secrets.awsAccessKeyID}"
AWS_SECRET_ACCESS_KEY = "${secrets.awsSecretAccessKeyID}"
HPE_DOCKER_HUB_SECRET = "${secrets.dockerHubToken}"
}
}
}
stage("make-poc-codebase") {
steps {
// Istio clone from the specified branch
sh "git clone --single-branch --branch ${params.ISTIO_BRANCH} https://github.com/istio/istio.git"
// Apply Mithril patches
sh """
cd istio
git apply ${WORKSPACE}/POC/patches/poc.${params.ISTIO_BRANCH}.patch
"""
}
}
stage("build-and-push-dev-images-ecr"){
environment {
AWS_ACCESS_KEY_ID = "${AWS_ACCESS_KEY_ID}"
AWS_SECRET_ACCESS_KEY = "${AWS_SECRET_ACCESS_KEY}"
}
steps {
script {
// Creating volume for the docker.sock, passing some environment variables for Dockerhub authentication
// and build tag, building Istio and pushing images to the ECR.
docker.image(BUILD_IMAGE).inside("-v /var/run/docker.sock:/var/run/docker.sock") {
def ECR_REGISTRY = AWS_ACCOUNT_ID + ".dkr.ecr." + ECR_REGION + ".amazonaws.com";
sh """#!/bin/bash
aws ecr get-login-password --region ${ECR_REGION} | \
docker login --username AWS --password-stdin ${ECR_REGISTRY}
docker build -t mithril:${BUILD_TAG} \
-f ./docker/Dockerfile .
docker tag mithril:${BUILD_TAG} ${DEVELOPMENT_IMAGE}:${BUILD_TAG}
docker push ${DEVELOPMENT_IMAGE}:${BUILD_TAG}
"""
}
}
}
}
stage("unit-test") {
environment {
AWS_ACCESS_KEY_ID = "${AWS_ACCESS_KEY_ID}"
AWS_SECRET_ACCESS_KEY = "${AWS_SECRET_ACCESS_KEY}"
ISTIO_BRANCH = "${params.ISTIO_BRANCH}"
}
steps {
script {
docker.image(BUILD_IMAGE).inside("-v /var/run/docker.sock:/var/run/docker.sock") {
sh '''#!/bin/bash
cd ${WORKSPACE}/terraform/istio-unit-tests
echo "** Begin istio unit tests **"
terraform init
terraform apply -auto-approve -var "BUILD_TAG"=${BUILD_TAG} -var "AWS_PROFILE"=${AWS_PROFILE} -var "ISTIO_BRANCH"=${ISTIO_BRANCH}
num_tries=0
while [ $num_tries -lt 500 ];
do
aws s3api head-object --bucket mithril-artifacts --key "${BUILD_TAG}/${BUILD_TAG}-istio-unit-tests-log.txt" --no-cli-pager 2> /dev/null
if [ $? -eq 0 ];
then
break;
else
((num_tries++))
sleep 1;
fi
done
terraform destroy -auto-approve
aws s3 cp "s3://mithril-artifacts/${BUILD_TAG}/${BUILD_TAG}-istio-unit-tests-result.txt" .
RESULT=$(tail -n 1 "${BUILD_TAG}-istio-unit-tests-result.txt" | grep -oE '^..')
if [[ "$RESULT" == "ok" ]];
then
echo "Istio unit tests successful"
else
echo "Istio unit tests failed"
cat "${BUILD_TAG}-istio-unit-tests-result.txt"
exit 1
fi
'''
}
}
}
}
stage("build-and-push-istio-images") {
environment {
AWS_ACCESS_KEY_ID = "${AWS_ACCESS_KEY_ID}"
AWS_SECRET_ACCESS_KEY = "${AWS_SECRET_ACCESS_KEY}"
BUILD_WITH_CONTAINER = 0
ISTIO_BRANCH = "${params.ISTIO_BRANCH}"
}
steps {
script {
def passwordMask = [
$class: 'MaskPasswordsBuildWrapper',
varPasswordPairs: [ [ password: HPE_DOCKER_HUB_SECRET ] ]
]
wrap(passwordMask) {
docker.image(BUILD_IMAGE).inside("-v /var/run/docker.sock:/var/run/docker.sock") {
// Build and push to ECR registry
def ECR_REGISTRY = AWS_ACCOUNT_ID + ".dkr.ecr." + ECR_REGION + ".amazonaws.com";
def ECR_HUB = ECR_REGISTRY + "/" + ECR_REPOSITORY_PREFIX;
sh """#!/bin/bash
export HUB=${HPE_REGISTRY}
export TAG=${BUILD_TAG}
export istio_branch=${istio_branch}
echo ${HPE_DOCKER_HUB_SECRET} | docker login hub.docker.hpecorp.net --username ${HPE_DOCKER_HUB_SECRET} --password-stdin
# Checks go version dependencies of istio branches
. ./terraform/istio-unit-tests/check-go-version.sh
cd istio && go get github.com/spiffe/go-spiffe/v2 && go mod tidy && make push
aws ecr get-login-password --region ${ECR_REGION} | \
docker login --username AWS --password-stdin ${ECR_REGISTRY}
docker images --format "{{.ID}} {{.Repository}}" | while read line; do
pieces=(\$line)
if [[ "\${pieces[1]}" == *"hub.docker.hpecorp.net"* ]] && [[ "\${pieces[1]}" != *"/ubuntu"* ]]; then
tag=\$(echo "\${pieces[1]}" | sed -e "s|^${HPE_REGISTRY}||")
docker tag "\${pieces[0]}" "${ECR_HUB}\${tag}:${BUILD_TAG}"
docker push "${ECR_HUB}\${tag}:${BUILD_TAG}"
fi
done
"""
}
}
}
}
}
// Tag the current build as "latest" whenever a new commit
// comes into master and pushes the tag to the ECR repository
stage("tag-latest-images") {
environment {
AWS_ACCESS_KEY_ID = "${AWS_ACCESS_KEY_ID}"
AWS_SECRET_ACCESS_KEY = "${AWS_SECRET_ACCESS_KEY}"
}
when {
allOf {
branch MITHRIL_MAIN_BRANCH
equals expected: ISTIO_STABLE_BRANCH, actual: params.ISTIO_BRANCH
}
}
steps {
script {
def ECR_REGISTRY = AWS_ACCOUNT_ID + ".dkr.ecr." + ECR_REGION + ".amazonaws.com"
def ECR_HUB = ECR_REGISTRY + "/" + ECR_REPOSITORY_PREFIX
docker.image(BUILD_IMAGE).inside("-v /var/run/docker.sock:/var/run/docker.sock") {
sh """#!/bin/bash
aws ecr get-login-password --region ${ECR_REGION} | \
docker login --username AWS --password-stdin ${ECR_REGISTRY}
docker images "${ECR_HUB}/*" --format "{{.ID}} {{.Repository}}" | while read line; do
pieces=(\$line)
docker tag "\${pieces[0]}" "\${pieces[1]}":latest
docker push "\${pieces[1]}":latest
done
"""
}
}
}
}
stage("run-integration-tests") {
environment {
AWS_ACCESS_KEY_ID = "${AWS_ACCESS_KEY_ID}"
AWS_SECRET_ACCESS_KEY = "${AWS_SECRET_ACCESS_KEY}"
}
steps {
script {
def folders = sh(script: 'cd terraform/integration-tests && ls -1', returnStdout: true).split()
def builders = [:]
folders.each{ folder ->
builders[folder] = {
stage("$folder") {
script {
def usecase = folder
docker.image(BUILD_IMAGE).inside("-v /var/run/docker.sock:/var/run/docker.sock --name $usecase -e usecase=$usecase ") {
sh '''#!/bin/bash
cd terraform/integration-tests/${usecase}
echo "** Begin test ${usecase} **"
terraform init
terraform apply -auto-approve -var "BUILD_TAG"=${BUILD_TAG} -var "AWS_PROFILE"=${AWS_PROFILE}
num_tries=0
while [ $num_tries -lt 500 ];
do
aws s3api head-object --bucket mithril-artifacts --key "${BUILD_TAG}/${BUILD_TAG}-${usecase}-log.txt" --no-cli-pager 2> /dev/null
if [ $? -eq 0 ];
then
break;
else
((num_tries++))
sleep 1;
fi
done
terraform destroy -auto-approve
'''
}
}
}
}
}
parallel builders
}
}
}
stage("analyze-integration-tests") {
environment {
AWS_ACCESS_KEY_ID = "${AWS_ACCESS_KEY_ID}"
AWS_SECRET_ACCESS_KEY = "${AWS_SECRET_ACCESS_KEY}"
}
steps {
script {
docker.image(BUILD_IMAGE).inside("-v /var/run/docker.sock:/var/run/docker.sock") {
sh '''#!/bin/bash
RESULT_LIST=()
cd terraform/integration-tests
HAS_MISSING_ARTIFACTS=false
for FOLDER in *;
do
BUCKET_EXISTS=false
aws s3api head-object --bucket mithril-artifacts --key "${BUILD_TAG}/${BUILD_TAG}-${FOLDER}-result.txt" --no-cli-pager
if [ $? -eq 0 ];
then
BUCKET_EXISTS=true
fi
if $BUCKET_EXISTS;
then
echo "Artifact object for usecase ${FOLDER} exists"
else
echo "Artifact ${BUILD_TAG}/${BUILD_TAG}-${FOLDER}-result.txt object for usecase ${FOLDER} does not exist"
HAS_MISSING_ARTIFACTS=true
fi
done
if $HAS_MISSING_ARTIFACTS;
then
echo "One or more artifacts do not exist"
exit 1
else
echo "All artifacts found"
fi
HAS_FAILED_TEST=false
for FOLDER in *;
do
aws s3 cp "s3://mithril-artifacts/${BUILD_TAG}/${BUILD_TAG}-${FOLDER}-result.txt" .
RESULT=$(tail -n 1 "${BUILD_TAG}-${FOLDER}-result.txt" | grep -oE '^..')
if [ "$RESULT" == "ok" ];
then
echo "Test for usecase ${FOLDER} successful"
else
echo "Test for usecase ${FOLDER} failed"
cat "${BUILD_TAG}-${FOLDER}-result.txt"
HAS_FAILED_TEST=true
fi
done
if $HAS_FAILED_TEST;
then
echo "One or more tests have failed"
exit 1
fi
'''
}
}
}
}
stage("distribute-poc") {
environment {
AWS_ACCESS_KEY_ID = "${AWS_ACCESS_KEY_ID}"
AWS_SECRET_ACCESS_KEY = "${AWS_SECRET_ACCESS_KEY}"
}
when {
allOf {
branch MITHRIL_MAIN_BRANCH
equals expected: ISTIO_STABLE_BRANCH, actual: params.ISTIO_BRANCH
}
}
failFast true
parallel {
stage("distribute-assets") {
steps {
script {
def S3_CUSTOMER_BUCKET = "s3://" + CUSTOMER_BUCKET
docker.image(BUILD_IMAGE).inside("-v /var/run/docker.sock:/var/run/docker.sock") {
sh """
cd ./POC
tar -zcvf mithril.tar.gz bookinfo spire istio configmaps.yaml \
deploy-all.sh create-namespaces.sh cleanup-all.sh forward-port.sh create-kind-cluster.sh \
doc/poc-instructions.md demo/demo-script.sh demo/README.md demo/federation-demo.sh ../usecases/federation
aws s3 cp mithril.tar.gz ${S3_CUSTOMER_BUCKET}
aws s3api put-object-acl --bucket ${CUSTOMER_BUCKET} --key mithril.tar.gz --acl public-read
"""
}
}
}
}
stage("distribute-patches") {
steps {
script {
def S3_PATCHSET_BUCKET = "s3://" + PATCHSET_BUCKET
docker.image(BUILD_IMAGE).inside("-v /var/run/docker.sock:/var/run/docker.sock") {
sh """
cd ./POC
tar -zcvf mithril-poc-patchset.tar.gz patches
aws s3 cp mithril-poc-patchset.tar.gz ${S3_PATCHSET_BUCKET}
aws s3api put-object-acl --bucket ${PATCHSET_BUCKET} --key mithril-poc-patchset.tar.gz --acl public-read
"""
}
}
}
}
}
}
}
post {
success {
slackSend (
channel: CHANNEL_NAME,
color: 'good',
message: "The pipeline ${currentBuild.fullDisplayName} completed successfully. (<${env.BUILD_URL}|See Job>)"
)
}
failure {
script {
SLACK_ERROR_MESSAGE = "Ooops! The pipeline ${currentBuild.fullDisplayName} failed."
SLACK_ERROR_COLOR = "bad"
if (BRANCH_NAME == MITHRIL_MAIN_BRANCH) {
SLACK_ERROR_MESSAGE = "@channel The pipeline ${currentBuild.fullDisplayName} failed on `${BRANCH_NAME}`"
SLACK_ERROR_COLOR = "danger"
}
}
slackSend (
channel: CHANNEL_NAME,
color: SLACK_ERROR_COLOR,
message: "${SLACK_ERROR_MESSAGE} (<${env.BUILD_URL}|See Job>)",
)
}
}
}
// Method for creating the build tag
def makeTag() {
def today = new Date()
return today.format("dd-MM-yyyy") + "-" + params.ISTIO_BRANCH + "-" + env.GIT_BRANCH + "-" + env.GIT_COMMIT.substring(0,7)
}