c23114b211a464c22381dd585fed6e8eed07ebae
[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                                                  * We need to retrieve the jenkins2 gerrit https credentials
84                                                  * in case this review is in a restricted project.
85                                                  */
86                                                 withCredentials([usernamePassword(credentialsId: env.JENKINS_GERRIT_CREDS,
87                                                         usernameVariable: 'gerrit_user')]) {
88
89                                                         checkout scm: [$class: 'GitSCM',
90                                                                 branches: [[name: env.GERRIT_BRANCH ]],
91                                                                 extensions: [
92                                                                         [$class: 'ScmName', name: 'gerrit-public'],
93                                                                         [$class: 'CleanBeforeCheckout'],
94                                                                         [$class: 'PreBuildMerge', options: [
95                                                                                 mergeRemote: 'gerrit-public',
96                                                                                 fastForwardMode: 'NO_FF',
97                                                                                 mergeStrategy: 'RECURSIVE',
98                                                                                 mergeTarget: env.GERRIT_BRANCH]],
99                                                                         [$class: 'CloneOption',
100                                                                                 honorRefspec: true,
101                                                                                 noTags: true,
102                                                                                 depth: 10,
103                                                                                 shallow: true
104                                                                         ],
105                                                                         [$class: 'PruneStaleBranch'],
106                                                                         [$class: 'BuildChooserSetting',
107                                                                                 buildChooser: [$class: 'GerritTriggerBuildChooser']
108                                                                         ]
109                                                                 ],
110                                                                 userRemoteConfigs: [
111                                                                         [
112                                                                         credentialsId: env.JENKINS_GERRIT_CREDS,
113                                                                         name: env.GERRIT_NAME,
114                                                                         refspec: env.GERRIT_REFSPEC,
115                                                                         url: env.GERRIT_PROJECT_URL.replaceAll("http(s)?://", "http\$1://${gerrit_user}@")
116                                                                         ]
117                                                                 ]
118                                                         ]
119                                                 }
120                                                 sh "sudo tests/CI/setupJenkinsEnvironment.sh"
121                                         }
122
123                                         def images = env.DOCKER_IMAGES.split(' ')
124                                         def r = currentBuild.startTimeInMillis % images.length
125                                         def ri = images[(int)r]
126                                         def randomImage = env.DOCKER_REGISTRY + "/" + ri
127                                         def dockerOptions = "--privileged --ulimit core=0 --ulimit nofile=10240 " +
128                                                 " -v /srv/jenkins:/srv/jenkins:rw -v /srv/cache:/srv/cache:rw " +
129                                                 " --entrypoint=''"
130                                         def bt = env.BUILD_TAG.replaceAll(/[^a-zA-Z0-9_.-]/, '-')
131                                         def outputdir = "tests/CI/output/Testsuite"
132
133                                         manager.createSummary("/plugin/workflow-job/images/48x48/pipelinejob.png").appendText("Docker Image: ${randomImage}", false)
134                                         def img = docker.image(randomImage)
135                                         img.pull()
136
137                                         stage ("Build") {
138                                                 img.inside(dockerOptions + " --name ${bt}-build") {
139                                                         echo 'Building..'
140                                                         env.CCACHE_DIR = "/srv/cache/ccache"
141                                                         sh "./tests/CI/buildAsterisk.sh --branch-name=${BRANCH_NAME} --output-dir=${outputdir} --cache-dir=/srv/cache"
142
143                                                         archiveArtifacts allowEmptyArchive: true, defaultExcludes: false, fingerprint: false,
144                                                                 artifacts: "${outputdir}/*"
145                                                 }
146                                         }
147
148                                         def testGroups = readJSON file: "tests/CI/gateTestGroups.json"
149                                         def parallelTasks = [ : ]
150
151                                         for (def testGroup in testGroups) {
152                                                 /*
153                                                  * Because each task is a Groovy closure, we need to
154                                                  * keep local references to some variables.
155                                                  */
156                                                 def groupName = testGroup.name
157                                                 def groupDir = testGroup.dir
158                                                 def groupTestcmd = testGroup.testcmd
159                                                 def testsuiteUrl = env.GERRIT_PROJECT_URL.replaceAll(/\/(Security-)?[^\/]+$/, "/\$1testsuite")
160
161                                                 parallelTasks[groupName] = {
162                                                         stage (groupName) {
163
164                                                                 img.inside("${dockerOptions} --name ${bt}-${groupName}") {
165
166                                                                         lock("${JOB_NAME}.${NODE_NAME}.installer") {
167                                                                                 sh "sudo ./tests/CI/installAsterisk.sh --uninstall-all --branch-name=${BRANCH_NAME} --user-group=jenkins:users"
168                                                                         }
169
170                                                                         sh "sudo rm -rf ${groupDir} || : "
171                                                                         
172                                                                         checkout scm: [$class: 'GitSCM',
173                                                                                 branches: [[name: "${BRANCH_NAME}"]],
174                                                                                         extensions: [
175                                                                                                 [$class: 'RelativeTargetDirectory', relativeTargetDir: groupDir],
176                                                                                                 [$class: 'CloneOption',
177                                                                                                         noTags: true,
178                                                                                                         depth: 10,
179                                                                                                         honorRefspec: true,
180                                                                                                         shallow: true
181                                                                                                 ],
182                                                                                         ],
183                                                                                         userRemoteConfigs: [[name: env.GERRIT_NAME, url: testsuiteUrl]]
184                                                                                 ]
185
186                                                                         sh "sudo tests/CI/runTestsuite.sh --testsuite-dir='${groupDir}' --testsuite-command='${groupTestcmd}'"
187
188                                                                         archiveArtifacts allowEmptyArchive: true, defaultExcludes: false, fingerprint: true,
189                                                                                 artifacts: "${groupDir}/asterisk-test-suite-report.xml, ${groupDir}/logs/**, ${groupDir}/core*.txt"
190
191                                                                         junit testResults: "${groupDir}/asterisk-test-suite-report.xml",
192                                                                                 healthScaleFactor: 1.0,
193                                                                                 keepLongStdio: true
194
195                                                                         echo "Group result d: ${currentBuild.currentResult}"
196                                                                 }
197                                                                 echo "Group result s: ${currentBuild.currentResult}"
198                                                         }
199                                                 }
200                                         }
201                                         parallel parallelTasks
202                                 }
203                         }
204                 }
205         }
206         post {
207                 cleanup {
208                         sh "sudo make distclean 2&>/dev/null || : "
209                         sh "sudo rm -rf tests/CI/output  2&>/dev/null || : "
210                 }
211                 /*
212                  * The Gerrit Trigger will automatically post the "Verified" results back
213                  * to Gerrit but the verification publisher publishes extra stuff in the
214                  * "Code Review" section of the review.
215                  */
216                 always {
217                         script {
218                                 def cat
219                                 def comment
220                                 def rvalue
221                                 switch (currentBuild.currentResult) {
222                                         case ~/^SUCCESS$/:
223                                                 cat = "Passed"
224                                                 comment = ""
225                                                 rvalue = 2
226                                                 break
227                                         case ~/^FAILURE$/:
228                                                 cat = "Failed"
229                                                 comment = "Fatal Error"
230                                                 rvalue = -1
231                                                 break
232                                         case ~/^UNSTABLE$/:
233                                                 cat = "Failed"
234                                                 comment = "Tests Failed"
235                                                 rvalue = -1
236                                                 break
237                                 }
238
239                                 gerritverificationpublisher verifyStatusValue: rvalue,
240                                         verifyStatusCategory: cat, verifyStatusURL: '',
241                                         verifyStatusComment: comment, verifyStatusName: '',
242                                         verifyStatusReporter: 'Jenkins2', verifyStatusRerun: 'regate'
243                         }
244                 }
245                 success {
246                         echo "Reporting ${currentBuild.currentResult} Passed"
247                 }
248                 failure {
249                         echo "Reporting ${currentBuild.currentResult}: Failed: Fatal Error"
250                 }
251                 unstable {
252                         echo "Reporting ${currentBuild.currentResult}: Failed: Tests Failed"
253                 }
254         }
255 }