Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell Jenkins "Build every project in folder X"?

I have set up some folders (Using Cloudbees Folder Plugin).

It sounds like the simplest possible command to be able to tell Jenkins: Build every job in Folder X.

I do not want to have to manually create a comma-separated list of every job in the folder. I do not want to add to this list whenever I want to add a job to this folder. I simply want it to find all the jobs in the folder at run time, and try to build them.

I'm not finding a plugin that lets me do that.

I've tried using the Build Pipeline Plugin, the Bulk Builder Plugin, the MultiJob plugin, and a few others. None seem to support the use case I'm after. I simply want any Job in the folder to be built. In other words, adding a job to this build is as simple as creating a job in this folder.

How can I achieve this?

like image 407
Steve Cohen Avatar asked May 24 '16 22:05

Steve Cohen


2 Answers

I've been using Jenkins for some years and I've not found a way of doing what you're after.

The best I've managed is: I have a "run every job" job (which contains a comma-separated list of all the jobs you want). Then I have a separate job that runs periodically and updates the "run every job" job as new projects come and go.

like image 164
Wayne Booth Avatar answered Sep 28 '22 01:09

Wayne Booth


One way to do this is to create a Pipeline job that runs Groovy script to enumerate all jobs in the current folder and then launch them.

The version below requires the sandbox to be disabled (so it can access Jenkins.instance).

def names = jobNames()
for (i = 0; i < names.size(); i++) {
    build job: names[i], wait: false
}

@NonCPS
def jobNames() {
  def project = Jenkins.instance.getItemByFullName(currentBuild.fullProjectName)
  def childItems = project.parent.items

  def targets = []
  for (i = 0; i < childItems.size(); i++) {
      def childItem = childItems[i]
      if (!childItem instanceof AbstractProject) continue;
      if (childItem.fullName == project.fullName) continue;

      targets.add(childItem.fullName)
  }

  return targets
}

If you use Pipeline libraries, then the following is much nicer (and does not require you to allow a Groovy sandbox escape:

Add the following to your library:

package myorg;

public String runAllSiblings(jobName) {
  def names = siblingProjects(jobName)
  for (def i = 0; i < names.size(); i++) {
    build job: names[i], wait: false
  }
}

@NonCPS
private List siblingProjects(jobName) {
  def project = Jenkins.instance.getItemByFullName(jobName)
  def childItems = project.parent.items

  def targets = []
  for (def i = 0; i < childItems.size(); i++) {
    def childItem = childItems[i]
    if (!childItem instanceof AbstractProject) continue;
    if (childItem.fullName == jobName) continue;

    targets.add(childItem.fullName)
  }
  return targets
}

And then create a pipeline with the following code:

(new myorg.JobUtil()).runAllSiblings(currentBuild.fullProjectName)

Yes, there are ways to simplify this further, but it should give you some ideas.

like image 43
Ben Walding Avatar answered Sep 28 '22 02:09

Ben Walding