forked from data61/anonlink-entity-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile.groovy
511 lines (452 loc) · 18.3 KB
/
Jenkinsfile.groovy
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
@Library("[email protected]")
import com.n1analytics.docker.DockerContainer;
import com.n1analytics.docker.DockerUtils;
import com.n1analytics.docker.QuayIORepo
import com.n1analytics.git.GitUtils;
import com.n1analytics.git.GitCommit;
import com.n1analytics.git.GitRepo;
String gitContextDockerBuild = "required-docker-images-build"
String gitContextComposeDeploy = "required-dockercompose-deploy"
String gitContextIntegrationTests = "required-integration-tests"
String gitContextDocumentation = "required-build-documentation"
String gitContextPublish = "required-publish-docker-images"
String gitContextKubernetesDeployment = "optional-kubernetes-deployment"
String gitContextLocalBenchmark = "jenkins-benchmark"
DockerUtils dockerUtils
GitCommit gitCommit
String composeProject
String composeCmd(String composeProject, String cmd) {
return sh (
script: """ docker-compose --no-ansi \
-f tools/docker-compose.yml \
-f tools/ci.yml \
-p ${composeProject} ${cmd}""",
returnStdout: true
).trim()
}
node('docker&&multicore&&ram') {
stage ('Initialisation') {
// Knowing this git commit will enable us to send to github the corresponding status.
gitCommit = GitUtils.checkoutFromSCM(this)
// Login to quay.io
dockerUtils = new DockerUtils(this)
dockerUtils.dockerLoginQuayIO(QuayIORepo.ENTITY_SERVICE_APP)
composeProject = "es-${BRANCH_NAME}-${BUILD_NUMBER}".replaceAll("[-_.]", "").toLowerCase()
}
try {
stage('Docker Build') {
gitCommit.setInProgressStatus(gitContextDockerBuild);
try {
dir("backend") {
String imageNameLabel = QuayIORepo.ENTITY_SERVICE_APP.getRepo() + ":latest"
dockerUtils.dockerCommand("build -t ${imageNameLabel} .")
}
dir("frontend") {
String imageNameLabel = QuayIORepo.ENTITY_SERVICE_NGINX.getRepo() + ":latest"
dockerUtils.dockerCommand("build -t ${imageNameLabel} .")
}
dir("docs") {
String imageNameLabel = "quay.io/n1analytics/entity-docs:latest"
dockerUtils.dockerCommand("build -t ${imageNameLabel} .")
}
dir("benchmarking") {
String imageNameLabel = "quay.io/n1analytics/entity-benchmark:latest"
dockerUtils.dockerCommand("build -t ${imageNameLabel} .")
}
gitCommit.setSuccessStatus(gitContextDockerBuild)
} catch (err) {
print("Error in docker build stage:\n" + err)
gitCommit.setFailStatus(gitContextDockerBuild)
throw err
}
}
stage('Compose Deploy') {
gitCommit.setInProgressStatus(gitContextComposeDeploy);
try {
echo("Start all the containers (including tests)")
sh "docker-compose -v"
composeCmd(composeProject, "up -d db minio redis backend db_init worker nginx tests")
gitCommit.setSuccessStatus(gitContextComposeDeploy)
} catch (err) {
print("Error in compose deploy stage:\n" + err)
gitCommit.setFailStatus(gitContextComposeDeploy)
throw err
}
}
stage('Integration Tests') {
gitCommit.setInProgressStatus(gitContextIntegrationTests);
try {
testContainerID = composeCmd(composeProject,"ps -q tests")
DockerContainer containerTests = new DockerContainer(dockerUtils, testContainerID)
sleep 30
timeout(time: 60, unit: 'MINUTES') {
containerTests.watchLogs()
composeCmd(composeProject, "logs nginx &> nginx.log")
composeCmd(composeProject, "logs backend &> backend.log")
composeCmd(composeProject, "logs worker &> worker.log")
composeCmd(composeProject, "logs db &> db.log")
archiveArtifacts artifacts: "*.log", fingerprint: false
if (containerTests.getExitCode() != "0") {
throw new Exception("The integration tests failed.")
}
gitCommit.setSuccessStatus(gitContextIntegrationTests)
}
} catch (err) {
print("Error in integration tests stage:\n" + err)
gitCommit.setFailStatus(gitContextIntegrationTests)
throw err
}
}
stage('Benchmark') {
gitCommit.setInProgressStatus(gitContextLocalBenchmark);
String benchmarkContainerName = composeProject + "benchmark"
String networkName = composeProject + "_default"
String localserver = "http://nginx:8851"
try {
timeout(time: 15, unit: 'MINUTES') {
def experiments = readJSON text: """
[
{
"sizes": ["100K", "100K"],
"threshold": 0.95
}
]
"""
// Create a docker volume to use as a cache (if it doesn't exist)
sh '''
./tools/create-benchmark-cache.sh
'''
// We use a custom experiments for jenkins
writeJSON file: 'linkage-bench-cache-experiments.json', json: experiments
sh """
echo "Copying experiments into cache volume"
docker run --rm -v `pwd`:/src \
-v linkage-benchmark-data:/data busybox \
sh -c "cp -r /src/linkage-bench-cache-experiments.json /data; chown -R 1000:1000 /data"
echo "Starting benchmarks"
docker run \
--name ${benchmarkContainerName} \
--network ${networkName} \
-e SERVER=${localserver} \
-e DATA_PATH=/cache \
-e EXPERIMENT=/cache/linkage-bench-cache-experiments.json \
-e RESULTS_PATH=/app/results.json \
--mount source=linkage-benchmark-data,target=/cache \
quay.io/n1analytics/entity-benchmark:latest
docker cp ${benchmarkContainerName}:/app/results.json results.json
"""
archiveArtifacts artifacts: "results.json", fingerprint: true
gitCommit.setSuccessStatus(gitContextLocalBenchmark)
}
} catch (err) {
print("Error or timeout in benchmarking stage:\n" + err)
gitCommit.setFailStatus(gitContextLocalBenchmark)
throw err
} finally {
sh """
docker rm -v ${benchmarkContainerName}
"""
}
}
stage('Documentation') {
gitCommit.setInProgressStatus(gitContextDocumentation);
String dockerContainerDocsBuilderName = composeProject + "docbuilder"
// TODO Update this stage with the library.
try {
sh """
mkdir -p html
docker run -v `pwd`:/src --name ${dockerContainerDocsBuilderName} quay.io/n1analytics/entity-app:doc-builder
docker cp ${dockerContainerDocsBuilderName}:/build `pwd`/html
cd html/build
zip -q -r n1-docs.zip *
mv n1-docs.zip ../../
cd ../..
cp -r html/build/* frontend/static
rm -r html
docker build -t quay.io/n1analytics/entity-nginx:latest frontend
docker rm ${dockerContainerDocsBuilderName}
"""
archiveArtifacts artifacts: 'n1-docs.zip', fingerprint: true
gitCommit.setSuccessStatus(gitContextDocumentation)
} catch (err) {
print("Error in documentation stage:\n" + err)
gitCommit.setFailStatus(gitContextDocumentation)
throw err
}
}
stage('Publish') {
gitCommit.setInProgressStatus(gitContextPublish);
try {
sh '''
./tools/upload.sh
'''
gitCommit.setSuccessStatus(gitContextPublish)
} catch (Exception err) {
print("Error in publish stage:\n" + err)
gitCommit.setFailStatus(gitContextPublish)
throw err
}
sh 'docker logout quay.io'
}
} finally {
stage("Cleaning") {
try {
dockerUtils.dockerLogoutQuayIOWithoutFail()
composeCmd(composeProject, " down -v")
} catch(Exception err) {
print("Error in cleaning stage, but do nothing about it:\n" + err)
// Do nothing on purpose.
}
}
}
}
node('helm && kubectl') {
stage('K8s Deployment') {
gitCommit.setInProgressStatus(gitContextKubernetesDeployment)
GitUtils.checkoutFromSCM(this)
// Pre-existant configuration file available from jenkins
CLUSTER_CONFIG_FILE_ID = "awsClusterConfig"
DEPLOYMENT = composeProject
NAMESPACE = "default"
def TAG = sh(script: """python tools/get_docker_tag.py $BRANCH_NAME app""", returnStdout: true).trim()
def NGINXTAG = sh(script: """python tools/get_docker_tag.py $BRANCH_NAME nginx""", returnStdout: true).trim()
configFileProvider([configFile(fileId: CLUSTER_CONFIG_FILE_ID, variable: 'KUBECONFIG')]) {
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', credentialsId: 'aws_jenkins', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
try {
dicTestVersions = [
"api" : [
"www" : [
"image": [
"tag": NGINXTAG
]
],
"app" : [
"image": [
"tag": TAG
]
],
"dbinit": [
"image": [
"tag": TAG
]
]
],
"workers": [
"image": [
"tag": TAG
]
]
]
timeout(time: 60, unit: 'MINUTES') {
dir("deployment/entity-service") {
if (fileExists("test-versions.yaml")) {
sh "rm test-versions.yaml"
}
writeYaml(file: "test-versions.yaml", data: dicTestVersions)
sh """
helm version
helm dependency update
helm upgrade --install --wait --namespace ${NAMESPACE} ${DEPLOYMENT} . \
-f values.yaml -f minimal-values.yaml -f test-versions.yaml \
--set api.app.debug=true \
--set postgresql.postgresqlPassword=notaproductionpassword \
--set api.ingress.enabled=false
"""
// give the cluster a chance to provision volumes etc, assign an IP to the service, then create a new job to test it
sleep(time: 120, unit: "SECONDS")
}
String serviceIP = sh(script: """
kubectl get services -lapp=${DEPLOYMENT}-entity-service -o jsonpath="{.items[0].spec.clusterIP}"
""", returnStdout: true).trim()
pvc = createK8sTestJob(DEPLOYMENT, QuayIORepo.ENTITY_SERVICE_APP.getRepo() + ":" + TAG, serviceIP)
sleep(time: 120, unit: "SECONDS")
def jobPodName = sh(script: """
kubectl get pods -l deployment=${DEPLOYMENT} -o jsonpath="{.items[0].metadata.name}"
""", returnStdout: true).trim()
echo jobPodName
sh """
# Watch the output
kubectl logs -f $jobPodName
"""
resultFile = getResultsFromK8s(DEPLOYMENT, pvc)
junit resultFile
}
gitCommit.setSuccessStatus(gitContextKubernetesDeployment)
} catch (error) { // timeout reached or input false
print("Error in k8s deployment stage:\n" + error)
gitCommit.setFailStatus(gitContextKubernetesDeployment)
throw new Exception("The k8s integration tests failed.")
} finally {
try {
sh """
helm delete --purge ${DEPLOYMENT}
kubectl delete job -lapp=${DEPLOYMENT}-entity-service
kubectl delete job ${DEPLOYMENT}-test
kubectl delete all -ldeployment=${DEPLOYMENT}
kubectl delete all -lrelease=${DEPLOYMENT}
"""
} catch(Exception err) {
print("Error in final cleanup, Ignoring:\n" + err)
// Do nothing on purpose.
}
}
}
}
}
}
void createK8sFromManifest(LinkedHashMap<String, Object> manifest) {
String fileName = "tmp.yaml"
if (fileExists(fileName)) {
sh "rm " + fileName
}
writeYaml(file: fileName, data: manifest)
String shCommand = "kubectl create -f " + fileName
sh shCommand
sh "rm " + fileName
}
String getResultsFromK8s(String deploymentName, String pvcName) {
String pod_name = "${BRANCH_NAME}-${BUILD_NUMBER}-tmppod"
def k8s_pod_manifest = [
"apiVersion": "v1",
"kind": "Pod",
"metadata": [
"name": pod_name,
"labels" : [
"deployment": deploymentName
]
],
"spec": [
"restartPolicy" : "Never",
"volumes": [
[
"name": "results",
"persistentVolumeClaim": [
"claimName": pvcName
]
]
],
"containers" : [
[
"name" : "resultpod",
"image" : "python",
"command" : [
"sleep",
"3600"
],
"volumeMounts": [[
"mountPath": "/mnt",
"name": "results"
]]
]
],
]
]
createK8sFromManifest(k8s_pod_manifest)
sleep(time: 60, unit: "SECONDS")
String out = "k8s-results.xml"
sh """
# fetch the results from the running pod
kubectl cp $NAMESPACE/$pod_name:/mnt/results.xml $out
# delete the temp pod
kubectl delete pod $pod_name
# delete the pvc
kubectl delete pvc -l deployment=$deploymentName
"""
return out
}
String createK8sTestJob(String deploymentName, String imageNameWithTag, String serviceIP) {
String pvc_name = deploymentName + "-test-results"
def k8s_pvc_manifest = [
"apiVersion": "v1",
"kind" : "PersistentVolumeClaim",
"metadata" : [
"name": pvc_name,
"labels" : [
"jobgroup": "jenkins-es-integration-test",
"deployment": deploymentName
]
],
"spec" : [
"storageClassName": "default",
"accessModes": [
"ReadWriteOnce"
],
"resources" : [
"requests": [
"storage": "1Gi"
]
]
]
]
def k8s_job_manifest = [
"apiVersion": "batch/v1",
"kind" : "Job",
"metadata" : [
"name": deploymentName + "-test",
"labels" : [
"jobgroup": "jenkins-es-integration-test",
"deployment": deploymentName
]
],
"spec" : [
"completions": 1,
"parallelism": 1,
"template" : [
"metadata": [
"labels": [
"jobgroup" : "jenkins-es-integration-test",
"deployment": deploymentName
]
],
"spec" : [
"securityContext": [
"runAsUser": 0,
"fsGroup": 0
],
"restartPolicy" : "Never",
"volumes": [
[
"name": "results",
"persistentVolumeClaim": [
"claimName": pvc_name
]
]
],
"containers" : [
[
"name" : "entitytester",
"image" : imageNameWithTag,
"imagePullPolicy": "Always",
"env" : [
[
"name" : "ENTITY_SERVICE_URL",
"value": "http://" + serviceIP + "/api/v1"
]
],
"command" : [
"python",
"-m",
"pytest",
"entityservice/tests",
"-x",
"--junit-xml=/mnt/results.xml"
],
"volumeMounts": [[
"mountPath": "/mnt",
"name": "results"
]]
]
],
"imagePullSecrets": [
[
"name": "n1-quay-pull-secret"
]
]
]
]
]
]
createK8sFromManifest(k8s_pvc_manifest)
createK8sFromManifest(k8s_job_manifest)
return pvc_name
}