Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable triggers from branch indexing but still allow SCM triggering in multibranch jobs

When using a Jenkins Multibranch Pipeline Job if you choose Suppress Automatic SCM trigger in the job it will stop the job from building after indexing branches (great functionality).

However for some reason this ALSO will kill the ability to trigger the build from SCM events!

Is there any way to stop builds from triggering after branch discovery (branch indexing) but still build normally by SCM events?

like image 424
emmdee Avatar asked Mar 14 '19 23:03

emmdee


2 Answers

You can always add logic to your pipeline to abort on branch indexing causes. For example:

  boolean isBranchIndexingCause() {
    def isBranchIndexing = false
    if (!currentBuild.rawBuild) {
      return true
    }

    currentBuild.rawBuild.getCauses().each { cause ->
      if (cause instanceof jenkins.branch.BranchIndexingCause) {
        isBranchIndexing = true
      }
    }
    return isBranchIndexing
  }

Adjust the logic to suit your use-case.

EDIT: The Pipeline Syntax > Global Variables Reference embedded within the Jenkins UI (e.g.: <jenkins url>/job/<pipeline job name>/pipeline-syntax/globals) has information about the currentBuild global variable, which lead to some javadocs:

The currentBuild variable, which is of type RunWrapper, may be used to refer to the currently running build. It has the following readable properties:

...

rawBuild:

a hudson.model.Run with further APIs, only for trusted libraries or administrator-approved scripts outside the sandbox; the value will not be Serializable so you may only access it inside a method marked @NonCPS

...

See also: jenkins.branch.BranchIndexingCause

like image 130
Adam Bennett Avatar answered Nov 15 '22 10:11

Adam Bennett


I know this post is very old, but maybe someone still has this issue, first you need to install the plugin basic-branch-build-strategies:

If you are using jenkins dsl:

buildStrategies {
  buildAllBranches {
    strategies {
      skipInitialBuildOnFirstBranchIndexing()
    }
  }
}
like image 22
c4f4t0r Avatar answered Nov 15 '22 09:11

c4f4t0r