bc23781d6fc8d0aedb1313982feff09ac42ecc42
[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 = readJSON file: "tests/CI/gateTestGroups.json"
163                                         def parallelTasks = [ : ]
164
165                                         for (def testGroup in testGroups) {
166                                                 /*
167                                                  * Because each task is a Groovy closure, we need to
168                                                  * keep local references to some variables.
169                                                  */
170                                                 def groupName = testGroup.name
171                                                 def groupDir = testGroup.dir
172                                                 def groupTestcmd = testGroup.testcmd
173                                                 def testsuiteUrl = env.GERRIT_PROJECT_URL.replaceAll(/\/(Security-)?[^\/]+$/, "/\$1testsuite")
174
175                                                 parallelTasks[groupName] = {
176                                                         stage (groupName) {
177
178                                                                 img.inside("${dockerOptions} --name ${bt}-${groupName}") {
179
180                                                                         lock("${JOB_NAME}.${NODE_NAME}.installer") {
181                                                                                 sh "sudo ./tests/CI/installAsterisk.sh --uninstall-all --branch-name=${BRANCH_NAME} --user-group=jenkins:users"
182                                                                         }
183
184                                                                         sh "sudo rm -rf ${groupDir} || : "
185                                                                         
186                                                                         withCredentials([usernamePassword(credentialsId: "${JENKINS_GERRIT_CREDS}",
187                                                                                 passwordVariable: 'GERRIT_USER_PW', usernameVariable: 'GERRIT_USER_NAME')]) {
188                                                                                 checkout scm: [$class: 'GitSCM',
189                                                                                         branches: [[name: "${BRANCH_NAME}"]],
190                                                                                         extensions: [
191                                                                                                 [$class: 'RelativeTargetDirectory', relativeTargetDir: groupDir],
192                                                                                                 [$class: 'CloneOption',
193                                                                                                         noTags: true,
194                                                                                                         depth: 10,
195                                                                                                         honorRefspec: true,
196                                                                                                         shallow: true
197                                                                                                 ],
198                                                                                         ],
199                                                                                         userRemoteConfigs: [
200                                                                                                 [
201                                                                                                 credentialsId: env.JENKINS_GERRIT_CREDS,
202                                                                                                 name: env.GERRIT_NAME,
203                                                                                                 url: testsuiteUrl.replaceAll("http(s)?://", "http\$1://${GERRIT_USER_NAME}@")
204                                                                                                 ]
205                                                                                         ]
206                                                                                 ]
207                                                                         }
208
209                                                                         sh "sudo tests/CI/runTestsuite.sh --testsuite-dir='${groupDir}' --testsuite-command='${groupTestcmd}'"
210
211                                                                         archiveArtifacts allowEmptyArchive: true, defaultExcludes: false, fingerprint: true,
212                                                                                 artifacts: "${groupDir}/asterisk-test-suite-report.xml, ${groupDir}/logs/**, ${groupDir}/core*.txt"
213
214                                                                         junit testResults: "${groupDir}/asterisk-test-suite-report.xml",
215                                                                                 healthScaleFactor: 1.0,
216                                                                                 keepLongStdio: true
217
218                                                                         echo "Group result d: ${currentBuild.currentResult}"
219                                                                 }
220                                                                 echo "Group result s: ${currentBuild.currentResult}"
221                                                         }
222                                                 }
223                                         }
224                                         parallel parallelTasks
225                                 }
226                         }
227                 }
228         }
229         post {
230                 cleanup {
231                         sh "sudo make distclean >/dev/null 2>&1 || : "
232                         sh "sudo rm -rf tests/CI/output >/dev/null 2>&1 || : "
233                 }
234                 /*
235                  * The Gerrit Trigger will automatically post the "Verified" results back
236                  * to Gerrit but the verification publisher publishes extra stuff in the
237                  * "Code Review" section of the review.
238                  */
239                 always {
240                         script {
241                                 def cat
242                                 def comment
243                                 def rvalue
244                                 switch (currentBuild.currentResult) {
245                                         case ~/^SUCCESS$/:
246                                                 cat = "Passed"
247                                                 comment = ""
248                                                 rvalue = 2
249                                                 break
250                                         case ~/^FAILURE$/:
251                                                 cat = "Failed"
252                                                 comment = "Fatal Error"
253                                                 rvalue = -1
254                                                 break
255                                         case ~/^UNSTABLE$/:
256                                                 cat = "Failed"
257                                                 comment = "Tests Failed"
258                                                 rvalue = -1
259                                                 break
260                                 }
261
262                                 gerritverificationpublisher verifyStatusValue: rvalue,
263                                         verifyStatusCategory: cat, verifyStatusURL: '',
264                                         verifyStatusComment: comment, verifyStatusName: '',
265                                         verifyStatusReporter: 'Jenkins2', verifyStatusRerun: 'regate'
266                         }
267                 }
268                 success {
269                         echo "Reporting ${currentBuild.currentResult} Passed"
270                 }
271                 failure {
272                         echo "Reporting ${currentBuild.currentResult}: Failed: Fatal Error"
273                 }
274                 unstable {
275                         echo "Reporting ${currentBuild.currentResult}: Failed: Tests Failed"
276                 }
277         }
278 }