CI: Move test group config files to Jenkins
[asterisk/asterisk.git] / tests / CI / gates.jenkinsfile
1 /*
2  * This pipeline is the "template" for the Asterisk Gate Tests multi-branch
3  * parent job.  Jenkins will automatically scan the branches in the "asterisk"
4  * or "Security-asterisk" projects in Gerrit and automatically create a branch-
5  * specific job for each branch it finds this file in.
6  *
7  * This file starts as a declarative pipeline because with a declarative
8  * pipeline, you can define the trigger in the pipeline file.  This keeps
9  * everything in one place.  We transition to scripted pipeline later on because
10  * we need to dynamically determine which docker image we're going to use and
11  * you can't do that in a delcarative pipeline.
12  */
13 def timeoutTime = 60
14 def timeoutUnits = 'MINUTES'
15 if (env.TIMEOUT_GATES) {
16         def _timeout = env.TIMEOUT_GATES.split()
17         timeoutTime = _timeout[0].toInteger()
18         timeoutUnits = _timeout[1]
19 }
20
21 pipeline {
22         options {
23                 timestamps()
24                 timeout(time: timeoutTime, unit: timeoutUnits)
25         }
26         triggers {
27                 /*
28                  * This trigger will match either the "asterisk" or "Security-asterisk"
29                  * projects.  The branch is taken from the branch this job was created
30                  * for.
31                  */
32                 gerrit customUrl: '',
33                         commentTextParameterMode: 'PLAIN',
34                         commitMessageParameterMode: 'PLAIN',
35                         gerritBuildSuccessfulVerifiedValue: 2,
36                         gerritBuildFailedVerifiedValue: -1,
37                         gerritBuildUnstableVerifiedValue: -1,
38                         gerritProjects: [
39                                 [branches: [[compareType: 'PLAIN', pattern: "${BRANCH_NAME}"]],
40                                         compareType: 'REG_EXP',
41                                         disableStrictForbiddenFileVerification: false,
42                                         pattern: '^(Security-)?asterisk.*'
43                                 ]
44                         ],
45                         silentMode: false,
46                         triggerOnEvents: [
47                                 commentAddedContains('^regate$'),
48                                 commentAdded(commentAddedTriggerApprovalValue: '+2', verdictCategory: 'Code-Review'),
49                         ],
50                         skipVote: [
51                                 onFailed: false,
52                                 onNotBuilt: true,
53                                 onSuccessful: false,
54                                 onUnstable: false
55                         ]
56         }
57         
58         agent {
59                 /* All of the stages need to be performed on a docker host */
60                 label "swdev-docker"
61         }
62
63         stages {
64                 stage ("->") {
65                         /*
66                          * Jenkins will try to automatically rebuild this job when
67                          * the jenkinsfile changes but since this job is dependent on
68                          * Gerrit, we really don't want to do anything in that case.
69                          */
70                         when {
71                                 not { environment name: 'GERRIT_CHANGE_NUMBER', value: '' }
72                                 not { environment name: 'GERRIT_EVENT_ACCOUNT_NAME', value: 'Jenkins2' }
73                         }
74                         steps {
75                                 /* Here's where we switch to scripted pipeline */
76                                 script {
77                                         manager.build.displayName = "${env.GERRIT_CHANGE_NUMBER}"
78                                         manager.createSummary("/plugin/workflow-job/images/48x48/pipelinejob.png").appendText("Docker Host: ${NODE_NAME}", false)
79
80                                         stage ("Checkout") {
81                                                 sh "sudo chown -R jenkins:users ."  
82                                                 env.GERRIT_PROJECT_URL = env.GIT_URL.replaceAll(/[^\/]+$/, env.GERRIT_PROJECT)
83                                         
84                                                 /*
85                                                  * Jenkins has already automatically checked out the base branch
86                                                  * for this change but we now need to check out the change itself
87                                                  * and rebase it on the current base branch.  If the rebase fails,
88                                                  * that's an indication to the user that they'll need to sort their
89                                                  * change out.
90                                                  *
91                                                  * The Gerrit Trigger provides all the URLs and refspecs to
92                                                  * check out the change.
93                                                  *
94                                                  * We need to retrieve the jenkins2 gerrit https credentials
95                                                  * in case this review is in a restricted project.
96                                                  */
97                                                 withCredentials([usernamePassword(credentialsId: "${JENKINS_GERRIT_CREDS}",
98                                                         passwordVariable: 'GERRIT_USER_PW', usernameVariable: 'GERRIT_USER_NAME')]) {
99
100                                                         sh "printenv | sort"
101
102                                                         checkout scm: [$class: 'GitSCM',
103                                                                 branches: [[name: env.GERRIT_BRANCH ]],
104                                                                 extensions: [
105                                                                         [$class: 'ScmName', name: env.GERRIT_NAME],
106                                                                         [$class: 'CleanBeforeCheckout'],
107                                                                         [$class: 'PreBuildMerge', options: [
108                                                                                 mergeRemote: env.GERRIT_NAME,
109                                                                                 fastForwardMode: 'NO_FF',
110                                                                                 mergeStrategy: 'RECURSIVE',
111                                                                                 mergeTarget: env.GERRIT_BRANCH]],
112                                                                         [$class: 'CloneOption',
113                                                                                 honorRefspec: true,
114                                                                                 noTags: true,
115                                                                                 depth: 10,
116                                                                                 shallow: true
117                                                                         ],
118                                                                         [$class: 'PruneStaleBranch'],
119                                                                         [$class: 'BuildChooserSetting',
120                                                                                 buildChooser: [$class: 'GerritTriggerBuildChooser']
121                                                                         ]
122                                                                 ],
123                                                                 userRemoteConfigs: [
124                                                                         [
125                                                                         credentialsId: env.JENKINS_GERRIT_CREDS,
126                                                                         name: env.GERRIT_NAME,
127                                                                         refspec: env.GERRIT_REFSPEC,
128                                                                         url: env.GERRIT_PROJECT_URL.replaceAll("http(s)?://", "http\$1://${GERRIT_USER_NAME}@")
129                                                                         ]
130                                                                 ]
131                                                         ]
132                                                 }
133                                                 sh "sudo tests/CI/setupJenkinsEnvironment.sh"
134                                         }
135
136                                         def images = env.DOCKER_IMAGES.split(' ')
137                                         def r = currentBuild.startTimeInMillis % images.length
138                                         def ri = images[(int)r]
139                                         def randomImage = env.DOCKER_REGISTRY + "/" + ri
140                                         /* FYI... Jenkins takes care of mouting the workspace for the container */
141                                         def dockerOptions = "--privileged --ulimit core=0 --ulimit nofile=10240 " +
142                                                 " --tmpfs /tmp:exec,size=1G -v /srv/jenkins:/srv/jenkins:rw -v /srv/cache:/srv/cache:rw " +
143                                                 " --entrypoint=''"
144                                         def bt = env.BUILD_TAG.replaceAll(/[^a-zA-Z0-9_.-]/, '-')
145                                         def outputdir = "tests/CI/output/Testsuite"
146
147                                         manager.createSummary("/plugin/workflow-job/images/48x48/pipelinejob.png").appendText("Docker Image: ${randomImage}", false)
148                                         def img = docker.image(randomImage)
149                                         img.pull()
150
151                                         stage ("Build") {
152                                                 img.inside(dockerOptions + " --name ${bt}-build") {
153                                                         echo 'Building..'
154                                                         env.CCACHE_DIR = "/srv/cache/ccache"
155                                                         sh "./tests/CI/buildAsterisk.sh --branch-name=${BRANCH_NAME} --output-dir=${outputdir} --cache-dir=/srv/cache"
156
157                                                         archiveArtifacts allowEmptyArchive: true, defaultExcludes: false, fingerprint: false,
158                                                                 artifacts: "${outputdir}/*"
159                                                 }
160                                         }
161
162                                         def testGroups
163                                         configFileProvider([configFile(fileId: 'asterisk_gate_test_groups', variable: 'GATE_TEST_GROUPS')]) {
164                                         echo "Retrieved config file from ${env.GATE_TEST_GROUPS}"
165                                                 testGroups = readJSON file: env.GATE_TEST_GROUPS
166                                         }
167                                         echo "Running test groups:"
168                                         for (def testGroup in testGroups) {
169                                                 echo "${testGroup.name} ${testGroup.dir} ${testGroup.testcmd}"
170                                         }
171
172                                         def parallelTasks = [ : ]
173
174                                         for (def testGroup in testGroups) {
175                                                 /*
176                                                  * Because each task is a Groovy closure, we need to
177                                                  * keep local references to some variables.
178                                                  */
179                                                 def groupName = testGroup.name
180                                                 def groupDir = testGroup.dir
181                                                 def groupTestcmd = testGroup.testcmd
182                                                 def testsuiteUrl = env.GERRIT_PROJECT_URL.replaceAll(/\/(Security-)?[^\/]+$/, "/\$1testsuite")
183
184                                                 parallelTasks[groupName] = {
185                                                         stage (groupName) {
186
187                                                                 img.inside("${dockerOptions} --name ${bt}-${groupName}") {
188
189                                                                         lock("${JOB_NAME}.${NODE_NAME}.installer") {
190                                                                                 sh "sudo ./tests/CI/installAsterisk.sh --uninstall-all --branch-name=${BRANCH_NAME} --user-group=jenkins:users"
191                                                                         }
192
193                                                                         sh "sudo rm -rf ${groupDir} || : "
194                                                                         
195                                                                         withCredentials([usernamePassword(credentialsId: "${JENKINS_GERRIT_CREDS}",
196                                                                                 passwordVariable: 'GERRIT_USER_PW', usernameVariable: 'GERRIT_USER_NAME')]) {
197                                                                                 checkout scm: [$class: 'GitSCM',
198                                                                                         branches: [[name: "${BRANCH_NAME}"]],
199                                                                                         extensions: [
200                                                                                                 [$class: 'RelativeTargetDirectory', relativeTargetDir: groupDir],
201                                                                                                 [$class: 'CloneOption',
202                                                                                                         noTags: true,
203                                                                                                         depth: 10,
204                                                                                                         honorRefspec: true,
205                                                                                                         shallow: true
206                                                                                                 ],
207                                                                                         ],
208                                                                                         userRemoteConfigs: [
209                                                                                                 [
210                                                                                                 credentialsId: env.JENKINS_GERRIT_CREDS,
211                                                                                                 name: env.GERRIT_NAME,
212                                                                                                 url: testsuiteUrl.replaceAll("http(s)?://", "http\$1://${GERRIT_USER_NAME}@")
213                                                                                                 ]
214                                                                                         ]
215                                                                                 ]
216                                                                         }
217
218                                                                         sh "sudo tests/CI/runTestsuite.sh --testsuite-dir='${groupDir}' --testsuite-command='${groupTestcmd}'"
219
220                                                                         archiveArtifacts allowEmptyArchive: true, defaultExcludes: false, fingerprint: true,
221                                                                                 artifacts: "${groupDir}/asterisk-test-suite-report.xml, ${groupDir}/logs/**, ${groupDir}/core*.txt"
222
223                                                                         junit testResults: "${groupDir}/asterisk-test-suite-report.xml",
224                                                                                 healthScaleFactor: 1.0,
225                                                                                 keepLongStdio: true
226
227                                                                         echo "Group result d: ${currentBuild.currentResult}"
228                                                                 }
229                                                                 echo "Group result s: ${currentBuild.currentResult}"
230                                                         }
231                                                 }
232                                         }
233                                         parallel parallelTasks
234                                 }
235                         }
236                 }
237         }
238         post {
239                 cleanup {
240                         sh "sudo make distclean >/dev/null 2>&1 || : "
241                         sh "sudo rm -rf tests/CI/output >/dev/null 2>&1 || : "
242                 }
243                 /*
244                  * The Gerrit Trigger will automatically post the "Verified" results back
245                  * to Gerrit but the verification publisher publishes extra stuff in the
246                  * "Code Review" section of the review.
247                  */
248                 always {
249                         script {
250                                 def cat
251                                 def comment
252                                 def rvalue
253                                 switch (currentBuild.currentResult) {
254                                         case ~/^SUCCESS$/:
255                                                 cat = "Passed"
256                                                 comment = ""
257                                                 rvalue = 2
258                                                 break
259                                         case ~/^FAILURE$/:
260                                                 cat = "Failed"
261                                                 comment = "Fatal Error"
262                                                 rvalue = -1
263                                                 break
264                                         case ~/^UNSTABLE$/:
265                                                 cat = "Failed"
266                                                 comment = "Tests Failed"
267                                                 rvalue = -1
268                                                 break
269                                 }
270
271                                 gerritverificationpublisher verifyStatusValue: rvalue,
272                                         verifyStatusCategory: cat, verifyStatusURL: '',
273                                         verifyStatusComment: comment, verifyStatusName: '',
274                                         verifyStatusReporter: 'Jenkins2', verifyStatusRerun: 'regate'
275                         }
276                 }
277                 success {
278                         echo "Reporting ${currentBuild.currentResult} Passed"
279                 }
280                 failure {
281                         echo "Reporting ${currentBuild.currentResult}: Failed: Fatal Error"
282                 }
283                 unstable {
284                         echo "Reporting ${currentBuild.currentResult}: Failed: Tests Failed"
285                 }
286         }
287 }