82bafff38e49beec046e3ad1b973c6a5f0ee757e
[asterisk/asterisk.git] / tests / CI / unittests.jenkinsfile
1 /*
2  * This pipeline is the "template" for the Asterisk Unit 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: 1,
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('^recheck$'),
36                                 patchsetCreated(excludeDrafts: false,
37                                                                 excludeNoCodeChange: true,
38                                                                 excludeTrivialRebase: false),
39                                 draftPublished()
40                         ],
41                         skipVote: [
42                                 onFailed: false,
43                                 onNotBuilt: true,
44                                 onSuccessful: false,
45                                 onUnstable: false
46                         ]
47         }
48         agent {
49                 /* All of the stages need to be performed on a docker host */
50                 label "swdev-docker"
51         }
52
53         stages {
54                 stage ("->") {
55                         /*
56                          * Jenkins will try to automatically rebuild this job when
57                          * the jenkinsfile changes but since this job is dependent on
58                          * Gerrit, we really don't want to do anything in that case.
59                          */
60                         when {
61                                 not { environment name: 'GERRIT_CHANGE_NUMBER', value: '' }
62                                 not { environment name: 'GERRIT_EVENT_ACCOUNT_NAME', value: 'Jenkins2' }
63                         }
64                         steps {
65                                 script {
66                                         manager.build.displayName = "${env.GERRIT_CHANGE_NUMBER}"
67                                         manager.createSummary("/plugin/workflow-job/images/48x48/pipelinejob.png").appendText("Docker Host: ${NODE_NAME}", false)
68
69                                         stage ("Checkout") {
70                                                 sh "sudo chown -R jenkins:users ."
71                                                 env.GERRIT_PROJECT_URL = env.GERRIT_CHANGE_URL.replaceAll(/\/[0-9]+$/, "/${env.GERRIT_PROJECT}")
72                                                 sh "printenv | sort"
73
74                                                 /*
75                                                  * Jenkins has already automatically checked out the base branch
76                                                  * for this change but we now need to check out the change itself
77                                                  * and rebase it on the current base branch.  If the rebase fails,
78                                                  * that's an indication to the user that they'll need to sort their
79                                                  * change out.
80                                                  *
81                                                  * The Gerrit Trigger provides all the URLs and refspecs to
82                                                  * check out the change.
83                                                  */
84                                                 checkout scm: [$class: 'GitSCM',
85                                                         branches: [[name: env.GERRIT_BRANCH ]],
86                                                         extensions: [
87                                                                 [$class: 'ScmName', name: 'gerrit-public'],
88                                                                 [$class: 'CleanBeforeCheckout'],
89                                                                 [$class: 'PreBuildMerge', options: [
90                                                                         mergeRemote: 'gerrit-public',
91                                                                         fastForwardMode: 'NO_FF',
92                                                                         mergeStrategy: 'RECURSIVE',
93                                                                         mergeTarget: env.GERRIT_BRANCH]],
94                                                                 [$class: 'CloneOption',
95                                                                         honorRefspec: true,
96                                                                         noTags: true,
97                                                                         depth: 10,
98                                                                         shallow: true
99                                                                 ],
100                                                                 [$class: 'PruneStaleBranch'],
101                                                                 [$class: 'BuildChooserSetting',
102                                                                         buildChooser: [$class: 'GerritTriggerBuildChooser']
103                                                                 ]
104                                                         ],
105                                                         userRemoteConfigs: [
106                                                                 [name: env.GERRIT_NAME, refspec: env.GERRIT_REFSPEC, url: env.GERRIT_PROJECT_URL ]
107                                                         ]
108                                                 ]
109
110                                                 sh "sudo tests/CI/setupJenkinsEnvironment.sh"
111                                         }
112
113                                         def images = env.DOCKER_IMAGES.split(' ')
114                                         def r = currentBuild.startTimeInMillis % images.length
115                                         def ri = images[(int)r]
116                                         def randomImage = env.DOCKER_REGISTRY + "/" + ri;
117                                         def bt = env.BUILD_TAG.replaceAll(/[^a-zA-Z0-9_.-]/, '-')
118                                         def dockerOptions = "--privileged --ulimit core=0 --ulimit nofile=10240 " +
119                                                 " -v /srv/jenkins:/srv/jenkins:rw -v /srv/cache:/srv/cache:rw " +
120                                                 " --entrypoint='' --name ${bt}-build"
121                                         def outputdir = "tests/CI/output/UnitTests"
122
123                                         manager.createSummary("/plugin/workflow-job/images/48x48/pipelinejob.png").appendText("Docker Image: ${randomImage}", false)
124                                         def img = docker.image(randomImage)
125                                         img.pull()
126                                         img.inside(dockerOptions) {
127
128                                                 stage ('Build') {
129                                                         echo 'Building..'
130
131                                                         sh "./tests/CI/buildAsterisk.sh --branch-name=${BRANCH_NAME} --output-dir=${outputdir} --cache-dir=/srv/cache"
132
133                                                         archiveArtifacts allowEmptyArchive: true, defaultExcludes: false, fingerprint: false,
134                                                                 artifacts: "${outputdir}/*"
135                                                 }
136
137                                                 stage ('Test') {
138                                                         def outputfile = "${outputdir}/unittests-results.xml"
139                                                         def testcmd = "test execute all"
140
141                                                         sh "sudo ./tests/CI/installAsterisk.sh --uninstall-all --branch-name=${BRANCH_NAME} --user-group=jenkins:users"
142
143                                                         sh "tests/CI/runUnittests.sh --user-group=jenkins:users --output-dir='${outputdir}' --output-xml='${outputfile}' --unittest-command='${testcmd}'"
144
145                                                         archiveArtifacts allowEmptyArchive: true, defaultExcludes: false, fingerprint: true,
146                                                                 artifacts: "${outputdir}/**"
147
148                                                         junit testResults: outputfile,
149                                                                 healthScaleFactor: 1.0,
150                                                                 keepLongStdio: true
151                                                 }
152                                         }
153                                 }
154                         }
155                 }
156         }
157         post {
158                 cleanup {
159                         sh "sudo make distclean 2&>/dev/null || : "
160                         sh "sudo rm -rf tests/CI/output 2&>/dev/null || : "
161                 }
162                 /*
163                  * The Gerrit Trigger will automatically post the "Verified" results back
164                  * to Gerrit but the verification publisher publishes extra stuff in the
165                  * "Code Review" section of the review.
166                  */
167                 always {
168                         script {
169                                 def cat
170                                 def comment
171                                 def rvalue
172                                 switch (currentBuild.currentResult) {
173                                         case ~/^SUCCESS$/:
174                                                 cat = "Passed"
175                                                 comment = ""
176                                                 rvalue = 1
177                                                 break
178                                         case ~/^FAILURE$/:
179                                                 cat = "Failed"
180                                                 comment = "Fatal Error"
181                                                 rvalue = -1
182                                                 break
183                                         case ~/^UNSTABLE$/:
184                                                 cat = "Failed"
185                                                 comment = "Tests Failed"
186                                                 rvalue = -1
187                                                 break
188                                 }
189
190                                 gerritverificationpublisher verifyStatusValue: rvalue,
191                                         verifyStatusCategory: cat, verifyStatusURL: '',
192                                         verifyStatusComment: comment, verifyStatusName: '',
193                                         verifyStatusReporter: 'Jenkins2', verifyStatusRerun: 'regate'
194                         }
195                 }
196                 success {
197                         echo "Reporting ${currentBuild.currentResult} Passed"
198                 }
199                 failure {
200                         echo "Reporting ${currentBuild.currentResult}: Failed: Fatal Error"
201                 }
202                 unstable {
203                         echo "Reporting ${currentBuild.currentResult}: Failed: Tests Failed"
204                 }
205         }
206 }