Developing on Staxmanade

How to Migrate a Jenkins Job to New Jenkins Server


We recently setup a new Jenkins build server for some iOS applications and I wanted to find a quick way to copy a couple Jobs from the old server to the new one.

Below are a few small options I found while working on the task.

Option 1: Copy jobs directory

One option (and seems to be the recommended one) is to just copy the jobs directory from the old server to the new one.

From the documentation Moving/copying/renaming jobs:

You can:

  1. Move a job from one installation of Jenkins to another by simply copying the corresponding job directory.
  2. Make a copy of an existing job by making a clone of a job directory by a different name.
  3. Rename an existing job by renaming a directory. Note that the if you change a job name you will need to change any other job that tries to call the renamed job.

Those operations can be done even when Jenkins is running. For changes like these to take effect, you have to click "reload config" to force Jenkins to reload configuration from the disk.

For me, I skipped this option because I was having a hard time finding where the jobs directory was on the old server. (Or just too lazy to find it, and I only had a couple jobs to copy over)

Option 2: Try one of the plugins out there

There are some Jenkins plugins out there that provide some job export options. Here are a couple...

Option 3: Use Jenkins CLI

This is what I used, which worked nicely for only the few jobs we had. If you have a large number of Jenkins jobs, you may consider the first aproach above.

  1. First download the Jenkins CLI jar.
  • You can do this from your jenkin's CLI page within your installed Jenkins instance.

jenkins CLI menu

  1. Next we can use the following command (pointing to the old server) to list the jobs.

java -jar jenkins-cli.jar -s http://<YourBuildServer>:<YourBuildServerPort>/ list-jobs

  1. Using one job from the list above, let's copy the xml of a job to the clipboard. (I'm using a Mac which is were pbcopy & pbpaste come from below)

java -jar jenkins-cli.jar -s http://<YourBuildServer>:<YourBuildServerPort>/ get-job "NAME_OF_JOB" | pbcopy

This uses the cli get-job "NAME_OF_JOB" command to print the job's xml to stdout, which we pipe to pbcopy on the Mac to load the configuration into the clipboard. You could of course pipe the output to a file like ... > job.xml

  1. If the above command placed a job's XML into the clipboard, you can use the below command to add it to the new server.

pbpaste | java -jar jenkins-cli.jar -s http://<YourBuildServer>:<YourBuildServerPort> create-job "NAME_OF_JOB"

This uses pbpaste to take what is in the clipboard, send it to stdin and pipe it to the Jenkins cli's create-job "NAME_OF_JOB" command.

Hope this helps...