Skip to content

Commit 7d48cc9

Browse files
committed
Add a sample git hook
This helps you kick off jenkins builds if you need to login before kicking the special magic url. In my Jenkins instance this is called: "Build Triggers" -> "Trigger builds remotely (e.g., from scripts)" I had to set the "Authentication Token" to something manually. I used `pwgen` to pick one!
1 parent 6201731 commit 7d48cc9

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

extras/post-commit.git-hook

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
# TODO: rename this to `post-commit` and place in project/.git/hooks/
3+
4+
# This is a git hook to trigger Jenkins builds automatically. It logins in if
5+
# your Jenkins instance requires password authentication, and then kicks the
6+
# special "Build Triggers" -> "Trigger builds remotely (e.g., from scripts)"
7+
# URL for you. You'll have to set an "Authentication Token" manually first.
8+
9+
# notify the centos jenkins ci that we should run a build
10+
JENKINS_BASE='https://ci.centos.org'
11+
JENKINS_USERNAME='purpleidea' # TODO: set this
12+
JENKINS_PASSWORD='hunter2' # TODO: set this
13+
JENKINS_JOB='purpleidea-oh-my-vagrant' # TODO: set this
14+
JENKINS_TOKEN='some-token-that-you-generate' # TODO: set this
15+
JENKINS_CAUSE='started+from+git+hook' # a url safe comment to pass as a build reason
16+
17+
branch=$(git rev-parse --abbrev-ref HEAD)
18+
if [ "$branch" = 'master' ] || [ "$1" = '--force' ]; then
19+
# login first
20+
wget -q -O - --save-cookies cookies.txt --post-data "j_username=${JENKINS_USERNAME}&j_password=${JENKINS_PASSWORD}&remember_me=on&Submit=log+in" "${JENKINS_BASE}/j_acegi_security_check" 1> /dev/null
21+
# trigger the build
22+
wget -q -O - --load-cookies cookies.txt "${JENKINS_BASE}/me/my-views/view/All/job/${JENKINS_JOB}/build?token=${JENKINS_TOKEN}&cause=${JENKINS_CAUSE}" 1> /dev/null
23+
# clean up the crumbs
24+
rm -rf cookies.txt
25+
fi

0 commit comments

Comments
 (0)