| 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 declarative pipeline. |
| 12 |
*/ |
| 13 |
def timeoutTime = 60 |
| 14 |
def timeoutUnits = 'MINUTES' |
| 15 |
if (env.TIMEOUT_GATES) { |
| 16 |
def _timeout = env.TIMEOUT_GATES.split() |
| 17 |
timeoutTime = _timeout[0].toInteger() |
| 18 |
timeoutUnits = _timeout[1] |
| 19 |
} |
| 20 |
|
| 21 |
pipeline { |
| 22 |
options { |
| 23 |
ansiColor('gnome-terminal') |
| 24 |
throttle(['asterisk-gate']) |
| 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: 2, |
| 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('^regate$'), |
| 50 |
commentAdded(commentAddedTriggerApprovalValue: '+2', verdictCategory: 'Code-Review'), |
| 51 |
], |
| 52 |
skipVote: [ |
| 53 |
onFailed: false, |
| 54 |
onNotBuilt: true, |
| 55 |
onSuccessful: false, |
| 56 |
onUnstable: false |
| 57 |
] |
| 58 |
} |
| 59 |
|
| 60 |
agent { |
| 61 |
/* All of the stages need to be performed on a docker host */ |
| 62 |
label "asterisk-gate" |
| 63 |
} |
| 64 |
|
| 65 |
stages { |
| 66 |
stage ("->") { |
| 67 |
when { |
| 68 |
/* |
| 69 |
* Jenkins will try to automatically rebuild this job when |
| 70 |
* the jenkinsfile changes but since this job is dependent on |
| 71 |
* Gerrit, we really don't want to do anything in that case. |
| 72 |
*/ |
| 73 |
not { environment name: 'GERRIT_CHANGE_NUMBER', value: '' } |
| 74 |
/* If "skip_gate" is in the comments, don't run the job */ |
| 75 |
not { expression { env.GERRIT_EVENT_COMMENT_TEXT ==~ /.*skip_gate.*/ } } |
| 76 |
} |
| 77 |
steps { |
| 78 |
/* Here's where we switch to scripted pipeline */ |
| 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 |
sh "sudo tests/CI/setupJenkinsEnvironment.sh" |
| 136 |
} |
| 137 |
|
| 138 |
def images = env.DOCKER_IMAGES.split(' ') |
| 139 |
def r = currentBuild.startTimeInMillis % images.length |
| 140 |
def ri = images[(int)r] |
| 141 |
def randomImage = env.DOCKER_REGISTRY + "/" + ri |
| 142 |
/* FYI... Jenkins takes care of mounting the workspace for the container */ |
| 143 |
def dockerOptions = "--privileged --ulimit core=0 --ulimit nofile=10240 " + |
| 144 |
" --tmpfs /tmp:exec,size=1G -v /srv/jenkins:/srv/jenkins:rw -v /srv/cache:/srv/cache:rw " + |
| 145 |
" --entrypoint=''" |
| 146 |
def bt = env.BUILD_TAG.replaceAll(/[^a-zA-Z0-9_.-]/, '-') |
| 147 |
def outputdir = "tests/CI/output/Testsuite" |
| 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 |
|
| 153 |
stage ("Build") { |
| 154 |
img.inside(dockerOptions + " --name ${bt}-build") { |
| 155 |
echo 'Building..' |
| 156 |
env.CCACHE_DIR = "/srv/cache/ccache" |
| 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 |
|
| 164 |
def testGroups |
| 165 |
configFileProvider([configFile(fileId: 'asterisk_gate_test_groups', variable: 'GATE_TEST_GROUPS')]) { |
| 166 |
echo "Retrieved config file from ${env.GATE_TEST_GROUPS}" |
| 167 |
testGroups = readJSON file: env.GATE_TEST_GROUPS |
| 168 |
} |
| 169 |
echo "Running test groups:" |
| 170 |
for (def testGroup in testGroups) { |
| 171 |
echo "${testGroup.name} ${testGroup.dir} ${testGroup.testcmd}" |
| 172 |
} |
| 173 |
|
| 174 |
def parallelTasks = [ : ] |
| 175 |
|
| 176 |
for (def testGroup in testGroups) { |
| 177 |
/* |
| 178 |
* Because each task is a Groovy closure, we need to |
| 179 |
* keep local references to some variables. |
| 180 |
*/ |
| 181 |
def groupName = testGroup.name |
| 182 |
def groupDir = testGroup.dir |
| 183 |
def groupTestcmd = testGroup.testcmd |
| 184 |
def testsuiteUrl = env.GERRIT_PROJECT_URL.replaceAll(/\/(Security-)?[^\/]+$/, "/\$1testsuite") |
| 185 |
|
| 186 |
parallelTasks[groupName] = { |
| 187 |
stage (groupName) { |
| 188 |
|
| 189 |
img.inside("${dockerOptions} --name ${bt}-${groupName}") { |
| 190 |
|
| 191 |
lock("${JOB_NAME}.${NODE_NAME}.installer") { |
| 192 |
sh "sudo ./tests/CI/installAsterisk.sh --uninstall-all --branch-name=${BRANCH_NAME} --user-group=jenkins:users" |
| 193 |
} |
| 194 |
|
| 195 |
sh "sudo rm -rf ${groupDir} || : " |
| 196 |
|
| 197 |
withCredentials([usernamePassword(credentialsId: "${JENKINS_GERRIT_CREDS}", |
| 198 |
passwordVariable: 'GERRIT_USER_PW', usernameVariable: 'GERRIT_USER_NAME')]) { |
| 199 |
checkout scm: [$class: 'GitSCM', |
| 200 |
branches: [[name: "${BRANCH_NAME}"]], |
| 201 |
extensions: [ |
| 202 |
[$class: 'RelativeTargetDirectory', relativeTargetDir: groupDir], |
| 203 |
[$class: 'CloneOption', |
| 204 |
noTags: true, |
| 205 |
honorRefspec: true, |
| 206 |
shallow: false |
| 207 |
], |
| 208 |
], |
| 209 |
userRemoteConfigs: [ |
| 210 |
[ |
| 211 |
credentialsId: env.JENKINS_GERRIT_CREDS, |
| 212 |
name: env.GERRIT_NAME, |
| 213 |
url: testsuiteUrl.replaceAll("http(s)?://", "http\$1://${GERRIT_USER_NAME}@") |
| 214 |
] |
| 215 |
] |
| 216 |
] |
| 217 |
} |
| 218 |
|
| 219 |
sh "sudo tests/CI/runTestsuite.sh --testsuite-dir='${groupDir}' --testsuite-command='${groupTestcmd}'" |
| 220 |
|
| 221 |
archiveArtifacts allowEmptyArchive: true, defaultExcludes: false, fingerprint: true, |
| 222 |
artifacts: "${groupDir}/asterisk-test-suite-report.xml, ${groupDir}/logs/**, ${groupDir}/core*.txt" |
| 223 |
|
| 224 |
junit testResults: "${groupDir}/asterisk-test-suite-report.xml", |
| 225 |
healthScaleFactor: 1.0, |
| 226 |
keepLongStdio: true |
| 227 |
|
| 228 |
echo "Group result d: ${currentBuild.currentResult}" |
| 229 |
} |
| 230 |
echo "Group result s: ${currentBuild.currentResult}" |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
parallel parallelTasks |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
post { |
| 240 |
cleanup { |
| 241 |
script { |
| 242 |
if (env.CLEANUP_WS_GATES.toBoolean()) { |
| 243 |
cleanWs deleteDirs: true, notFailBuild: false |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
/* |
| 248 |
* The Gerrit Trigger will automatically post the "Verified" results back |
| 249 |
* to Gerrit but the verification publisher publishes extra stuff in the |
| 250 |
* "Code Review" section of the review. |
| 251 |
*/ |
| 252 |
always { |
| 253 |
script { |
| 254 |
def cat |
| 255 |
def comment |
| 256 |
def rvalue |
| 257 |
switch (currentBuild.currentResult) { |
| 258 |
case ~/^SUCCESS$/: |
| 259 |
cat = "Passed" |
| 260 |
comment = "" |
| 261 |
rvalue = 2 |
| 262 |
break |
| 263 |
case ~/^FAILURE$/: |
| 264 |
cat = "Failed" |
| 265 |
comment = "Fatal Error" |
| 266 |
rvalue = -1 |
| 267 |
break |
| 268 |
case ~/^UNSTABLE$/: |
| 269 |
cat = "Failed" |
| 270 |
comment = "Tests Failed" |
| 271 |
rvalue = -1 |
| 272 |
break |
| 273 |
} |
| 274 |
|
| 275 |
gerritverificationpublisher verifyStatusValue: rvalue, |
| 276 |
verifyStatusCategory: cat, verifyStatusURL: '', |
| 277 |
verifyStatusComment: comment, verifyStatusName: '', |
| 278 |
verifyStatusReporter: 'Jenkins2', verifyStatusRerun: 'regate' |
| 279 |
} |
| 280 |
} |
| 281 |
success { |
| 282 |
echo "Reporting ${currentBuild.currentResult} Passed" |
| 283 |
} |
| 284 |
failure { |
| 285 |
echo "Reporting ${currentBuild.currentResult}: Failed: Fatal Error" |
| 286 |
} |
| 287 |
unstable { |
| 288 |
echo "Reporting ${currentBuild.currentResult}: Failed: Tests Failed" |
| 289 |
} |
| 290 |
} |
| 291 |
} |