Created
October 4, 2011 20:17
-
-
Save evilchili/1262665 to your computer and use it in GitHub Desktop.
function library for git hooks & a post-receive hook for automatic branch building
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
GIT_DIR=$(git rev-parse --git-dir 2>/dev/null) | |
if [ -z "$GIT_DIR" ]; then | |
echo >&2 "fatal: hooks/functions: GIT_DIR not set" | |
exit 1 | |
fi | |
read oldrev newrev refname | |
# --- Interpret | |
# 0000->1234 (create) | |
# 1234->2345 (update) | |
# 2345->0000 (delete) | |
if expr "$oldrev" : '0*$' >/dev/null | |
then | |
change_type="create" | |
else | |
if expr "$newrev" : '0*$' >/dev/null | |
then | |
change_type="delete" | |
else | |
change_type="update" | |
fi | |
fi | |
# --- Get the revision types | |
newrev_type=$(git cat-file -t $newrev 2> /dev/null) | |
oldrev_type=$(git cat-file -t "$oldrev" 2> /dev/null) | |
case "$change_type" in | |
create|update) | |
rev="$newrev" | |
rev_type="$newrev_type" | |
;; | |
delete) | |
rev="$oldrev" | |
rev_type="$oldrev_type" | |
;; | |
esac | |
# The revision type tells us what type the commit is, combined with | |
# the location of the ref we can decide between | |
# - working branch | |
# - tracking branch | |
# - unannoted tag | |
# - annotated tag | |
case "$refname","$rev_type" in | |
refs/tags/*,commit) | |
# un-annotated tag | |
refname_type="tag" | |
short_refname=${refname##refs/tags/} | |
;; | |
refs/tags/*,tag) | |
# annotated tag | |
refname_type="annotated tag" | |
short_refname=${refname##refs/tags/} | |
# change recipients | |
if [ -n "$announcerecipients" ]; then | |
recipients="$announcerecipients" | |
fi | |
;; | |
refs/heads/*,commit) | |
# branch | |
refname_type="branch" | |
short_refname=${refname##refs/heads/} | |
;; | |
refs/remotes/*,commit) | |
# tracking branch | |
refname_type="tracking branch" | |
short_refname=${refname##refs/remotes/} | |
;; | |
*) | |
# Anything else (is there anything else?) | |
refname_type="unknown" | |
;; | |
esac | |
describe=$(git describe $rev 2>/dev/null) | |
if [ -z "$describe" ]; then | |
describe=$rev | |
fi | |
projectdesc=$(sed -ne '1p' "$GIT_DIR/description") | |
# Check if the description is unchanged from it's default, and shorten it to | |
# a more manageable length if it is | |
if expr "$projectdesc" : "Unnamed repository.*$" >/dev/null | |
then | |
projectdesc="UNNAMED PROJECT" | |
fi | |
# some email helpers | |
recipients=$(git config hooks.mailinglist) | |
announcerecipients=$(git config hooks.announcelist) | |
envelopesender=$(git config hooks.envelopesender) | |
emailprefix=$(git config hooks.emailprefix || echo '[SCM] ') | |
custom_showrev=$(git config hooks.showrev) | |
# get the name of the target branch for builds | |
regex='refs/heads/(.*)' | |
if [[ $refname =~ $regex ]]; then | |
target_branch="${BASH_REMATCH[1]}" | |
fi | |
# how to spawn builds in jenkins | |
jenkins_url="http://jenkins//job" | |
jenkins_token="SEEEKRIT" | |
get_autobuild_options() { | |
configfile_hash=$(git ls-tree $newrev | grep autobuild.cfg | cut -d' ' -f 3 | cut -f 1 ) | |
if [[ "$configfile_hash" != "" ]] ; then | |
autobuild_options=$( git cat-file blob "$configfile_hash" | egrep -i "^\s*${target_branch}=" | cut -d= -f 2 ) | |
echo "autobuild options detected: '${autobuild_options}'" | |
autobuild_enabled=$( echo "$autobuild_options" | grep '\<autobuild\>' ) | |
fi | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
job="MyJenkinsJob" | |
# load the git hook functions library | |
. git-hook-functions.sh | |
# read the autobuild configuration | |
get_autobuild_options | |
if [[ $autobuild_enabled ]] ; then | |
function trigger_build { | |
echo "Automatically scheduling new build against origin/$target_branch: $jenkins_url/$job" | |
wget -q -O - "$jenkins_url/$job/buildWithParameters?token=$jenkins_token&BRANCH=$target_branch" > /dev/null | |
} | |
case "$change_type" in | |
create) | |
if expr "$newrev" : '0*$' >/dev/null; then | |
echo "$refname is newly created, so not triggering a build." | |
else | |
trigger_build | |
fi | |
;; | |
update) | |
trigger_build | |
;; | |
delete) | |
echo "$refname is being deleted, so not triggering a build." | |
;; | |
esac | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment