43

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?

Isaac
  • 9,372
  • 4
  • 28
  • 31

10 Answers10

35

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.

Magnus
  • 1,261
  • 1
  • 12
  • 20
  • 10
    Note that you will have to reload the configuration from disk for the copied job to appear on the new Jenkins instance. There is a link in the admin pages to perform the reload. You should add that to your answer. – K Erlandsson Nov 27 '15 at 14:07
  • 1
    If you won't make sure first that you have all relevant plug-ins installed any migration won't work for you as Jenkins won't know how to read the sections from the xml files – Dvir669 May 22 '16 at 10:39
16

There is a very plugin to do that online without file copies (which also works): the Job Import plugin: https://plugins.jenkins.io/job-import-plugin/

regards

didier

Nahum
  • 6,959
  • 12
  • 48
  • 69
Didier Durand
  • 627
  • 7
  • 16
7

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
jprism
  • 3,239
  • 3
  • 40
  • 56
4

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. :)

Amol Manthalkar
  • 1,890
  • 2
  • 16
  • 16
4

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)

Community
  • 1
  • 1
Christian.D
  • 879
  • 1
  • 9
  • 21
2

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.

Roberto
  • 2,115
  • 24
  • 32
  • If you want to preserve the build numbering sequences (perhaps you record these to Jira for example) you have to copy the number in the build number file too. Its in that same folder beside config.xml. – Lee Meador Nov 15 '16 at 15:43
  • Don't forget that the config.xml contains stuff related to plugins. If you have different plugins in the new Jenkins, you will want to go to the Jenkins configure screen for any copied jobs to check on the settings and save again. – Lee Meador Nov 15 '16 at 15:45
0

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!

Dvir669
  • 6,577
  • 1
  • 20
  • 21
  • Good luck because you're advocating Perl? This may work, but requires several things to be true: A> the right version of java available, B> the presence of " old-jenkins-cli.jar" and "new-jenkins-cli.jar", C> the use of a now deprecated form of cli commands, D> working Perl, E> That old and new server have the same (or VERY similar) plugin and configuration, F> That the copied jobs aren't relying on some other content like credentials or local environment. – Steven the Easily Amused Sep 29 '17 at 00:57
  • Hi Steven. I didn't say "use Perl", it is what I used for that specific task. It is part of a larger automation for a staging environment that makes sure the same plugin versions, environment and other configurations will be applied when arranging new server automatically. As the question was regarding this section, I've shared it. It doesn't have to be Perl, and it only shows another programmatic method as a possibility to achieve that. Also, none of the answers addressed mismatch of the plug-ins or environment configurations, so I fail to understand your point. – Dvir669 Sep 30 '17 at 02:19
  • I also sign most of my answers with Good Luck. – Dvir669 Sep 30 '17 at 02:25
  • @Dvir669 Will this work if the job has a Password Parameter with a default value? I've found that copying such jobs between Jenkins servers causes the default value to become corrupt. – pcjr May 17 '18 at 19:34
  • I never tested it on jobs with configured passwords, so I'm not sure. The script is very basic, and shows another approach, but I'm sure today there are many better ways to achieve this. – Dvir669 May 17 '18 at 19:56
  • seems i'm not a lucky guy..connections failed due to error password.. – Han.Oliver Jan 07 '20 at 02:26
0
  1. Copy the job in the existing folder
  2. Move the job to the new folder
Mo0gles
  • 10,517
  • 2
  • 21
  • 15
0

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

scp -r ~jenkins/jobs/{sourceJobName} user@remote:~jenkins/jobs/
MonoThreaded
  • 11,429
  • 12
  • 71
  • 102
0

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 ⚠️

fusion27
  • 2,396
  • 1
  • 25
  • 25