Wednesday, January 22, 2014

Backing up Jenkin Jobs

Backing up Jenkin Jobs


There are plugins that will back you full Jenkins server (https://wiki.jenkins-ci.org/display/JENKINS/Backup+Plugin). But what if in your development team you want to backup only your jobs and add the configuration of each job to your source control.
A simple way to do this is to write a batch file that uses the Jenkins cli. To get the jenkins-cli.jar you need to go to your server at the url: http://build_server/cli/. You should get the following:


You can download the jar from the link on the top of the page.
For each command you can see how to call it.
We will use two commands. The first is to get a list of the jobs you want to backup:
java -jar jenkins-cli.jar -s http://buildevsrv list-jobs abc > jobs.list
This will save the list, filtered by the work “abc” to a file jobs.list.
The for each entry in the file we do the following command:
java -jar jenkins-cli.jar -s http://buildevsrv get-job "job_name" --username %1 --password %2 > job_name.xml
Enter your user name and password (you need permission for configuration of the job). This will save the configuration to your disk. Now you can save it in your source control.

For a full script to get all jobs by a filter:
@echo off
echo getting list of jobs
java -jar jenkins-cli.jar -s http://buildevsrv list-jobs abc > jobs.list
for /f "delims=" %%i in (jobs.list) do (
                if NOT [%1] == [] (
                                echo getting config for: %%i
                                java -jar jenkins-cli.jar -s http://buildevsrv get-job "%%i" --username %1 --password %2 > %%i.xml
                )
)

Don’t forget to pass your username and password to the command.









No comments:

Post a Comment