How to Copy all the Jenkins Jobs programmatically

image
Rodrigo Asensio 7 years ago

1 min read

We are moving our projects from dev to qa and production. It is kind of painful to redo all jobs in jenkins or copy them individually. So using the scripting capabilities of Jenkins (groovy) we can copy the jobs with a new name and move them if you want to another view.


We are moving our projects from dev to qa and production. It is kind of painful to redo all jobs in jenkins or copy them individually. So using the scripting capabilities of Jenkins (groovy) we can copy the jobs with a new name and move them if you want to another view.

import hudson.model.*

def viewName = "product-build-dev"
def search = "-dev"
def replace = "-prod"

def view = Hudson.instance.getView(viewName)

/* now you copy all jobs of the view copy all projects of a view */
for(item in view.getItems()) {

  /* create the new project name */
  newName = item.getName().replace(search , replace)

  /* now copy the job */
  def job = Hudson.instance.copy(item, newName)
  job.save()

}

image

Rodrigo Asensio

Rodrigo Asensio is Manager of Solution Architecture at Amazon Web Services. He has more than 20 years of experience designing and operating distributed solutions. He is currently responsible for a team in the Enterprise segment helping large clients accelerate their adoption of the cloud and optimize the utilization of their resources.

Check out all articles