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.
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.
Example Script
Be sure to update 'your-org-folder-name' to match your actual folder name.
// 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
Go to Manage Jenkins (the little on the top right) → Script Console.
Paste in the script.
Replace 'your-org-folder-name' with the correct folder name.
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.
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”):
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.