Debian Codegraph @

tests/CI/unittests.jenkinsfile

raw
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 declarative pipeline.
12 */
13 def timeoutTime = 30
14 def timeoutUnits = 'MINUTES'
15 if (env.TIMEOUT_UNITTESTS) {
16 def _timeout = env.TIMEOUT_UNITTESTS.split()
17 timeoutTime = _timeout[0].toInteger()
18 timeoutUnits = _timeout[1]
19 }
20
21 pipeline {
22 options {
23 ansiColor('gnome-terminal')
24 throttle(['asterisk-check'])
25 timestamps()
26 timeout(time: timeoutTime, unit: timeoutUnits)
27 }
28 triggers {
29 /*
30 * This trigger will match either the "asterisk" or "Security-asterisk"
31 * projects. The branch is taken from the branch this job was created
32 * for.
33 */
34 gerrit customUrl: '',
35 commentTextParameterMode: 'PLAIN',
36 commitMessageParameterMode: 'PLAIN',
37 gerritBuildSuccessfulVerifiedValue: 1,
38 gerritBuildFailedVerifiedValue: -1,
39 gerritBuildUnstableVerifiedValue: -1,
40 gerritProjects: [
41 [branches: [[compareType: 'PLAIN', pattern: "${BRANCH_NAME}"]],
42 compareType: 'REG_EXP',
43 disableStrictForbiddenFileVerification: false,
44 pattern: '^(Security-)?asterisk.*'
45 ]
46 ],
47 silentMode: false,
48 triggerOnEvents: [
49 commentAddedContains('^recheck$'),
50 patchsetCreated(excludeDrafts: false,
51 excludeNoCodeChange: false,
52 excludeTrivialRebase: false),
53 draftPublished()
54 ],
55 skipVote: [
56 onFailed: false,
57 onNotBuilt: true,
58 onSuccessful: false,
59 onUnstable: false
60 ]
61 }
62 agent {
63 /* All of the stages need to be performed on a docker host */
64 label "asterisk-check"
65 }
66
67 stages {
68 stage ("->") {
69 /*
70 * Jenkins will try to automatically rebuild this job when
71 * the jenkinsfile changes but since this job is dependent on
72 * Gerrit, we really don't want to do anything in that case.
73 */
74 when {
75 not { environment name: 'GERRIT_CHANGE_NUMBER', value: '' }
76 not { environment name: 'GERRIT_EVENT_ACCOUNT_NAME', value: 'Jenkins2' }
77 }
78 steps {
79 script {
80 manager.build.displayName = "${env.GERRIT_CHANGE_NUMBER}"
81 manager.createSummary("/plugin/workflow-job/images/48x48/pipelinejob.png").appendText("Docker Host: ${NODE_NAME}", false)
82
83 stage ("Checkout") {
84 sh "sudo chown -R jenkins:users ."
85 env.GERRIT_PROJECT_URL = env.GIT_URL.replaceAll(/[^\/]+$/, env.GERRIT_PROJECT)
86
87 /*
88 * Jenkins has already automatically checked out the base branch
89 * for this change but we now need to check out the change itself
90 * and rebase it on the current base branch. If the rebase fails,
91 * that's an indication to the user that they'll need to sort their
92 * change out.
93 *
94 * The Gerrit Trigger provides all the URLs and refspecs to
95 * check out the change.
96 *
97 * We need to retrieve the jenkins2 gerrit https credentials
98 * in case this review is in a restricted project.
99 */
100 withCredentials([usernamePassword(credentialsId: "${JENKINS_GERRIT_CREDS}",
101 passwordVariable: 'GERRIT_USER_PW', usernameVariable: 'GERRIT_USER_NAME')]) {
102
103 sh "printenv -0 | sort -z | tr '\\0' '\\n'"
104
105 checkout scm: [$class: 'GitSCM',
106 branches: [[name: env.GERRIT_BRANCH ]],
107 extensions: [
108 [$class: 'ScmName', name: env.GERRIT_NAME],
109 [$class: 'CleanBeforeCheckout'],
110 [$class: 'PreBuildMerge', options: [
111 mergeRemote: env.GERRIT_NAME,
112 fastForwardMode: 'NO_FF',
113 mergeStrategy: 'RECURSIVE',
114 mergeTarget: env.GERRIT_BRANCH]],
115 [$class: 'CloneOption',
116 honorRefspec: true,
117 noTags: true,
118 shallow: false
119 ],
120 [$class: 'PruneStaleBranch'],
121 [$class: 'BuildChooserSetting',
122 buildChooser: [$class: 'GerritTriggerBuildChooser']
123 ]
124 ],
125 userRemoteConfigs: [
126 [
127 credentialsId: env.JENKINS_GERRIT_CREDS,
128 name: env.GERRIT_NAME,
129 refspec: env.GERRIT_REFSPEC,
130 url: env.GERRIT_PROJECT_URL.replaceAll("http(s)?://", "http\$1://${GERRIT_USER_NAME}@")
131 ]
132 ]
133 ]
134 }
135
136 sh "sudo tests/CI/setupJenkinsEnvironment.sh"
137 }
138
139 def images = env.DOCKER_IMAGES.split(' ')
140 def r = currentBuild.startTimeInMillis % images.length
141 def ri = images[(int)r]
142 def randomImage = env.DOCKER_REGISTRY + "/" + ri;
143 def bt = env.BUILD_TAG.replaceAll(/[^a-zA-Z0-9_.-]/, '-')
144 def dockerOptions = "--privileged --ulimit core=0 --ulimit nofile=10240 " +
145 " --tmpfs /tmp:exec,size=1G -v /srv/jenkins:/srv/jenkins:rw -v /srv/cache:/srv/cache:rw " +
146 " --entrypoint='' --name ${bt}-build"
147 def outputdir = "tests/CI/output/UnitTests"
148
149 manager.createSummary("/plugin/workflow-job/images/48x48/pipelinejob.png").appendText("Docker Image: ${randomImage}", false)
150 def img = docker.image(randomImage)
151 img.pull()
152 img.inside(dockerOptions) {
153
154 stage ('Build') {
155 echo 'Building..'
156
157 sh "./tests/CI/buildAsterisk.sh --branch-name=${BRANCH_NAME} --output-dir=${outputdir} --cache-dir=/srv/cache"
158
159 archiveArtifacts allowEmptyArchive: true, defaultExcludes: false, fingerprint: false,
160 artifacts: "${outputdir}/*"
161 }
162
163 stage ('Test') {
164 def outputfile = "${outputdir}/unittests-results.xml"
165 def testcmd = "test execute all"
166
167 sh "sudo ./tests/CI/installAsterisk.sh --uninstall-all --branch-name=${BRANCH_NAME} --user-group=jenkins:users"
168
169 sh "tests/CI/runUnittests.sh --user-group=jenkins:users --output-dir='${outputdir}' --output-xml='${outputfile}' --unittest-command='${testcmd}'"
170
171 archiveArtifacts allowEmptyArchive: true, defaultExcludes: false, fingerprint: true,
172 artifacts: "${outputdir}/**"
173
174 junit testResults: outputfile,
175 healthScaleFactor: 1.0,
176 keepLongStdio: true
177 }
178 }
179 }
180 }
181 }
182 }
183 post {
184 cleanup {
185 script {
186 if (env.CLEANUP_WS_UNITTESTS.toBoolean()) {
187 cleanWs deleteDirs: true, notFailBuild: false
188 }
189 }
190 }
191 /*
192 * The Gerrit Trigger will automatically post the "Verified" results back
193 * to Gerrit but the verification publisher publishes extra stuff in the
194 * "Code Review" section of the review.
195 */
196 always {
197 script {
198 def cat
199 def comment
200 def rvalue
201 switch (currentBuild.currentResult) {
202 case ~/^SUCCESS$/:
203 cat = "Passed"
204 comment = ""
205 rvalue = 1
206 break
207 case ~/^FAILURE$/:
208 cat = "Failed"
209 comment = "Fatal Error"
210 rvalue = -1
211 break
212 case ~/^UNSTABLE$/:
213 cat = "Failed"
214 comment = "Tests Failed"
215 rvalue = -1
216 break
217 }
218
219 gerritverificationpublisher verifyStatusValue: rvalue,
220 verifyStatusCategory: cat, verifyStatusURL: '',
221 verifyStatusComment: comment, verifyStatusName: '',
222 verifyStatusReporter: 'Jenkins2', verifyStatusRerun: 'regate'
223 }
224 }
225 success {
226 echo "Reporting ${currentBuild.currentResult} Passed"
227 }
228 failure {
229 echo "Reporting ${currentBuild.currentResult}: Failed: Fatal Error"
230 }
231 unstable {
232 echo "Reporting ${currentBuild.currentResult}: Failed: Tests Failed"
233 }
234 }
235 }