Skip to content

Instantly share code, notes, and snippets.

@samrocketman
Last active April 19, 2019 17:29
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samrocketman/9391439 to your computer and use it in GitHub Desktop.
Save samrocketman/9391439 to your computer and use it in GitHub Desktop.
Use JENKINS_HOME as a git repository with the following .gitignore file.

I initialze $JENKINS_HOME as a git repository with the following .gitignore file.

#global types to ignore
*.swp

#Only get job configs and ignore other data
!jobs/*
jobs/*/*
!jobs/*/config.xml

#Ignore expanded plugins folders because we only want jpi files
#plugins/*/*

#Ignore all plugins
plugins

#Ignore logs
logs

There is a cron job which automatically updates this repository.

@daily /app/builder/dailycommit.sh

The contents of dailycommit.sh is the following.

#!/bin/bash
#Sam Gleske (sag47)
#Tue Mar  4 14:35:59 EST 2014
#Red Hat Enterprise Linux Server release 6.5 (Santiago)
#Linux 2.6.32-431.3.1.el6.x86_64 x86_64
#GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)

if [ -z "${JENKINS_HOME}" ];then
  echo "JENKINS_HOME not set." 1>&2
  exit 1
fi
if [ ! "${USER}" = "jboss" ];then
  echo "Must be jboss user to run daily commit." 1>&2
  exit 1
fi

export PATH="/usr/local/bin:${PATH}"

cd "${JENKINS_HOME}"
git add .
git add -u
git commit -m "daily commit $(date '+%a %m/%d/%Y')"
git push origin master
@samrocketman
Copy link
Author

Keep in mind a backup could be something as simple as backing up only the config.xml via git. Here's a better .gitignore file one could use for $JENKINS_HOME config backups. This accounts for the Cloudbees folder plugin jobs as well unlike the original.

#ignore all JENKINS_HOME except jobs directory, root xml config, and .gitignore file
/*
!/jobs
!/.gitignore
!/*.xml

#ignore all files in jobs subdirectories except for folders
#note: git doesn't track folders, only file content
jobs/**
!jobs/**/

#exclude only config.xml files in repository subdirectories
!config.xml

#as a result only settings and job config.xml files in JENKINS_HOME will be tracked by git

How does it work? The .gitignore file works from top to bottom. Following rules override former rules. So when a rule ignores a file and a following rule excludes it from being ignored the following rule takes precedence.

cd "$JENKINS_HOME"
#use the custom .gitignore file
git init
git commit -m 'config.xml backup before plugin upgrade'

@samrocketman
Copy link
Author

Contributed to open source project - github/gitignore#1763

@samrocketman
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment