Skip to content

Instantly share code, notes, and snippets.

@bspaulding
Created June 27, 2012 12:25
Show Gist options
  • Save bspaulding/3003731 to your computer and use it in GitHub Desktop.
Save bspaulding/3003731 to your computer and use it in GitHub Desktop.
jenkins-post-receive
#!/bin/bash
#
# jenkins-post-receive
#
# Configuraton
# ------------
# jenkins-post-receive.enabled
# [OPTIONAL] Defaults to off (false). Treated as a boolean with git config --bool.
#
# jenkins-post-receive.jenkins-cli-jar-path
# [REQUIRED] Defaults to ""
# This value should contain the full path to jenkins-cli.jar. Do not trust your path. Absolute paths work best.
#
# Example:
# git config jenkins-post-receive.jenkins-cli-jar-path "/usr/local/bin/jenkins-cli.jar"
#
# jenkins-post-receive.jenkins-url
# [REQUIRED] Defaults to "".
# This value should contain the url to jenkins.
#
# jenkins-post-receive.template-job-name
# [OPTIONAL] Defaults to "#{jenkins.job-prefix}jenkins-post-receive-template"
# The xml config of this job is used as the template for other jobs.
#
# jenkins-post-receive.job-prefix
# [OPTIONAL] Defaults to "".
# This value is used to prefix refnames as job names in jenkins.
#
# Example:
# If I set jenkins.job-prefix to 'my-repo-name-', jobs in jenkins will be
# named like 'my-repo-name-master', 'myrepo-name-branch-foo', etc.
#
# Make sure we're enabled, and do nothing if we aren't.
# This is not an early exit, so that we can play nice
# in ConcatedHookScriptLandia.
enabled=$(git config --bool jenkins-post-receive.enabled)
if [ "$enabled" = "true" ]; then
# Setup Configuration Variables
jobPrefix=$(git config jenkins-post-receive.job-prefix)
jenkinsUrl=$(git config jenkins-post-receive.jenkins-url)
jenkinsCliJarPath=$(git config jenkins-post-receive.jenkins-cli-jar-path)
zero="0000000000000000000000000000000000000000"
while read oldrev newrev refname
do
if [ "$newrev" = "$zero" ]; then
newrev_type=delete
else
newrev_type=$(git cat-file -t $newrev)
fi
short_refname=${refname##refs/*/}
jobName="$jobPrefix$short_refname"
if [ "$oldrev" = "$zero" -a "$newrev_type" = "commit" ]; then
echo "[jenkins-post-receive] detected new ref: '$refname'"
echo "[jenkins-post-receive] creating jenkins job: '$jobName'"
jenkinsTemplateJobName=$(git config jenkins-post-receive.template-job-name)
if [ "$jenkinsTemplateJobName" = "" ]; then
jenkinsTemplateJobName="$jobPrefix"jenkins-post-receive-template
fi
jobConfigTemplate=$(java -jar $jenkinsCliJarPath -s $jenkinsUrl get-job $jenkinsTemplateJobName)
branchMatcherFind="<branches>*<hudson\.plugins\.git\.BranchSpec>*<name>*<\/name>*<\/hudson\.plugins\.git\.BranchSpec>*<\/branches>"
branchMatcherReplace="<branches><hudson\.plugins\.git\.BranchSpec><name>$short_refname<\/name><\/hudson\.plugins\.git\.BranchSpec><\/branches>"
jobConfig=${jobConfigTemplate/branchMatcherFind/branchMatcherReplace}
java -jar $jenkinsCliJarPath -s $jenkinsUrl create-job $jobName <<EOF
$jobConfig
EOF
fi
if [ "$newrev" = "$zero" ]; then
echo "[jenkins-post-receive] detected deleted ref: '$refname'"
echo "[jenkins-post-receive] deleting jenkins job: '$jobName'"
java -jar $jenkinsCliJarPath -s $jenkinsUrl delete-job $jobName
fi
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment