Gitolite post update hooks for Jenkins

Nick Doyle
3 min readJul 24, 2018

--

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.

  1. Install & setup gitolite as normal http://gitolite.com/gitolite/install.html
    Example
  2. Add git system account. All following steps done as this git account.
  3. git clone https://github.com/sitaramc/gitolite.git
  4. mkdir ~/bin
  5. ~/gitolite/install
    This will install local copy of gitolite code, in the git user’s home dir
  6. In ~/.gitolite.rc
  7. 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
  8. In ENABLE, uncomment ‘repo-specific-hooks’
    Self-explanatory
  9. In “%RC” section — GIT_CONFIG_KEYS -> ‘.*’,
    Allows us to put option directive in gitolite.conf, telling gitolite to use our hooks post-receive
  10. Clone gitolite admin repo
    e.g. git clone ssh://git@[git server]/gitolite-admin
    In this repo
  11. 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
  1. 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
  2. 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}”
  1. The $GL_REPO variable is set when script is run by gitolite and corresponds to the repo name
  2. 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
  3. 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.

--

--

Nick Doyle
Nick Doyle

Written by Nick Doyle

Cloud-Security-Agile, in Melbourne Australia, experience includes writing profanity-laced Perl, surprise Migrations, furious DB Admin and Motorcycle instructing

Responses (1)