45

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.

Ivar
  • 6,138
  • 12
  • 49
  • 61
Daniel Majano
  • 976
  • 2
  • 10
  • 18

6 Answers6

42

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

This prevents Jenkins from building everything with an Jenkinsfile.

Michael Lihs
  • 7,460
  • 17
  • 52
  • 85
42tg
  • 695
  • 6
  • 12
  • ohhhhh! thanks you very much!. this is which i need :) – Daniel Majano May 22 '17 at 08:12
  • 16
    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. – Andrew Cruickshank Oct 04 '17 at 15:08
  • 1
    @AndrewCruickshank any way to do this but keep the web hooks working?? – emmdee Jun 14 '18 at 22:29
  • It doesn't block a git hook if you use curl + auth token to trigger a build. – sridesmet Jul 04 '18 at 09:21
  • 4
    I can't find this, is it deprecated? What is the alternative? – Fadel Trivandi Dipantara Jun 14 '19 at 07:26
  • 1
    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 detection * of changes in the underlying SCM." (https://github.com/jenkinsci/branch-api-plugin/blob/master/src/main/java/jenkins/branch/NoTriggerBranchProperty.java) – mellow-yellow Jan 03 '20 at 15:17
  • 1
    @FadelTrivandiDipantara I am facing same issue. It's deprecated. What's the workaround? – Chaitanya Bapat Mar 09 '20 at 23:00
  • Nice explanation here on the deprecation : https://issues.jenkins-ci.org/browse/JENKINS-56231?page=com.atlassian.jira.plugin.system.issuetabpanels%3Aall-tabpanel – Jonathan Bergeron May 07 '20 at 21:22
  • 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. – Max Cascone May 27 '22 at 19:46
9

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.

Paweł Prażak
  • 3,091
  • 1
  • 27
  • 42
Stqs
  • 355
  • 1
  • 4
  • 13
  • this is a groovy snippet you can execute in Jenkins it gonna do the scanning but will not start new "builds" for all discovered branches – Stqs Oct 11 '18 at 08:44
  • 1
    where do I put this? – Fadel Trivandi Dipantara Jun 14 '19 at 07:26
  • @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. – Stqs Aug 21 '19 at 13:46
  • can be executed also in the Jenkins Admin > Nodes > Master and open **Script Console** – Daniel Andrei Mincă Oct 30 '19 at 11:54
  • can this be added to Github folder that mentions all Jenkinsfiles? I have groovy scripts there. – Chaitanya Bapat Mar 09 '20 at 22:59
  • 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. – Lukáš Doležal Jan 10 '22 at 12:12
5

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()
  }
}
scav
  • 235
  • 4
  • 13
  • do you need to have organizations or folders? how does it work? what should I use instead of "Some folder name"? – Stqs May 07 '20 at 19:30
  • 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. – scav Jul 07 '20 at 12:04
  • I got error an error with this. `ERROR: (script, line 13) No such property: skipInitialBuildOnFirstBranchIndexing for class: javaposse.jobdsl.dsl.helpers.workflow.BuildStrategiesContext` – Adhika Setya Pramudita Dec 01 '20 at 06:58
  • @scav I got this error using your answer javaposse.jobdsl.dsl.helpers.workflow.BuildStrategiesContext.skipInitialBuildOnFirstBranchIndexing() – c4f4t0r Dec 11 '20 at 08:08
  • plugin `basic-branch-build-strategies` is required – Abdennour TOUMI Feb 28 '21 at 09:01
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()
            ...
          }
        }
      }
    }
  }
  ...
}
Paweł Prażak
  • 3,091
  • 1
  • 27
  • 42
  • 1
    `noTriggerBranchProperty` is doing the job but blocks webhooks Is it possibly to have `noTriggerBranchProperty` together with webhook triggers? – Stqs Jun 24 '20 at 17:07
3
organizationFolder('my-folder') {

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

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

REFERENCES:

Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
2

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')
        }
    }
}
Aarhun
  • 21
  • 2