Daniel Majano
Daniel Majano

Reputation: 1116

Jenkins multibranch pipeline Scan without execution

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.

Upvotes: 48

Views: 25579

Answers (7)

Max Cascone
Max Cascone

Reputation: 833

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
  }

Upvotes: 1

Aarhun
Aarhun

Reputation: 31

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')
        }
    }
}

Upvotes: 3

Abdennour TOUMI
Abdennour TOUMI

Reputation: 93193

organizationFolder('my-folder') {

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

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

REFERENCES:

Upvotes: 3

scav
scav

Reputation: 265

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()
  }
}

Upvotes: 8

Paweł Prażak
Paweł Prażak

Reputation: 3191

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()
            ...
          }
        }
      }
    }
  }
  ...
}

Upvotes: 4

Stqs
Stqs

Reputation: 385

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.

Upvotes: 10

42tg
42tg

Reputation: 725

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

This prevents Jenkins from building everything with an Jenkinsfile.

Upvotes: 44

Related Questions