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!
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
- 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
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
MIT