Created
August 21, 2013 07:11
-
-
Save jedvardsson/6291202 to your computer and use it in GitHub Desktop.
Git post-receive hook for triggering release builds and dev build in Jenkins for selfhosted Git servers. Dev builds are triggered using Jenkins polling mechanism when branches are commited to. The release builds are triggered when tags are pushed.
This file contains hidden or 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 post-receive hook for triggering release builds and dev build in | |
## Jenkins for selfhosted Git servers. Dev builds are triggered using Jenkins | |
## polling mechanism when branches are commited to. The release builds are | |
## triggered when tags are pushed. | |
## Naming conventions | |
## | |
## Bare git repo: myproj.git | |
## Jenkins dev job: myproj | |
## Jenkins release job: myproj-release | |
JENKINS_URL="http://jenkins" | |
JENKINS_CREDENTIALS="someuser:apassword" | |
REPO_PATH="$(readlink -f "$(git rev-parse --git-dir)")" | |
JOB_NAME="$(basename $REPO_PATH .git)" | |
REL_JOB_NAME="$JOB_NAME-release" | |
############ | |
# Remove trailing slash if any | |
JENKINS_URL="${JENKINS_URL%/}" | |
while read oldval newval refname; do | |
# Ignore if we are deleting something | |
[ $newval = '0000000000000000000000000000000000000000' ] && break | |
case $refname in | |
refs/tags/*) | |
# Adding or updating tag | |
res="$(curl -sSf -u $JENKINS_CREDENTIALS "$JENKINS_URL/job/$REL_JOB_NAME/buildWithParameters" --data-urlencode "GIT_TAG=$refname" 2>&1)" | |
status=$? | |
case $status in | |
22) echo "Couldn't trigger Jenkins job for: $REL_JOB_NAME" ;; | |
0) echo "Triggered Jenkins job $REL_JOB_NAME: $refname" ;; | |
*) echo "Failed to trigger Jenkins job $REL_JOB_NAME: $res";; | |
esac | |
;; | |
refs/heads/*) | |
# Adding or updating a branch | |
res="$(curl -sSf -u $JENKINS_CREDENTIALS -X POST "$JENKINS_URL/job/$JOB_NAME/polling" 2>&1)" | |
status=$? | |
case $status in | |
22) echo "Couldn't trigger polling of Jenkins job for: $JOB_NAME" ;; | |
0) echo "Triggered polling of Jenkins job $JOB_NAME: $refname" ;; | |
*) echo "Failed to trigger polling of Jenkins job $JOB_NAME: $res";; | |
esac | |
;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment