Backup Jenkins Jobs to Git Repo Automatically

Because I set up many Jenkins jobs on my home server, and change it if need some time, but Jenkins job configuration does not provide history and a way to roll back. So this post shows to set up a job to backup jobs configuration to a git repo daily if any change.

Install git in Jenkins server and config git user first.
e.g. Jenkins Bot, jenkins.bot@xxx.me

Implement it via a shell script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/bash

# Jenkins Configuraitons Directory
cd /var/jenkins_home/jobs/

# Add all configuations files, including general configurations, job configurations
git add */*.xml

# mark as deleted anything that's been, well, deleted
to_remove=`git status | grep "deleted" | awk '{print $3}'`

if [ -n "$to_remove" ]; then
git rm --ignore-unmatch $to_remove
fi

git commit -m "Automated Jenkins commit"

git push http://username:password@github.com/xxxx/xxxx -q

I set up the backup job to run daily automatically, 3AM everyday.

then, you will see the git commit history every day.