Learn how to trigger all builds in a Jenkins organization or folder through the Groovy script console.

Jenkins is the automation server of choice for thousands of organizations. Though it is a bit quirky at times, it offer the features, flexibility and community that projects require. Central to Jenkins is the scripting language Groovy which allows for automating configuration and management.

Obtain the name of the folder or organization. This can be easily found in the Jenkins UI. Assume the name is my_organization.

First, obtain the job instance: Hudson.getItemByFullName()

def organization = Hudson.instance.getJob('my_organization')
if (!organization) throw new Exception("No organization by that name")

Then, get all the items in this folder: AbstractFolder.getItems()

def jobs = organization.getItems()

Now, iterate over the result and try to retrieve a sub-item with a specific characteristic. In this case, the branch name should match master. Using: Item.getName() and Groovy’s .find.

jobs.each { 
  // find the sub-job that's named "master"
  master = it.getItems().find { it.getName() == "master" } 
  // ... { trigger job } 
}

Finally, trigger the job if it is found, with WorkflowJob.scheduleBuild2() and a 0 quiet period.

  if ( master ) {
    master.scheduleBuild2(0)
  }

The final script looks like this: