Jenkins is an open-source CI/CD automation tool for delivering projects regardless of the platform. In this tutorial, we'll learn to export and import Jenkins jobs between two different machines.
First, we'll start the Jenkins server in a Docker container. Later, we'll export a job from the source machine to the target machine using the Job Import Plugin and Jenkins XML configurations.
2. Initial Setup
Before we start, let's first create a setup for the demo. Here, the setup includes two Linux VMs, a source machine, and a target machine. Naturally, there are different ways to install Jenkins in Linux. Usually, the easiest approach is a Docker container. Of course, the Jenkins server must be running on both machines.
Also, a sample job is required for us to export and import. To create a job, we need to follow several steps:
- Click on New Item in the Jenkins dashboard
- Set a Job Name (we use sampleJob)
- Select the Job Type (we use Freestyle project)
- Save the job
The above steps will create a basic job on the source Jenkins machine.
3. Export/Import Jobs in Jenkins
In any case, Jenkins jobs can be moved to different servers in many ways. However, to migrate Jenkins jobs files from one instance to another, we'll need to ensure proper connectivity between the source and target machines.
3.1. Using the Job Import Plugin
In Jenkins, the Job Import Plugin allows us to migrate jobs. In fact, all plugins can be managed and installed from the Jenkins UI. Of course, we install the Job Import Plugin on the target machine.
Now, to import jobs, we've to make changes in the configurations of the target Jenkins machine. First, from the Jenkins dashboard, we go to Manage Jenkins > Configure System. Next, we scroll down to the Job Import Plugin section.
Finally, we'll get a Job Import Plugin item in the left navigation bar of the Jenkins dashboard. Clicking on it, we can see the options the plugin provides.
Henceforth, we can get all the jobs of the source machine by clicking the query button. Additionally, when we use the Import checkbox, the job from the source machine will be imported to the target machine.
3.2. Using the jenkins-cli.jar File
Furthermore, we can also migrate Jenkins jobs using the jenkins-cli.jar file. To download jenkins-cli.jar, we can go to http://JENKINS_IP:JENKINS_PORT/jnlpJars/jenkins-cli.jar.
Moreover, to export a job from the source machine, we require the password of the source machine's Jenkins user. This password is in the stdout logs of the Docker container.
Let's now look at the command to get the configuration file of the sampleJob from the source machine:
$ java -jar jenkins-cli.jar -s http://JENKINS_USER:[email protected]_IP:JENKINS_PORT get-job sampleJob > sampleJob.xml
Here, we have several pieces of information, which we need to update with their appropriate values from the source machine:
Importantly, we must run the command from the directory where jenkins-cli.jar resides. Also, using the get-job option, we get the job dump in an XML format.
Let's check out the command to import the sampleJob.xml into the target machine:
$ java -jar jenkins-cli.jar -s http://JENKINS_USER:[email protected]_IP:JENKINS_PORT create-job sampleJob < sampleJob.xml
Again, the values need to be replaced, but this time according to the target machine.
Finally, the above command reads the sampleJob.xml file and imports the new job on the target machine.
In this article, we demonstrated how to export and import Jenkins jobs on different Jenkins servers. First, we learned to export and import using the Job Import Plugin. After that, we did the same with the jenkins-cli.jar file.