Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 326
Pace Avatar asked Aug 15 '17 16:08

Pace


1 Answers

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
    }
}
like image 103
bp2010 Avatar answered Sep 19 '22 13:09

bp2010