Manually trigger "scan multibranch pipeline" for all jobs in an organization folder

I’m currently running Jenkins 2.516.3.

I’m investigating setting up an organization folder (with the Bitbucket Branch Source plugin) and had some queries about triggering the builds.

I have many repositories in the organization, each with quite a few branches that need building. When I scan the organization folder manually, it adds the repositories as multi branch pipelines. If I push changes to the repositories and scan the organization again, it does not rebuild the branches with the changes, I have to go in to each repository/job and manually click “Scan multibranch pipeline now”.

Is there a way to manually trigger all the multibranch pipelines in the organization to be scanned at the same time to avoid needing to go through each one individually?

It’s looking unlikely that I will be able to use webhooks due to firewalls, at least for now.

I can set an interval for the “scan organization folder triggers”, which looks like it gets inherited by the “scan multibranch pipeline triggers”, but it doesn’t look like there’s control over exactly when this will run and I would like is the ability to force them all to run when builds are needed immediately.

Hello and welcome to this community, @arghness! :waving_hand:

I’m sure others will chime in and give you a better answer, but in the meantime…

If you want to quickly trigger scans for all Jenkins Multibranch Pipeline jobs inside an organization folder, instead of clicking “Scan Multibranch Pipeline Now” one by one, you can do this via the Script Console.


Why this should work

The script loops through all items in the chosen organization folder, checks if they are multibranch pipeline jobs (which support scheduleBuild2), and then schedules a scan for each.
This should make Jenkins look for new branches or updated commits and trigger builds if needed. :crossed_fingers:


Example Script

:warning: Be sure to update 'your-org-folder-name' to match your actual folder name. :wink:

// Trigger a scan on every multibranch pipeline inside the given organization folder
def orgFolder = Jenkins.instance.getItem('your-org-folder-name')

orgFolder.getItems().each { job ->
    if (job.metaClass.hasProperty(job, 'scheduleBuild2')) {
        println "Triggering scan for: ${job.fullName}"
        job.scheduleBuild2(0, new hudson.model.Cause.UserIdCause())
    }
}

How to use it

  1. Go to Manage Jenkins (the little :gear: on the top right) → Script Console.
  2. Paste in the script.
  3. Replace 'your-org-folder-name' with the correct folder name.
  4. Click Run.

A few notes

  • This does not run builds directly, it just starts the scanning process for each multibranch pipeline. Jenkins will only trigger builds if it finds new or changed branches/commits.
  • You’ll need admin/script permissions to use the Script Console.
  • If your folder contains nested subfolders, you may want to extend the script to recurse into them.
  • It might be helpful to try it on a test folder first to make sure it behaves as expected in your setup.

If you want that as a one click solution put the script in a “system groovy step” inside a freestyle job without sandbox. Needs script approval though.

1 Like

Thanks! That looks like it’s what I’m trying to do, but unfortunately I couldn’t get it to work.

It doesn’t seem to find the scheduleBuild2 property on the multi-pipeline-job.

I added some code to dump the metaClass properties, in case that’s any help in debugging this:

// Trigger a scan on every multibranch pipeline inside the given organization folder
def orgFolder = Jenkins.instance.getItemByFullName('MyView/asimpson-test-organization')

orgFolder.getItems().each { job ->
  println "Job: ${job.fullName}"
  job.metaClass.properties.each {
    property ->
    println "  Property: ${property.name}"
  }
    if (job.metaClass.hasProperty(job, 'scheduleBuild2')) {
        println "Triggering scan for: ${job.fullName}"
        job.scheduleBuild2(0, new hudson.model.Cause.UserIdCause())
    }
}

Output (jenkins-test1 and jenkins-test2 are the git repositories detected by the organization folder, which when clicked on in the UI give the option to “scan multibranch pipeline now”):

Job: MyView/asimpson-test-organization/jenkins-test1
  Property: rootDir
  Property: folderViews
  Property: searchIndex
  Property: properties
  Property: allJobs
  Property: overrides
  Property: ACL
  Property: descriptor
  Property: userViewsTabBar
  Property: lastSuccessfulBuild
  Property: sourcesList
  Property: search
  Property: allViews
  Property: healthMetrics
  Property: sources
  Property: actions
  Property: affinityKey
  Property: configFile
  Property: fullName
  Property: projectClass
  Property: displayName
  Property: fullDisplayName
  Property: ownerExecutable
  Property: searchName
  Property: resourceList
  Property: subTasks
  Property: absoluteUrl
  Property: whyBlocked
  Property: indexing
  Property: buildable
  Property: items
  Property: icon
  Property: url
  Property: name
  Property: lastStableBuild
  Property: ownerTask
  Property: searchIcon
  Property: api
  Property: searchUrl
  Property: views
  Property: iconColor
  Property: hasEvents
  Property: triggers
  Property: projectFactory
  Property: pronoun
  Property: displayNameOrNull
  Property: staplerFallback
  Property: viewActions
  Property: assignedLabel
  Property: parent
  Property: nameEditable
  Property: urlChildPrefix
  Property: buildHealthReports
  Property: viewsTabBar
  Property: lastFailedBuild
  Property: defaultAuthentication2
  Property: sourcePronoun
  Property: sameNodeConstraint
  Property: disabled
  Property: class
  Property: successfulDestination
  Property: lastBuiltOn
  Property: buildHealth
  Property: primaryView
  Property: computation
  Property: welcomeView
  Property: causeOfBlockage
  Property: target
  Property: orphanedItemStrategy
  Property: SCMSources
  Property: jobsDir
  Property: shortUrl
  Property: searchGroup
  Property: computationDir
  Property: buildBlocked
  Property: allItems
  Property: itemsStream
  Property: concurrentBuild
  Property: orphanedItemStrategyDescriptors
  Property: itemGroup
  Property: taskNoun
  Property: triggerDescriptors
  Property: allActions
  Property: description
  Property: estimatedDuration
  Property: defaultAuthentication
  Property: iconClassName
Job: MyView/asimpson-test-organization/jenkins-test2
  Property: rootDir
  Property: folderViews
  Property: searchIndex
  Property: properties
  Property: allJobs
  Property: overrides
  Property: ACL
  Property: descriptor
  Property: userViewsTabBar
  Property: lastSuccessfulBuild
  Property: sourcesList
  Property: search
  Property: allViews
  Property: healthMetrics
  Property: sources
  Property: actions
  Property: affinityKey
  Property: configFile
  Property: fullName
  Property: projectClass
  Property: displayName
  Property: fullDisplayName
  Property: ownerExecutable
  Property: searchName
  Property: resourceList
  Property: subTasks
  Property: absoluteUrl
  Property: whyBlocked
  Property: indexing
  Property: buildable
  Property: items
  Property: icon
  Property: url
  Property: name
  Property: lastStableBuild
  Property: ownerTask
  Property: searchIcon
  Property: api
  Property: searchUrl
  Property: views
  Property: iconColor
  Property: hasEvents
  Property: triggers
  Property: projectFactory
  Property: pronoun
  Property: displayNameOrNull
  Property: staplerFallback
  Property: viewActions
  Property: assignedLabel
  Property: parent
  Property: nameEditable
  Property: urlChildPrefix
  Property: buildHealthReports
  Property: viewsTabBar
  Property: lastFailedBuild
  Property: defaultAuthentication2
  Property: sourcePronoun
  Property: sameNodeConstraint
  Property: disabled
  Property: class
  Property: successfulDestination
  Property: lastBuiltOn
  Property: buildHealth
  Property: primaryView
  Property: computation
  Property: welcomeView
  Property: causeOfBlockage
  Property: target
  Property: orphanedItemStrategy
  Property: SCMSources
  Property: jobsDir
  Property: shortUrl
  Property: searchGroup
  Property: computationDir
  Property: buildBlocked
  Property: allItems
  Property: itemsStream
  Property: concurrentBuild
  Property: orphanedItemStrategyDescriptors
  Property: itemGroup
  Property: taskNoun
  Property: triggerDescriptors
  Property: allActions
  Property: description
  Property: estimatedDuration
  Property: defaultAuthentication
  Property: iconClassName
Result: [org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject@7166bc02[MyView/asimpson-test-organization/jenkins-test1], org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject@3ce8f128[MyView/asimpson-test-organization/jenkins-test2]]

Thanks, is a plugin needed for that?

I couldn’t see the option to add a “system groovy step” as a build step in a freestyle project on my Jenkins instance, but it looks like what you’re describing may be possible with Groovy or another similar plugin?

yes the groovy plugin is required for this. You can do that also with a pipeline job, but it requires that you put the code inside a method that is annotated with @ NonCPS and ideally the job is also configured in speed mode that avoids that Jenkins tries to serialize the state. Also here you need to approve the script and also not to run that in the sandbox.

1 Like

I could check for the method with “respondsTo” and the method signature wasn’t quite right. This seems to work for me:

// Trigger a scan on every multibranch pipeline inside the given organization folder
def orgFolder = Jenkins.instance.getItem('your-org-folder-name')

orgFolder.getItems().each { job ->
    if (job.respondsTo('scheduleBuild2', int, Action[])) {
        println "Triggering scan for: ${job.fullName}"
        job.scheduleBuild2(0, new hudson.model.CauseAction(new hudson.model.Cause.UserIdCause()))
    }
}

Thanks again for pointing me in the right direction!

Thanks a lot for the feedback! :+1: