Pace
Pace

Reputation: 43967

How to avoid Jenkins multibranch pipeline job triggering itself

I would like my Jenkins multibranch pipeline job to avoid triggering itself. The job makes a commit because it increments the version file and checks it into source control which causes an endless loop.

In a regular job I can follow these instructions to avoid this loop (although it's not the cleanest way).

The instructions do not work for a multibranch pipeline (there is no 'ignore commits from certain users' option). Is there any way in a Jenkins mulitbranch pipeline to prevent a self-triggered commit?

Upvotes: 8

Views: 7072

Answers (2)

coz
coz

Reputation: 486

For the Jenkins multibranch pipeline, in case you push any changes from the pipeline to its own repo, please use the plugin https://github.com/jenkinsci/ignore-committer-strategy-plugin to ignore a specific user commit, as recommended in https://docs.cloudbees.com/docs/cloudbees-ci-kb/latest/best-practices/avoiding-build-loops

Upvotes: 0

bp2010
bp2010

Reputation: 2472

A workaround if using GIT:

When bumping the version and committing, use a specific message in the commit log eg: [git-version-bump] - Bumping the version

After scm checkout, check if the last commit was a version bump commit, if it is, abort the job.

stage('Checkout') {
    checkout scm
    if (lastCommitIsBumpCommit()) {
        currentBuild.result = 'ABORTED'
        error('Last commit bumped the version, aborting the build to prevent a loop.')
    } else {
        echo('Last commit is not a bump commit, job continues as normal.')
    }
}

private boolean lastCommitIsBumpCommit() {
    lastCommit = sh([script: 'git log -1', returnStdout: true])
    if (lastCommit.contains("[git-version-bump]")) {
        return true
    } else {
        return false
    }
}

Upvotes: 9

Related Questions