Skip to content

Useful code snippets for Jenkins pipeline definitions (Jenkinsfile)

License

Notifications You must be signed in to change notification settings

aprilrd/jenkinsfile-recipes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

Jenkinsfile Recipe

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.

Set up related recipes

To set a commit status in github to be in progress

step([$class: 'GitHubSetCommitStatusBuilder'])
  • Tested in Jenkins ^2.1

To run the build with the merged code

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

Build related recipes

To use a shell command stdout in Jenkinsfile

sh(script: script, returnStdout: true)
  • Tested in Jenkins ^2.16

To catch errors per stage

def handleStageFailure(String stageName, Closure stageDefinition) {
    stage stageName
    try {
        stageDefinition()
    } catch (err) {
        setCurrentBuildFailureStatus("${stageName} failed: ${err}")
        throw err
    }
}
  • Tested in Jenkins ^2.14

To not include credentials in SCM

withCredentials([
    [$class: 'StringBinding', credentialsId: credentialsId, variable: credentialsEnvId],
]) {
    //Stuff to do
}
  • Tested in Jenkins ^2.14

Tear down related recipes

To make build statuses descriptive

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

To archive Junit xml files

step([$class: 'JUnitResultArchiver', testResults: testResultsPath])

To archive other build artifacts

archive excludes: 'excludesPath', includes: 'includesPath'
  • excludesPath and includesPath are in the form of output/**.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.

To clean up the directory before/after the build

deleteDir()
  • Tested in Jenkins ^2.1

To set a commit status in github based on currentBuild.status after the build is finished

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

To add Cobertura coverage report

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

License

MIT

About

Useful code snippets for Jenkins pipeline definitions (Jenkinsfile)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published