37
votes

I am setting up a new server to run Jenkins. I have an existing Jenkins server with jobs in place. Now, I want to copy the jobs over from the old instance to the new instance.

On the new instance I am at the New Job screen. I notice that there is a "copy existing job" option. When I put in the path to the job on the old instance, I keep getting an error saying "no such job at http://old-instance/job/jobName".

How can I copy a job from one instance to another?

10
And you are sure that you don't just want to set up your new server as a jenkins slave, such that jobs will be distributed among both servers?Bjarke Freund-Hansen
No, the old machine is being retired.Isaac

10 Answers

31
votes

According to the manual, https://wiki.jenkins-ci.org/display/JENKINS/Administering+Jenkins, it's simply to move the corresponding job directory to the new Jenkins instance.

The "Copy existing Job" option requires the job to exist on the current Jenkins instance. It's an option to use the existing job as a template. It can't be used to move jobs between instances.

16
votes

There is a very plugin to do that online without file copies (which also works): the Job Import plugin: https://wiki.jenkins-ci.org/display/JENKINS/Job+Import+Plugin

regards

didier

7
votes

There are atleast three ways

  1. Copy jobs directory
  2. Use Import Plugin https://wiki.jenkins-ci.org/display/JENKINS/Job+Import+Plugin
  3. Use Jenkins CLI to list the jobs, get the job xml and then create a job on new server based on the xml
4
votes

Job Import Plugin is the easiest way to copy jobs from one jenkins instance to other. If that is not working due to some security concerns you can just copy the entire jobs directory from old jenkins instance into new jenkins instance. Jobs are present in {Jenkins_Home}\jobs.. on the server.

Once you are done with copy, just reload the configuration on new instance. You can reload it from Jenkins->Manage Jenkins->Reload Configuration From Disk

Once the reload is complete you can see your jobs. :)

3
votes

You could also do this on the commandline:

  1. Gather authentication API tokens from the two jenkins instances for your user: http://<jenkins-server>/user/<username>/configure
  2. Curl command to create a Job on the new instance from the config of the old instance: curl -k -s http://<user>:<token>@OLD_JENKINS/job/JOBNAME/config.xml | curl -k -X POST https://<user>:<token>@http://NEW_JENKINS/createItem?name=JOBNAME --header "Content-Type: application/xml" -d @-

(courtesy of https://stackoverflow.com/a/30575318/3891027)

1
votes

You just need to create a directory with the same name as the job and copy the config.xml file from the original Jenkins.

Just beware that if you need to preserve the build numbering you'll need to copy the number in the build number file, and be careful if you have different plugins on both Jenkins servers, as @LeeMeador says in his comments.

0
votes

I used Perl to automate such processes using the CLI (go to $jenkins_URL/cli/ and download the Jenkins-cli.jar) (script changed to your scenario)

It is important to make sure that you have all necessary plug-ins installed on your new Jenkins otherwise any migration method won't work...

my $newJobPrefix = "New_Jenkins";
my $jobPrefix = "Old_Jenkins";
my $result = `"$java" -jar old-jenkins-cli.jar -noKeyAuth -s $jenkins_URL list-jobs All`;
@jobList = split("\n", $result);
foreach my $job (@jobList)
{
    # ---- Getting configuration of jobs ----
    print "Getting config for: $job \n";
    my $config = `"$java" -jar $jenkinsJar -noKeyAuth -s $jenkins_URL get-job $job`;

    my $file = "$jobPrefix\\$job.xml";
    unless(open FILE, '>', $file) {die "Unable to open $file";}
    print FILE $config;
    close FILE;

    # ---- Adding Job to new Jenkins ----
    my $result = `"$java" -jar new-jenkins-cli.jar -noKeyAuth -s $New_jenkins_URL create-job $job< $file`;
}

Good luck!

0
votes
  1. Copy the job in the existing folder
  2. Move the job to the new folder
0
votes

I would recommend copying jobs directories from ~jenkins/jobs directly into your destination with scp:

scp -r ~jenkins/jobs/{sourceJobName} user@remote:~jenkins/jobs/
0
votes

via Jenkins CLI

  • .jenkins-cli & .jenkins-cli_newserver are single-line files which contain

    username:userapikey

  • Piping XML output from old server get-job response into new server create-job request.

    java -jar ~/jenkins-cli.jar \
        -s http://oldserver:8080 \
        -auth @/.jenkins-cli \
        get-job "job name" \
    | java -jar ~/jenkins-cli_newserver.jar \
        -s http://newserver:8080 \
        -auth @/.jenkins-cli_newserver \
        create-job "new or existing job name"
    

    ⚠️ Note that create-job can rename the job ⚠️