euccas
7/28/2016 - 5:44 PM

[groovy] jenkins copy all jobs in a view

Copy all jobs in a view

import hudson.model.*
 
def str_view = "AIR_Project_A630"
def str_search = "air_a630"
def str_replace = "air_a630v1"
 
def view = Hudson.instance.getView(str_view)
 
//copy all projects of a view
for(item in view.getItems())
{

  //create the new project name
  def oldname = item.getName()
  if (oldname) {
  	def 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
  	job.save()
    
    // update the workspace to avoid having two projects point to the same location
  	AbstractProject project = job
  
  	def old_workspace = project.getCustomWorkspace()
  	if (old_workspace) {
  		def new_workspace = project.getCustomWorkspace().replace(str_search, str_replace)
  		project.setCustomWorkspace(new_workspace)
  		project.save()
  	}
  
  	println(" $item.name copied as $newName")
  }
 
}