This repository has been archived by the owner on Jun 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Jenkinsfile
169 lines (149 loc) · 4.82 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
//
// Copyright (C) 2020 IBM. All Rights Reserved.
//
// See LICENSE.txt file in the root directory
// of this source tree for licensing information.
//
pipeline {
agent {
node {
label 'master'
customWorkspace "workspace/clai/${env.BRANCH_NAME}/${env.BUILD_ID}"
}
}
options {
ansiColor('css')
}
environment {
RANDOM_NAME=sh(
script: "cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 8 | head -n 1",
returnStdout: true
).trim()
COMMON_NAME="${env.RANDOM_NAME}_${env.BUILD_ID}"
IMAGE_NAME="clai_tstimg_${env.COMMON_NAME}"
TEST_OUTPUT_FILENAME="pytest.out"
OUTFILE_PATH="${env.WORKSPACE}/${env.TEST_OUTPUT_FILENAME}"
}
stages {
stage('begin') {
steps {
script{
// Does an image with the given name already exist?
// If not, create it.
IMAGE_ID = getImageID(env.IMAGE_NAME)
if(!IMAGE_ID){
sh """
sudo DOCKER_BUILD_FLAGS='' \
CLAI_DOCKER_IMAGE_NAME=${env.IMAGE_NAME} \
CLAI_DOCKER_JENKINSBUILD='true' \
${env.WORKSPACE}/BuildDockerImage.sh
"""
echo "Image ID: ${IMAGE_ID}"
}
echo "'begin' step complete"
}
}
}
stage ('test') {
steps {
script{
CONTAINER_NAME = getContainerName(env.IMAGE_NAME)
sh """
sudo CLAI_DOCKER_IMAGE_NAME=${env.IMAGE_NAME} \
CLAI_DOCKER_CONTAINER_NAME=${CONTAINER_NAME} \
CLAI_BASEDIR=${env.WORKSPACE} \
CLAI_DOCKER_JENKINSBUILD='true' \
CLAI_DOCKER_OUTPUT='${env.TEST_OUTPUT_FILENAME}' \
${env.WORKSPACE}/RunDockerImage.sh
"""
def numErrors = getNumberOfErrors(env.TEST_OUTPUT_FILENAME)
sh "exit ${numErrors}"
}
}
}
}
post {
success {
echo 'Build successful'
commonCleanupProcedures()
}
failure {
echo 'Build failed!'
commonCleanupProcedures()
}
}
}
def getImageID(String imgName){
IMAGE_ID = sh (
script: "sudo docker image ls -q ${imgName}",
returnStdout: true
).trim()
RTN_VLU = (IMAGE_ID == "") ? null : IMAGE_ID
echo "getImageID(${imgName}) returns: ${RTN_VLU}"
return RTN_VLU
}
def getContainerID(String ctrName){
CONTAINER_ID = sh (
script: "sudo docker container ls -aq --filter name=${ctrName}",
returnStdout: true
).trim()
RTN_VLU = (CONTAINER_ID == "") ? null : CONTAINER_ID
echo "getContainerID(${ctrName}) returns: ${RTN_VLU}"
return RTN_VLU
}
def getContainerName(String imgName){
CONTAINER_NAME = sh (
script: "echo ${env.IMAGE_NAME} | sed -e 's/tstimg/ctrimg/g'",
returnStdout: true,
encoding: 'UTF-8'
).trim()
RTN_VLU = (CONTAINER_NAME == "") ? null : CONTAINER_NAME
echo "getContainerName(${imgName}) returns: ${RTN_VLU}"
return RTN_VLU
}
def cleanupBuild(){
CONTAINER_NAME = getContainerName(env.IMAGE_NAME)
CONTAINER_ID = getContainerID(CONTAINER_NAME)
if(CONTAINER_ID){
sh"""
sudo docker container stop ${CONTAINER_ID}
sudo docker container rm ${CONTAINER_ID}
"""
}
IMAGE_ID = getImageID(env.IMAGE_NAME)
if(IMAGE_ID){
sh"sudo docker image rm ${IMAGE_ID}"
}
}
def doesOutputFileExist(){
OUTFILE_EXISTS = sh (
script: "if [ -f ${env.OUTFILE_PATH} ]; then echo 'True'; else echo 'False'; fi",
returnStdout: true
).trim()
return (OUTFILE_EXISTS == "True") ? true : false
}
def getNumberOfErrors(String resultsFile){
if(doesOutputFileExist()){
NUM_ERRORS = sh (
script: "cat ${resultsFile} \
| grep -o '[0-9]** error in [0-9]*\\.[0-9]* seconds =========' \
| cut -d' ' -f1",
returnStdout: true
).trim()
return NUM_ERRORS.toInteger()
}
else{
return -1
}
}
def commonCleanupProcedures(){
if(doesOutputFileExist()){
def numErrors = getNumberOfErrors(env.OUTFILE_PATH)
if(numErrors > 0){
sh "cat ${env.OUTFILE_PATH}"
}
echo "Automated testing completed with ${numErrors} errors"
echo "Test results are in ${env.OUTFILE_PATH}"
}
cleanupBuild()
}