Jenkins: import a job to another server

By | 03/13/2019

We have an old Jenkins with a bunch of iOS jobs which needs to be copied to a new Jenkins instance.

Documentation suggests to:

Move a job from one installation of Jenkins to another by simply copying the corresponding job directory.

I.e. the first solution is just to copy a whole jobs directory or copy a per-project job’s catalog.

Disadvantage (or advantage – depends on an exact task) of such approach is that with a job’s directory a whole its history, artifacts, logs etc will be copied as well.

Another solution is to use the Jenkins CLI and allows to copy a full job’s configuration – but it will be created “from scratch”.

Jenkins CLI

Go to Manage Jenkins > Jenkins CLI and download the CLI’s jar-file:

Download:

[simterm]

$ sudo wget -O /usr/local/bin/jenkins-cli.jar https://jenkins.example.com/jnlpJars/jenkins-cli.jar

[/simterm]

Check it – in -auth pass your user:pass or user:api_token, in -s – Jenkin’s host URL, here it’s running locally using Jenkin’s own CLI file:

[simterm]

root@jenkins:/backups# java -jar /data/jenkins/war/WEB-INF/jenkins-cli.jar -auth user:pass -s http://localhost:8080/ version
2.89.4

[/simterm]

Now import the Unit Test job from the iOS Projectname directory into the Projectname_Unit_Test_job.xml file:

[simterm]

root@jenkins:/backups# java -jar /data/jenkins/war/WEB-INF/jenkins-cli.jar -auth user:pass -s http://localhost:8080/ get-job 'iOS Projectname/Unit Test' > Projectname_Unit_Test_job.xml

[/simterm]

Check the file’s content:

[simterm]

root@jenkins:/backups# head Projectname_Unit_Test_job.xml
<?xml version='1.0' encoding='UTF-8'?>
<project>
  <actions/>
  <description>Runs test, and delivers feedback to slack</description>
  <keepDependencies>false</keepDependencies>
  <properties>
    <com.coravy.hudson.plugins.github.GithubProjectProperty plugin="[email protected]">
      <projectUrl>https://github.com/Projectname-dev/Projectname-iOS/</projectUrl>
      <displayName></displayName>
    </com.coravy.hudson.plugins.github.GithubProjectProperty>

[/simterm]

And export it to the iOS Projectname on a new Jenkins server: read the file’s content using the cat and then pass data to the CLI via pipe |:

[simterm]

root@jenkins:/backups# cat Projectname_Unit_Test_job.xml | java -jar /data/jenkins/war/WEB-INF/jenkins-cli.jar -auth user:pass -s https://ci.example.com create-job 'iOS Projectname/Unit Test'

[/simterm]

Check it:

Done.