CI: Add https credentials to gerrit checkouts
[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                                                  * We need to retrieve the jenkins2 gerrit https credentials
85                                                  * in case this review is in a restricted project.
86                                                  */
87                                                 withCredentials([usernamePassword(credentialsId: env.JENKINS_GERRIT_CREDS,
88                                                         usernameVariable: 'gerrit_user')]) {
89
90                                                         checkout scm: [$class: 'GitSCM',
91                                                                 branches: [[name: env.GERRIT_BRANCH ]],
92                                                                 extensions: [
93                                                                         [$class: 'ScmName', name: 'gerrit-public'],
94                                                                         [$class: 'CleanBeforeCheckout'],
95                                                                         [$class: 'PreBuildMerge', options: [
96                                                                                 mergeRemote: 'gerrit-public',
97                                                                                 fastForwardMode: 'NO_FF',
98                                                                                 mergeStrategy: 'RECURSIVE',
99                                                                                 mergeTarget: env.GERRIT_BRANCH]],
100                                                                         [$class: 'CloneOption',
101                                                                                 honorRefspec: true,
102                                                                                 noTags: true,
103                                                                                 depth: 10,
104                                                                                 shallow: true
105                                                                         ],
106                                                                         [$class: 'PruneStaleBranch'],
107                                                                         [$class: 'BuildChooserSetting',
108                                                                                 buildChooser: [$class: 'GerritTriggerBuildChooser']
109                                                                         ]
110                                                                 ],
111                                                                 userRemoteConfigs: [
112                                                                         [
113                                                                         credentialsId: env.JENKINS_GERRIT_CREDS,
114                                                                         name: env.GERRIT_NAME,
115                                                                         refspec: env.GERRIT_REFSPEC,
116                                                                         url: env.GERRIT_PROJECT_URL.replaceAll("http(s)?://", "http\$1://${gerrit_user}@")
117                                                                         ]
118                                                                 ]
119                                                         ]
120                                                 }
121
122                                                 sh "sudo tests/CI/setupJenkinsEnvironment.sh"
123                                         }
124
125                                         def images = env.DOCKER_IMAGES.split(' ')
126                                         def r = currentBuild.startTimeInMillis % images.length
127                                         def ri = images[(int)r]
128                                         def randomImage = env.DOCKER_REGISTRY + "/" + ri;
129                                         def bt = env.BUILD_TAG.replaceAll(/[^a-zA-Z0-9_.-]/, '-')
130                                         def dockerOptions = "--privileged --ulimit core=0 --ulimit nofile=10240 " +
131                                                 " -v /srv/jenkins:/srv/jenkins:rw -v /srv/cache:/srv/cache:rw " +
132                                                 " --entrypoint='' --name ${bt}-build"
133                                         def outputdir = "tests/CI/output/UnitTests"
134
135                                         manager.createSummary("/plugin/workflow-job/images/48x48/pipelinejob.png").appendText("Docker Image: ${randomImage}", false)
136                                         def img = docker.image(randomImage)
137                                         img.pull()
138                                         img.inside(dockerOptions) {
139
140                                                 stage ('Build') {
141                                                         echo 'Building..'
142
143                                                         sh "./tests/CI/buildAsterisk.sh --branch-name=${BRANCH_NAME} --output-dir=${outputdir} --cache-dir=/srv/cache"
144
145                                                         archiveArtifacts allowEmptyArchive: true, defaultExcludes: false, fingerprint: false,
146                                                                 artifacts: "${outputdir}/*"
147                                                 }
148
149                                                 stage ('Test') {
150                                                         def outputfile = "${outputdir}/unittests-results.xml"
151                                                         def testcmd = "test execute all"
152
153                                                         sh "sudo ./tests/CI/installAsterisk.sh --uninstall-all --branch-name=${BRANCH_NAME} --user-group=jenkins:users"
154
155                                                         sh "tests/CI/runUnittests.sh --user-group=jenkins:users --output-dir='${outputdir}' --output-xml='${outputfile}' --unittest-command='${testcmd}'"
156
157                                                         archiveArtifacts allowEmptyArchive: true, defaultExcludes: false, fingerprint: true,
158                                                                 artifacts: "${outputdir}/**"
159
160                                                         junit testResults: outputfile,
161                                                                 healthScaleFactor: 1.0,
162                                                                 keepLongStdio: true
163                                                 }
164                                         }
165                                 }
166                         }
167                 }
168         }
169         post {
170                 cleanup {
171                         sh "sudo make distclean 2&>/dev/null || : "
172                         sh "sudo rm -rf tests/CI/output 2&>/dev/null || : "
173                 }
174                 /*
175                  * The Gerrit Trigger will automatically post the "Verified" results back
176                  * to Gerrit but the verification publisher publishes extra stuff in the
177                  * "Code Review" section of the review.
178                  */
179                 always {
180                         script {
181                                 def cat
182                                 def comment
183                                 def rvalue
184                                 switch (currentBuild.currentResult) {
185                                         case ~/^SUCCESS$/:
186                                                 cat = "Passed"
187                                                 comment = ""
188                                                 rvalue = 1
189                                                 break
190                                         case ~/^FAILURE$/:
191                                                 cat = "Failed"
192                                                 comment = "Fatal Error"
193                                                 rvalue = -1
194                                                 break
195                                         case ~/^UNSTABLE$/:
196                                                 cat = "Failed"
197                                                 comment = "Tests Failed"
198                                                 rvalue = -1
199                                                 break
200                                 }
201
202                                 gerritverificationpublisher verifyStatusValue: rvalue,
203                                         verifyStatusCategory: cat, verifyStatusURL: '',
204                                         verifyStatusComment: comment, verifyStatusName: '',
205                                         verifyStatusReporter: 'Jenkins2', verifyStatusRerun: 'regate'
206                         }
207                 }
208                 success {
209                         echo "Reporting ${currentBuild.currentResult} Passed"
210                 }
211                 failure {
212                         echo "Reporting ${currentBuild.currentResult}: Failed: Fatal Error"
213                 }
214                 unstable {
215                         echo "Reporting ${currentBuild.currentResult}: Failed: Tests Failed"
216                 }
217         }
218 }