Skip to content

Instantly share code, notes, and snippets.

@ronensh-nice
Forked from cenkalti/jenkins-home-git.sh
Last active February 28, 2019 14:52
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 ronensh-nice/3ca9b1dd6778ca2169e7ce3dfd7a174c to your computer and use it in GitHub Desktop.
Save ronensh-nice/3ca9b1dd6778ca2169e7ce3dfd7a174c to your computer and use it in GitHub Desktop.
Backup Jenkins home periodicallly with git.
#!/bin/bash
# Setup
#
# - Create a new Jenkins Job
# - Mark "None" for Source Control Management
# - Select the "Build Periodically" build trigger
# - configure to run as frequently as you like
# - Add a new "Execute Shell" build step
# - Paste the contents of this file as the command
# - Save
#
# NOTE: before this job will work, you'll need to manually navigate to the $JENKINS_HOME directory
# and do the initial set up of the git repository (git init).
# Make sure the appropriate remote is added and the default remote/branch set up (git clone).
#
# Jenkins Configurations Directory
cd $JENKINS_HOME
# Add general configurations, job configurations, and user content
git add -- *.xml userContent/* secrets nodes
# only add jobs config if they exist
if [ -d jobs ]; then
find -L jobs -type f -iname config.xml -follow -print0 -exec git add {} \;
else
echo "[Error] missing jobs folder"
exit 1
fi
# only add user configurations if they exist
if [ -d users ]; then
find -L users -type f -iname config.xml -follow -print0 -exec git add {} \;
else
echo "[Error] missing users folder"
exit 1
fi
# add plugins
git add plugins/*.jpi*
# mark as deleted anything that's been, well, deleted
to_remove=`git status | grep "deleted" | awk '{print $2}'`
if [ -n "$to_remove" ]; then
git rm --ignore-unmatch $to_remove
else
echo "[Info] no files to be removed"
fi
echo "[Info] committing changes"
git commit -m "Automated Jenkins commit from `hostname` (master) node, `date`"
echo "[Info] pushing commit to git repository"
git push -q -u origin master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment