Skip to content

Steve Hill

How Do I Backup Jenkins Jobs?

jenkins, grunt-jenkins1 min read

Making changes to a Jenkins instance that has been around for a while can make even the most experienced developers nervous. Jenkins configurations tend to have a number of things in common:

  • They grow massively over time
  • Their intent isn't always clear
  • They are often put together by different people at different times

Assumptions

  • Your Jenkins server is located at http://localhost:8080
  • Your Jenkins job is named deploy-prod

Backup Jobs

A reasonable way to deal with this anxiety is to backup the configuration. Fortunately, that's easy with Jenkins. Simply point your browser to http://localhost:8080/job/deploy-prod/config.xml and save the XML (example below) to disk.

1<?xml version='1.0' encoding='UTF-8'?>
2<project>
3 <actions/>
4 <description>deploys to cloud instance</description>
5 <keepDependencies>false</keepDependencies>
6 <properties/>
7 <scm class="hudson.scm.NullSCM"/>
8 <canRoam>true</canRoam>
9 <disabled>false</disabled>
10 <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
11 <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
12 <triggers class="vector"/>
13 <concurrentBuild>false</concurrentBuild>
14 <builders>
15 <hudson.tasks.Shell>
16 <command>echo "deploying to prod"</command>
17 </hudson.tasks.Shell>
18 </builders>
19 <publishers/>
20 <buildWrappers/>
21</project>

Restore Jobs

A backup is only as good as your confidence in restoring it, so it's a good idea to take a few minutes before making any of those big changes and see if you can restore the configuration. I'd suggest changing the description, as it's a low-risk update that is easily visible. To restore your configuration, you'll need a tool like Postman[1], where you can paste the XML contents in the body and POST to http://localhost:8080/job/deploy-prod/config.xml

Postman Restore Jenkins Job

Automate It

This quickly becomes a repetitive and mundane task that is perfect to automate which is exactly what I've done for the grunt JavaScript build tool.

After installing grunt-jenkins[2][3] from npm, include it in your grunt.js gruntfile and add the jenkins configuration block:

1grunt.initConfig({
1// all your other config
2
3 jenkins: {
4 serverAddress: 'http://ci.mycompany.com:8080',
5 pipelineDirectory: 'ci' //optional, defaults to 'pipeline'
6 }
7});

Usage is simple:

1grunt jenkins-backup-jobs

This command will contact your Jenkins server, get a list of all the jobs, and save every configuration to pipeline/job-name/config.xml.

To see how restoring works, make a small change to a job's description in the XML and run:

1grunt jenkins-install-jobs

Now check out your change on the Jenkins server. Pipeline configuration instantly has the respect it deserves in your source tree.

  1. Available for Google Chrome only in the Chrome Web Store.
  2. grunt-jenkins on github
  3. grunt-jenkins on npm