Jenkins : Clone all projects in a View

This script enumerates all projects belonging to a specific view and clones them. This is very handy if you created a new branch and need to setup build automation for it. The script has ample opportunity for improvement, error checking, only a single save etc, but it took me a while to make this work and wanted to share the progress so far. So if you have improvements, feel free to add them :-)

This is what the script does in detail:

  • enumerate all projects that belong to a view
  • clone each project
  • give it a new name
  • disable the project
  • update the custom workspace

Note: the script assumes that you have a custom workspace declared, which all our projects have. If you don't have that, just take out the part of the code.

import hudson.model.*
def str_view = "MyProduct_Release_1.0"
def str_search = "Rel_1.0"
def str_replace = "Rel_1.1"
def view = Hudson.instance.getView(str_view)
//copy all projects of a view
for(item in view.getItems())

  //create the new project name
  newName = item.getName().replace(str_search, str_replace)

  // copy the job, disable and save it
  def job = Hudson.instance.copy(item, newName)
  job.disabled = true
  // update the workspace to avoid having two projects point to the same location
  AbstractProject project = job
  def new_workspace = project.getCustomWorkspace().replace(str_search, str_replace)
  println(" $ copied as $newName")