It's about open source softwares

Thursday, July 2, 2015

Backup and Restore JENKINS Jobs


In an organization Jenkins plays important role achieving continuous integration and deployment/delivery a.k.a CI/CD pipeline. There are other tools present for achieving CI/CD like Travis, Jetbrains, Bamboo, etc. I am using Jenkins because it is popular in open source community.

While automating the tasks we are making changes in Jenkins jobs. Over the period of time Jenkins server is filled with lot of jobs. In case failure of Jenkins server, migrating Jenkins server to another machine or accidentally deletion jobs cause lot of trouble for DevOps. It is necessary to have backup of Jenkins jobs, so that we can restore them in short period of time. There are several plugins present for Jenkins like backup-plugin, configuration-history-plugin which does job for you. For this you have to do additional configurations. 

Jenkins provides REST API for managing jobs easily. Jenkins stores the job configuration in config.xml file that is present in the root directory of job. If you are OK with creating/migrating jobs without build number, build history, etc. then we can create/migrate jobs very easily with the help of jenkins's job REST API.

Backup Jenkins Job


Taking backup of Jenkins job is quite easy, you just have to login to your Jenkins server through browser. After login append "/job/job-name/config.xml" to URL. It will show job configuration. Just save the config.xml file contents.  

Here, I am backing up automerge-test job.


Jenkins Job Backup
 If you are familiar with CURL then using GET method you can get config.xml file.

Restore Jenkins Job


Restoring of jenkins jobs are as simple as taking backup. 
  • Create an empty job in Jenkins
  • POST the config.xml to Jenkins server using POSTMAN 

First you have to create an empty Jenkins job on the same server or new server in case of migrating jobs in to another server.

Create empty jenkins job

Newly created automerge-test job doesn't have anything right now.

Empty Configuration

Now install POSTMAN extension to your browser. Using POST method provide a job configuration to jenkins server. Use http://<jenkins_server_name>/job/<job_name>/config.xml in URL and paste config.xml content in body section with raw-text formatting.

Press Send button to post the config.xml contents to Jenkins server.

Restore Job using POSTMAN


 Using POST API configurations filled in newly created job. Confirm Job configuration in Jenkins.

Job Configuration

Jenkins jobs are backup and restore successfully using REST API.

Share:

0 comments:

Post a Comment