Copy Jobs in Jenkins

Copy Jobs in Jenkins

Recently guy from Dev-Ops team asked me whether I know a method to copy jobs in Jenkins bulkily while renaming them. I needed something to distract me from all these networking shenanigans. So I took the challange and wrote below script to save the day. Never knew Jenkins got a script console before this.

Anyway, Just replace the Variables at the beginning and paste the script into Manage Jenkins -> Script Console and RUN.

And then create a new view and select all the newly created jobs. That’s it.

import hudson.model.*
  
def str_view = "Source_View"
def str_suffix = "Text_TO_ADD_TO_THE_END_OF_JOB"

def view = Hudson.instance.getView(str_view)
 
for(item in view.getItems())
{
 
  newName = "$item.name"+"$str_suffix"
  def job = Hudson.instance.copy(item, newName)
  job.disabled = true
  job.save()

//print source and new name
  println(" $item.name copied as $newName")
 
}