Jenkins multibranch pipeline Scan without execution
Asked Answered
U

7

47

Is it possible to Scan a Multibranch Pipeline to detect the branches with a Jenkinsfile, but without the pipeline execution?

My projects have different branches and I don't want that all the children pipelines branches with a Jenkinsfile to start to execute when I launch a build scan from the parent pipeline multibranch.

Utterance answered 16/5, 2017 at 14:43 Comment(0)
H
43

In your Branch Sources section you can add a Property named Suppress automatic SCM triggering.

This prevents Jenkins from building everything with an Jenkinsfile.

Hectogram answered 19/5, 2017 at 8:50 Comment(12)
ohhhhh! thanks you very much!. this is which i need :)Utterance
This seems to not only prevent jobs from executing as result of a scan (which is what you want), but also prevents them from executing as part of a commit to the source branch (which you probably don't want). In other words, this change blocks any Git Hooks from triggering a build.Pajamas
@AndrewCruickshank any way to do this but keep the web hooks working??Philander
It doesn't block a git hook if you use curl + auth token to trigger a build.Harlie
I can't find this, is it deprecated? What is the alternative?Enounce
Just for clarification, Gudgip's workaround is not the same as a webhook--meaning, Andrew is correct, as the docs for that class show: " * Suppresses builds due to either {@link BranchIndexingCause} or {@link BranchEventCause}. * The purpose of this property is to prevent triggering builds resulting from the <em>detection</em> * of changes in the underlying SCM." (github.com/jenkinsci/branch-api-plugin/blob/master/src/main/…)Nowlin
@FadelTrivandiDipantara I am facing same issue. It's deprecated. What's the workaround?Inescutcheon
Nice explanation here on the deprecation : issues.jenkins-ci.org/browse/…Automata
This is great! I have a multibranch repo pipeline on my local machine, which i use to test before i push up to my org's github. When i create new branches, delete branches, etc, i have to use the Scan to find/delete them. The scan results in every branch running the pipe. With this new behavior configured, i can scan the repo at will without triggering any builds! Specifically: in Property Strategy -> all branches get the same... -> configure as .* and select matching branches suppress builds . other options up to you.Keystroke
@MaxCascone, how do I set this up so that feature/* branches builds are suppressed when indexing, but the rest are not suppressed?Etrem
@ChrisF if you set the filter in the Property Strategy -> all branches get the same... to feature/*, that should block feature/* branches from triggering, while allowing others to trigger.Keystroke
@MaxCascone thanks! I was making it too complicated.Etrem
S
10

Also, you can do it programatically

import jenkins.branch.*
import jenkins.model.Jenkins


for (f in Jenkins.instance.getAllItems(jenkins.branch.MultiBranchProject.class)) {
  if (f.parent instanceof jenkins.branch.OrganizationFolder) {
    continue;
  }
  for (s in f.sources) {
    def prop = new jenkins.branch.NoTriggerBranchProperty();
    def propList = [prop] as jenkins.branch.BranchProperty[];
    def strategy = new jenkins.branch.DefaultBranchPropertyStrategy(propList);
    s.setStrategy(strategy);
  }

  f.computation.run()
}

This is a Groovy snippet you can execute in Jenkins, it's gonna do the scanning but will not start new "builds" for all discovered branches.

Stefan answered 10/10, 2018 at 14:56 Comment(7)
this is a groovy snippet you can execute in Jenkins it gonna do the scanning but will not start new "builds" for all discovered branchesStefan
where do I put this?Enounce
@FadelTrivandiDipantara basically you put it into $JENKINS_HOME/init.groovy.d folder and then when you start your Jenkins - it gonna execute all scripts from this folder. if you already have a jenkins up and running you can go to Jenkins -> Script Console and try to execute that code there.Stefan
can be executed also in the Jenkins Admin > Nodes > Master and open Script ConsoleAssonance
can this be added to Github folder that mentions all Jenkinsfiles? I have groovy scripts there.Inescutcheon
Calling f.scheduleBuild() instead of f.computation.run() runs it in background, and so it allows the script to finish for large amount of jobs.Wengert
Very cool script! How can this be modified so it only suppresses builds for certain branches, e.g., all feature/* branches?Etrem
C
7

If you are using job-dsl you could simply do this and it will scan everything without actually running the build the first time you index.

organizationFolder('Some folder name') {
  buildStrategies {
    skipInitialBuildOnFirstBranchIndexing()
  }
}
Commiserate answered 13/2, 2020 at 14:48 Comment(5)
do you need to have organizations or folders? how does it work? what should I use instead of "Some folder name"?Stefan
No, you can probably find buildStrategies other places, check the documentation on your instance, it should hold all available options. "Some folder name" is simply a placeholder, it will name a folder in Jenkins to organise your builds.Commiserate
I got error an error with this. ERROR: (script, line 13) No such property: skipInitialBuildOnFirstBranchIndexing for class: javaposse.jobdsl.dsl.helpers.workflow.BuildStrategiesContextClements
@Commiserate I got this error using your answer javaposse.jobdsl.dsl.helpers.workflow.BuildStrategiesContext.skipInitialBuildOnFirstBranchIndexing()Venetic
plugin basic-branch-build-strategies is requiredGotthard
M
4

To add to @Stqs's answer, you could also set noTriggerBranchProperty it using Job DSL plugin, e.g.:

multibranchPipelineJob('example') {
  ...
  branchSources {
    branchSource {
      ...
      strategy {
        defaultBranchPropertyStrategy {
          props {
            // Suppresses the normal SCM commit trigger coming from branch indexing
            noTriggerBranchProperty()
            ...
          }
        }
      }
    }
  }
  ...
}
Milliken answered 1/10, 2019 at 13:15 Comment(1)
noTriggerBranchProperty is doing the job but blocks webhooks Is it possibly to have noTriggerBranchProperty together with webhook triggers?Stefan
G
3
organizationFolder('my-folder') {

  buildStrategies {
     buildRegularBranches()
     buildChangeRequests {
       ignoreTargetOnlyChanges true
       ignoreUntrustedChanges false
     }
   }
}

Note: plugin basic-branch-build-strategies is required

REFERENCES:

Gotthard answered 28/2, 2021 at 8:16 Comment(0)
R
3

After much struggle I've found this solution, it should only avoid triggering the build when branch indexing and not disable automatic build after commit. Just add it in the first stage of your project:

when {
    not {
        expression {
            def causes = currentBuild.getBuildCauses()
            String causesClass = causes._class[0]
            return causesClass.contains('BranchIndexingCause')
        }
    }
}
Retrorse answered 11/5, 2021 at 16:18 Comment(0)
K
0

If you are using Jenkinsfiles to define your pipelines (you should be!), add the overrideIndexTriggers(false) option.

pipeline {
  options {
    overrideIndexTriggers(false) // disable branch indexing build trigger
  }
Keystroke answered 19/12, 2023 at 16:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.