asterisk/tests/CI/gates.jenkinsfile

231 lines
7.2 KiB
Plaintext
Raw Normal View History

/*
* This pipeline is the "template" for the Asterisk Gate Tests multi-branch
* parent job. Jenkins will automatically scan the branches in the "asterisk"
* or "Security-asterisk" projects in Gerrit and automatically create a branch-
* specific job for each branch it finds this file in.
*
* This file starts as a declarative pipeline because with a declarative
* pipeline, you can define the trigger in the pipeline file. This keeps
* everything in one place. We transition to scripted pipeline later on because
* we need to dynamically determine which docker image we're going to use and
* you can't do that in a delcarative pipeline.
*/
pipeline {
triggers {
/*
* This trigger will match either the "asterisk" or "Security-asterisk"
* projects. The branch is taken from the branch this job was created
* for.
*/
gerrit customUrl: '',
commentTextParameterMode: 'PLAIN',
commitMessageParameterMode: 'PLAIN',
gerritBuildSuccessfulVerifiedValue: 2,
gerritBuildFailedVerifiedValue: -1,
gerritBuildUnstableVerifiedValue: -1,
gerritProjects: [
[branches: [[compareType: 'PLAIN', pattern: "${BRANCH_NAME}"]],
compareType: 'REG_EXP',
disableStrictForbiddenFileVerification: false,
pattern: '^(Security-)?asterisk.*'
]
],
silentMode: false,
triggerOnEvents: [
commentAddedContains('^regate$'),
commentAdded(commentAddedTriggerApprovalValue: '+2', verdictCategory: 'Code-Review'),
],
skipVote: [
onFailed: false,
onNotBuilt: true,
onSuccessful: false,
onUnstable: false
]
}
agent {
/* All of the stages need to be performed on a docker host */
label "swdev-docker"
}
stages {
stage ("->") {
/*
* Jenkins will try to automatically rebuild this job when
* the jenkinsfile changes but since this job is dependent on
* Gerrit, we really don't want to do anything in that case.
*/
when {
not { environment name: 'GERRIT_CHANGE_NUMBER', value: '' }
}
steps {
/* Here's where we switch to scripted pipeline */
script {
stage ("Checkout") {
sh "sudo chown -R jenkins:users ."
env.GERRIT_PROJECT_URL = env.GERRIT_CHANGE_URL.replaceAll(/\/[0-9]+$/, "/${env.GERRIT_PROJECT}")
sh "printenv | sort"
/*
* Jenkins has already automatically checked out the base branch
* for this change but we now need to check out the change itself
* and rebase it on the current base branch. If the rebase fails,
* that's an indication to the user that they'll need to sort their
* change out.
*
* The Gerrit Trigger provides all the URLs and refspecs to
* check out the change.
*/
checkout scm: [$class: 'GitSCM',
branches: [[name: env.GERRIT_BRANCH ]],
extensions: [
[$class: 'CleanBeforeCheckout'],
[$class: 'PreBuildMerge', options: [
mergeRemote: env.GERRIT_NAME,
mergeTarget: env.GERRIT_BRANCH ]
],
[$class: 'CloneOption',
noTags: true,
depth: 10,
shallow: true
],
[$class: 'BuildChooserSetting',
buildChooser: [$class: 'GerritTriggerBuildChooser']
]
],
userRemoteConfigs: [
[name: env.GERRIT_NAME, refspec: env.GERRIT_REFSPEC, url: env.GERRIT_PROJECT_URL ]
]
]
sh "sudo tests/CI/setupJenkinsEnvironment.sh"
}
def images = env.DOCKER_IMAGES.split(' ')
def r = currentBuild.startTimeInMillis % images.length
def ri = images[(int)r]
def randomImage = env.DOCKER_REGISTRY + "/" + ri
def dockerOptions = "--ulimit core=0 --ulimit nofile=10240 " +
" -v /srv/jenkins:/srv/jenkins:rw -v /srv/cache:/srv/cache:rw " +
" --entrypoint=''"
def bt = env.BUILD_TAG.replaceAll(/[^a-zA-Z0-9_.-]/, '-')
def outputdir = "tests/CI/output/Testsuite"
def img = docker.image(randomImage)
img.pull()
stage ("Build") {
img.inside(dockerOptions + " --name ${bt}-build") {
echo 'Building..'
env.CCACHE_DIR = "/srv/cache/ccache"
sh "./tests/CI/buildAsterisk.sh --output-dir=${outputdir} --cache-dir=/srv/cache"
archiveArtifacts allowEmptyArchive: true, defaultExcludes: false, fingerprint: false,
artifacts: "${outputdir}/*"
}
}
def testGroups = readJSON file: "tests/CI/gateTestGroups.json"
def parallelTasks = [ : ]
for (def testGroup in testGroups) {
/*
* Because each task is a Groovy closure, we need to
* keep local references to some variables.
*/
def groupName = testGroup.name
def groupDir = testGroup.dir
def groupTestcmd = testGroup.testcmd
def testsuiteUrl = env.GERRIT_PROJECT_URL.replaceAll(/\/(Security-)?[^\/]+$/, "/\$1testsuite")
parallelTasks[groupName] = {
stage (groupName) {
img.inside("${dockerOptions} --name ${bt}-${groupName}") {
lock("${JOB_NAME}.${NODE_NAME}.installer") {
sh 'sudo ./tests/CI/installAsterisk.sh --user-group=jenkins:users'
}
sh "sudo rm -rf ${groupDir} || : "
checkout scm: [$class: 'GitSCM',
branches: [[name: "${BRANCH_NAME}"]],
extensions: [
[$class: 'RelativeTargetDirectory', relativeTargetDir: groupDir],
[$class: 'CloneOption',
noTags: true,
depth: 10,
honorRefspec: true,
shallow: true
],
],
userRemoteConfigs: [[name: env.GERRIT_NAME, url: testsuiteUrl]]
]
sh "sudo tests/CI/runTestsuite.sh --testsuite-dir='${groupDir}' --test-command='${groupTestcmd}'"
archiveArtifacts allowEmptyArchive: true, defaultExcludes: false, fingerprint: true,
artifacts: "${groupDir}/asterisk-test-suite-report.xml, ${groupDir}/logs/**, ${groupDir}/core*.txt"
junit testResults: "${groupDir}/asterisk-test-suite-report.xml",
healthScaleFactor: 1.0,
keepLongStdio: true
echo "Group result d: ${currentBuild.currentResult}"
}
echo "Group result s: ${currentBuild.currentResult}"
}
}
}
parallel parallelTasks
}
}
}
}
post {
cleanup {
sh "sudo make distclean 2&>/dev/null || : "
sh "sudo rm -rf tests/CI/output 2&>/dev/null || : "
}
/*
* The Gerrit Trigger will automatically post the "Verified" results back
* to Gerrit but the verification publisher publishes extra stuff in the
* "Code Review" section of the review.
*/
always {
script {
def cat
def comment
switch (currentBuild.currentResult) {
case ~/^SUCCESS$/:
cat = "Passed"
comment = ""
break
case ~/^FAILURE$/:
cat = "Failed"
comment = "Fatal Error"
break
case ~/^UNSTABLE$/:
cat = "Failed"
comment = "Tests Failed"
break
}
gerritverificationpublisher verifyStatusCategory: cat,
verifyStatusURL: '', verifyStatusComment: comment,
verifyStatusName: '', verifyStatusReporter: 'Jenkins2',
verifyStatusRerun: 'regate'
}
}
success {
echo "Reporting ${currentBuild.currentResult} Passed"
}
failure {
echo "Reporting ${currentBuild.currentResult}: Failed: Fatal Error"
}
unstable {
echo "Reporting ${currentBuild.currentResult}: Failed: Tests Failed"
}
}
}