This repository lists useful code snippets for Jenkinsfile. Even with step references, snippet generator, and stack overflow, it is hard to learn what kind of tasks are available in Jenkinsfile. IE, figure out how to mark the github commit status using Jenkinsfile was very painful for me. So I hope this recipe can be helpful to those who just began learning about Jenkinsfile.
If you find a useful snippet is missing, please make a PR!
- Most of the recipes are usable in Jenkins Pipeline as well. I recommend using Jenkins Pipeline as it tends to be easier to read, maintain, and test Pipelines.
step([$class: 'GitHubSetCommitStatusBuilder'])
- Tested in Jenkins ^2.1
checkout(
scm: [
$class: 'GitSCM',
branches: [
[name: "refs/heads/${env.BRANCH_NAME}"]
],
extensions: [
[$class: 'PreBuildMerge', options: [fastForwardMode: 'FF', mergeRemote: 'origin', mergeTarget: branchToMerge]]
],
userRemoteConfigs: [
[credentialsId: credentialsId, url: gitUrl]
]
]
)
- Tested in Jenkins ^2.14
- Unclear whether same could be used for subversion
sh(script: script, returnStdout: true)
- Tested in Jenkins ^2.16
def handleStageFailure(String stageName, Closure stageDefinition) {
stage stageName
try {
stageDefinition()
} catch (err) {
setCurrentBuildFailureStatus("${stageName} failed: ${err}")
throw err
}
}
- Tested in Jenkins ^2.14
withCredentials([
[$class: 'StringBinding', credentialsId: credentialsId, variable: credentialsEnvId],
]) {
//Stuff to do
}
- Tested in Jenkins ^2.14
currentBuild.description = buildStatus
So if you want to set the description based on a Pipeline stage:
failure {
script {
currentBuild.description = "Failed to upload production build to S3"
}
}
- Tested in Jenkins ^2.1
step([$class: 'JUnitResultArchiver', testResults: testResultsPath])
testResultsPath
is in the form ofoutput/**/*.xml
- Tested in Jenkins ^2.16
- Needs https://wiki.jenkins-ci.org/display/JENKINS/JUnit+Plugin
- Throws an exception when there is no test results in the path
archive excludes: 'excludesPath', includes: 'includesPath'
excludesPath
andincludesPath
are in the form ofoutput/**.xml
.- Tested in Jenkins ^2.14
- Currently deprecated.
Use this instead:
archiveArtifacts excludes: 'excludesPath', artifacts: 'includesPath'
- Tested in Jenkins ^2.119. Though I am pretty sure it has been working long time ago.
deleteDir()
- Tested in Jenkins ^2.1
step([
$class: 'GitHubCommitStatusSetter',
errorHandlers: [[$class: 'ShallowAnyErrorHandler']],
statusResultSource: [
$class: 'ConditionalStatusResultSource',
results: [
[$class: 'BetterThanOrEqualBuildResult', result: 'SUCCESS', state: 'SUCCESS', message: currentBuild.description],
[$class: 'BetterThanOrEqualBuildResult', result: 'FAILURE', state: 'FAILURE', message: currentBuild.description],
[$class: 'AnyBuildResult', state: 'FAILURE', message: 'Loophole']
]
]
])
- Tested in Jenkins ^2.1
step([$class: 'CoberturaPublisher', autoUpdateHealth: false, autoUpdateStability: false, coberturaReportFile: 'output/coverage/cobertura-coverage.xml', failUnhealthy: false, failUnstable: false, maxNumberOfBuilds: 0, onlyStable: false, sourceEncoding: 'ASCII', zoomCoverageChart: false])
- Tested in Jenkins ^2.119
MIT