Merge "res/res_pjsip_sdp_rtp: put rtcp-mux in answer only if offered"
[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 pipeline {
14         triggers {
15                 /*
16                  * This trigger will match either the "asterisk" or "Security-asterisk"
17                  * projects.  The branch is taken from the branch this job was created
18                  * for.
19                  */
20                 gerrit customUrl: '',
21                         commentTextParameterMode: 'PLAIN',
22                         commitMessageParameterMode: 'PLAIN',
23                         gerritBuildSuccessfulVerifiedValue: 2,
24                         gerritBuildFailedVerifiedValue: -1,
25                         gerritBuildUnstableVerifiedValue: -1,
26                         gerritProjects: [
27                                 [branches: [[compareType: 'PLAIN', pattern: "${BRANCH_NAME}"]],
28                                         compareType: 'REG_EXP',
29                                         disableStrictForbiddenFileVerification: false,
30                                         pattern: '^(Security-)?asterisk.*'
31                                 ]
32                         ],
33                         silentMode: false,
34                         triggerOnEvents: [
35                                 commentAddedContains('^regate$'),
36                                 commentAdded(commentAddedTriggerApprovalValue: '+2', verdictCategory: 'Code-Review'),
37                         ],
38                         skipVote: [
39                                 onFailed: false,
40                                 onNotBuilt: true,
41                                 onSuccessful: false,
42                                 onUnstable: false
43                         ]
44         }
45         
46         agent {
47                 /* All of the stages need to be performed on a docker host */
48                 label "swdev-docker"
49         }
50
51         stages {
52                 stage ("->") {
53                         /*
54                          * Jenkins will try to automatically rebuild this job when
55                          * the jenkinsfile changes but since this job is dependent on
56                          * Gerrit, we really don't want to do anything in that case.
57                          */
58                         when {
59                                 not { environment name: 'GERRIT_CHANGE_NUMBER', value: '' }
60                                 not { environment name: 'GERRIT_EVENT_ACCOUNT_NAME', value: 'Jenkins2' }
61                         }
62                         steps {
63                                 /* Here's where we switch to scripted pipeline */
64                                 script {
65                                         manager.build.displayName = "${env.GERRIT_CHANGE_NUMBER}"
66                                         manager.createSummary("/plugin/workflow-job/images/48x48/pipelinejob.png").appendText("Docker Host: ${NODE_NAME}", false)
67
68                                         stage ("Checkout") {
69                                                 sh "sudo chown -R jenkins:users ."  
70                                                 env.GERRIT_PROJECT_URL = env.GERRIT_CHANGE_URL.replaceAll(/\/[0-9]+$/, "/${env.GERRIT_PROJECT}")
71                                                 sh "printenv | sort"
72                                         
73                                                 /*
74                                                  * Jenkins has already automatically checked out the base branch
75                                                  * for this change but we now need to check out the change itself
76                                                  * and rebase it on the current base branch.  If the rebase fails,
77                                                  * that's an indication to the user that they'll need to sort their
78                                                  * change out.
79                                                  *
80                                                  * The Gerrit Trigger provides all the URLs and refspecs to
81                                                  * check out the change.
82                                                  */
83                                                 checkout scm: [$class: 'GitSCM',
84                                                         branches: [[name: env.GERRIT_BRANCH ]],
85                                                         extensions: [
86                                                                 [$class: 'ScmName', name: 'gerrit-public'],
87                                                                 [$class: 'CleanBeforeCheckout'],
88                                                                 [$class: 'PreBuildMerge', options: [
89                                                                         mergeRemote: 'gerrit-public',
90                                                                         fastForwardMode: 'NO_FF',
91                                                                         mergeStrategy: 'RECURSIVE',
92                                                                         mergeTarget: env.GERRIT_BRANCH]],
93                                                                 [$class: 'CloneOption',
94                                                                         honorRefspec: true,
95                                                                         noTags: true,
96                                                                         depth: 10,
97                                                                         shallow: true
98                                                                 ],
99                                                                 [$class: 'PruneStaleBranch'],
100                                                                 [$class: 'BuildChooserSetting',
101                                                                         buildChooser: [$class: 'GerritTriggerBuildChooser']
102                                                                 ]
103                                                         ],
104                                                         userRemoteConfigs: [
105                                                                 [name: env.GERRIT_NAME, refspec: env.GERRIT_REFSPEC, url: env.GERRIT_PROJECT_URL ]
106                                                         ]
107                                                 ]
108
109                                                 sh "sudo tests/CI/setupJenkinsEnvironment.sh"
110                                         }
111
112                                         def images = env.DOCKER_IMAGES.split(' ')
113                                         def r = currentBuild.startTimeInMillis % images.length
114                                         def ri = images[(int)r]
115                                         def randomImage = env.DOCKER_REGISTRY + "/" + ri
116                                         def dockerOptions = "--privileged --ulimit core=0 --ulimit nofile=10240 " +
117                                                 " -v /srv/jenkins:/srv/jenkins:rw -v /srv/cache:/srv/cache:rw " +
118                                                 " --entrypoint=''"
119                                         def bt = env.BUILD_TAG.replaceAll(/[^a-zA-Z0-9_.-]/, '-')
120                                         def outputdir = "tests/CI/output/Testsuite"
121
122                                         manager.createSummary("/plugin/workflow-job/images/48x48/pipelinejob.png").appendText("Docker Image: ${randomImage}", false)
123                                         def img = docker.image(randomImage)
124                                         img.pull()
125
126                                         stage ("Build") {
127                                                 img.inside(dockerOptions + " --name ${bt}-build") {
128                                                         echo 'Building..'
129                                                         env.CCACHE_DIR = "/srv/cache/ccache"
130                                                         sh "./tests/CI/buildAsterisk.sh --branch-name=${BRANCH_NAME} --output-dir=${outputdir} --cache-dir=/srv/cache"
131
132                                                         archiveArtifacts allowEmptyArchive: true, defaultExcludes: false, fingerprint: false,
133                                                                 artifacts: "${outputdir}/*"
134                                                 }
135                                         }
136
137                                         def testGroups = readJSON file: "tests/CI/gateTestGroups.json"
138                                         def parallelTasks = [ : ]
139
140                                         for (def testGroup in testGroups) {
141                                                 /*
142                                                  * Because each task is a Groovy closure, we need to
143                                                  * keep local references to some variables.
144                                                  */
145                                                 def groupName = testGroup.name
146                                                 def groupDir = testGroup.dir
147                                                 def groupTestcmd = testGroup.testcmd
148                                                 def testsuiteUrl = env.GERRIT_PROJECT_URL.replaceAll(/\/(Security-)?[^\/]+$/, "/\$1testsuite")
149
150                                                 parallelTasks[groupName] = {
151                                                         stage (groupName) {
152
153                                                                 img.inside("${dockerOptions} --name ${bt}-${groupName}") {
154
155                                                                         lock("${JOB_NAME}.${NODE_NAME}.installer") {
156                                                                                 sh "sudo ./tests/CI/installAsterisk.sh --uninstall-all --branch-name=${BRANCH_NAME} --user-group=jenkins:users"
157                                                                         }
158
159                                                                         sh "sudo rm -rf ${groupDir} || : "
160                                                                         
161                                                                         checkout scm: [$class: 'GitSCM',
162                                                                                 branches: [[name: "${BRANCH_NAME}"]],
163                                                                                         extensions: [
164                                                                                                 [$class: 'RelativeTargetDirectory', relativeTargetDir: groupDir],
165                                                                                                 [$class: 'CloneOption',
166                                                                                                         noTags: true,
167                                                                                                         depth: 10,
168                                                                                                         honorRefspec: true,
169                                                                                                         shallow: true
170                                                                                                 ],
171                                                                                         ],
172                                                                                         userRemoteConfigs: [[name: env.GERRIT_NAME, url: testsuiteUrl]]
173                                                                                 ]
174
175                                                                         sh "sudo tests/CI/runTestsuite.sh --testsuite-dir='${groupDir}' --testsuite-command='${groupTestcmd}'"
176
177                                                                         archiveArtifacts allowEmptyArchive: true, defaultExcludes: false, fingerprint: true,
178                                                                                 artifacts: "${groupDir}/asterisk-test-suite-report.xml, ${groupDir}/logs/**, ${groupDir}/core*.txt"
179
180                                                                         junit testResults: "${groupDir}/asterisk-test-suite-report.xml",
181                                                                                 healthScaleFactor: 1.0,
182                                                                                 keepLongStdio: true
183
184                                                                         echo "Group result d: ${currentBuild.currentResult}"
185                                                                 }
186                                                                 echo "Group result s: ${currentBuild.currentResult}"
187                                                         }
188                                                 }
189                                         }
190                                         parallel parallelTasks
191                                 }
192                         }
193                 }
194         }
195         post {
196                 cleanup {
197                         sh "sudo make distclean 2&>/dev/null || : "
198                         sh "sudo rm -rf tests/CI/output  2&>/dev/null || : "
199                 }
200                 /*
201                  * The Gerrit Trigger will automatically post the "Verified" results back
202                  * to Gerrit but the verification publisher publishes extra stuff in the
203                  * "Code Review" section of the review.
204                  */
205                 always {
206                         script {
207                                 def cat
208                                 def comment
209                                 def rvalue
210                                 switch (currentBuild.currentResult) {
211                                         case ~/^SUCCESS$/:
212                                                 cat = "Passed"
213                                                 comment = ""
214                                                 rvalue = 2
215                                                 break
216                                         case ~/^FAILURE$/:
217                                                 cat = "Failed"
218                                                 comment = "Fatal Error"
219                                                 rvalue = -1
220                                                 break
221                                         case ~/^UNSTABLE$/:
222                                                 cat = "Failed"
223                                                 comment = "Tests Failed"
224                                                 rvalue = -1
225                                                 break
226                                 }
227
228                                 gerritverificationpublisher verifyStatusValue: rvalue,
229                                         verifyStatusCategory: cat, verifyStatusURL: '',
230                                         verifyStatusComment: comment, verifyStatusName: '',
231                                         verifyStatusReporter: 'Jenkins2', verifyStatusRerun: 'regate'
232                         }
233                 }
234                 success {
235                         echo "Reporting ${currentBuild.currentResult} Passed"
236                 }
237                 failure {
238                         echo "Reporting ${currentBuild.currentResult}: Failed: Fatal Error"
239                 }
240                 unstable {
241                         echo "Reporting ${currentBuild.currentResult}: Failed: Tests Failed"
242                 }
243         }
244 }