Skip to content

Instantly share code, notes, and snippets.

@michaelboke
Last active December 13, 2015 22:39
Show Gist options
  • Save michaelboke/4986027 to your computer and use it in GitHub Desktop.
Save michaelboke/4986027 to your computer and use it in GitHub Desktop.
Jenkins branch job create/delete hook for git post-receive
#!/bin/bash
# Copyright (c) 2013 Michael boke
#
# This hook creates new jobs for each new branch in the jenkins continuous integration tool.
# Besides creating the job if needed, the user who pushed is added to the job's email list if they were not already there.
while read oldrev newrev refname ; do
case "$refname" in
refs/tags/*)
exit 0
;;
refs/heads/*)
short_refname=${refname##refs/heads/}
;;
*)
echo >&2 "*** Unknown type of update to $refname"
exit 1
;;
esac
#load config
#Config file should contain the following keys
#curl_args="--user git:git"
#jenkins_url="http://jenkinsserver:8080"
#jenkins_job="ProjectJobHere"
#ignored="master development"
source `dirname $0`/config.conf
#ignore all branches in the ignore list
if [[ " $ignored " =~ " $short_refname " ]] ; then
case "$short_refname" in
master)
/usr/bin/curl --fail ${curl_args} -X POST -s ${jenkins_url}/job/${jenkins_job}-master/build 2>/dev/null
;;
development)
/usr/bin/curl --fail ${curl_args} -X POST -s ${jenkins_url}/job/${jenkins_job}-development/build 2>/dev/null
;;
esac
exit 0
fi
#is branch deleted?
if [ ! -f ${refname} ]; then
#delete the old branch job
/usr/bin/curl --fail ${curl_args} -X POST -s ${jenkins_url}/job/${jenkins_job}-${short_refname}/doDelete
echo "Jenkins branch job removed: '${jenkins_job}-${short_refname}'"
else
#add or update job in jenkins
#get user who committed
if [ -z "$USER_EMAIL" ] ; then
USER_EMAIL=$(git log -1 --pretty=format:'%ce' $newrev)
fi
#get the branch job config
branch_config=$(/usr/bin/curl --fail ${curl_args} -s ${jenkins_url}/job/${jenkins_job}-${short_refname}/config.xml 2>/dev/null)
if [ $? -ne 0 ] ; then
#new job
master_config=$(/usr/bin/curl --fail ${curl_args} -s ${jenkins_url}/job/${jenkins_job}/config.xml 2>/dev/null)
if [ $? -ne 0 ] ; then
echo "Cannot create jenkins branch job with master template ${jenkins_job}, not found"
exit 0
fi
#replace master with branch and replace branch name with this one
branch_config="${master_config/<name>\*\/master</<name>*/${short_refname}<}"
#add committer email to recipients list, if he does not exist yet
if [ "${branch_config/$USER_EMAIL/}" == "$branch_config" ] ; then
branch_config="${branch_config/<recipients>/<recipients>$USER_EMAIL }"
fi
#send the new job
/usr/bin/curl --fail ${curl_args} --data "$branch_config" -H "Content-Type: text/xml" -s ${jenkins_url}/createItem?name=${jenkins_job}-${short_refname} 2>/dev/null
if [ $? -ne 0 ] ; then
echo "Failed to create a new jenkins job '${jenkins_job}-${short_refname}'";
exit 0;
fi
#enable job build
/usr/bin/curl --fail ${curl_args} -X POST -s ${jenkins_url}/job/${jenkins_job}-${short_refname}/enable 2>/dev/null
echo "Jenkins branch job created: '${jenkins_job}-${short_refname}'"
else
#existing job
#add committer email to recipients list, if he does not exist yet
if [ "${branch_config/$USER_EMAIL/}" == "$branch_config" ] ; then
#add email
branch_config="${branch_config/<recipients>/<recipients>$USER_EMAIL }"
#update job
/usr/bin/curl --fail ${curl_args} --data "$branch_config" -H "Content-Type: text/xml" -s ${jenkins_url}/job/${jenkins_job}-${short_refname}/config.xml 2>/dev/null
if [ $? -ne 0 ] ; then
echo "Failed to updatea jenkins job '${jenkins_job}-${short_refname}'";
fi
fi
fi
#trigger job build
/usr/bin/curl --fail ${curl_args} -X POST -s ${jenkins_url}/job/${jenkins_job}-${short_refname}/build 2>/dev/null
echo "Jenkins job building: '${jenkins_job}-${short_refname}'"
fi
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment