Gitolite post update hooks for Jenkins
Originally published 18th February, 2014
Getting hooks to work in gitolite can be a pain in the ass.
Recently (Feb 2014) I did this with the new version of gitolite, to hit up our Jenkins CI server, and thought I’d document here.
This page is reasonably accurate
http://therub.org/2012/05/24/adding-custom-hooks-to-gitolite-v3/
but I found missing some things.
This is the process that worked for me, with version from Sitaram’s github repo here:
https://github.com/sitaramc/gitolite
version v3.5.3.1–11-g8f1fd84
Sorry if these steps are not verbatim; I’m typing them from memory post-success more for my own reference in future, but hopefully they may help others.
- Install & setup gitolite as normal http://gitolite.com/gitolite/install.html
Example - Add git system account. All following steps done as this git account.
- git clone https://github.com/sitaramc/gitolite.git
- mkdir ~/bin
- ~/gitolite/install
This will install local copy of gitolite code, in the git user’s home dir - In ~/.gitolite.rc
- LOCAL_CODE => “$ENV{HOME}/.gitolite/local-code
This will allow us to put hooks in the gitolite/admin repo, and after pushing have them available at e.g. /home/git/.gitolite/local-code/hooks - In ENABLE, uncomment ‘repo-specific-hooks’
Self-explanatory - In “%RC” section — GIT_CONFIG_KEYS -> ‘.*’,
Allows us to put option directive in gitolite.conf, telling gitolite to use our hooks post-receive - Clone gitolite admin repo
e.g. git clone ssh://git@[git server]/gitolite-admin
In this repo - In gitolite.conf, set up your repo with a line like ‘option hook.post-receive = jenkins-notify-repo-update’
This tells gitolite to run a ‘repo-specific’ hook, after a push, from file called ‘jenkins-notify-repo-update’
e.g.
- repo hydro RW+ = @admins RW+ = @devs R = @devs_readonly option hook.post-receive = jenkins-notify-repo-update
- mkdir -p local-code/hooks/repo-specific
So your gitolite admin repo should now look something like this:
gitolite-admin
— conf/gitolite.conf
— keydir
— local-code/hooks/repo-specific - Create script to notify Jenkins
e.g. for me
vim local-code/hooks/repo-specific/jenkins-notify-repo-update
with contents
- #!/bin/bash /usr/bin/curl “http://[JENKINS SERVER]:8080/git/notifyCommit?url=ssh://git@[GIT SERVER]/${GL_REPO}”
- The $GL_REPO variable is set when script is run by gitolite and corresponds to the repo name
- Make the script executable
This perm will be preserved when pushed to git server, and must be the case in order for gitolite to execute it when needed.
chmod u+x local-code/hooks/repo-specific/jenkins-notify-repo-update - git add . && git commit -m ‘reticulated splines’ && git push
If all goes well, gitolite will receive this and all will be well.
I noticed if you mess things up (e.g. permissions in previous step), it does give decent warnings.
That’s pretty much it.
Now when you push a change to the repo for which you set the hook in gitolite.conf, that hook will get called.
Note — that TheRub page I linked above, the section saying
At this point, once gitolite.conf is pushed, the option should be verified by viewing ~git/repositories/official/myrepo.git/config. It should have a line that looks like this:
[hooks]
cqintegration = true
Wasn’t true for me; my repo’s config had no [hooks] section, yet the hooks still got called by gitolite, presumably because it’s still the first post-update (to do all its gitolite magick), which apparently now includes running the hooks, even though they’re not explicitly in the repo config’s [hooks] section.