Search code examples
jenkinsjenkins-pipeline

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?


Solution

  • 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
        }
    }