unittests.jenkinsfile 7.4 KB

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