Skip to content

Instantly share code, notes, and snippets.

@raymyers
Created March 1, 2020 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raymyers/947b99f51e575aba92a1b525ae1e88a5 to your computer and use it in GitHub Desktop.
Save raymyers/947b99f51e575aba92a1b525ae1e88a5 to your computer and use it in GitHub Desktop.
Backup Jenkins Config Daily Job
# Last tested in Jenkins 2.22
*
!/.gitignore
!/*.xml
!/nextBuildNumber
!/jobs
!/jobs/*
!/jobs/*/*.xml
/jobs/*/disk-usage.xml
/jobs/*/builds
!plugins
!plugins/*.jpi
!/users
!/users/**/
!/users/**/*.xml
cd $JENKINS_HOME
git config --global user.email "jenkins@example.com"
git config --global user.name "Jenkins"
git add -A .
git status
git diff-index --quiet HEAD || git commit -m "Automated Jenkins commit"
git push origin master
  1. Create an empty repo in your Git provider of choice (GitHub, Bitbucket, Gitlab). I usually call it jenkins-config.
  2. Login to your Jenkins machine, cd into JENKINS_HOME (e.g. /var/lib/jenkins).
  3. To avoid permissions issues do this as the same user Jenkins runs as.
  4. git init and setup your remote origin.
  5. In the Jenkins interface, create job StoreConfigInGit and paste the contents of run.sh in an Execute Shell block.
  6. One it runs successfully, configure it to "Build Periodically" @daily
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment