Executing multiple git post-receive hooks on git/gitolite server

In a past post of mine called Integrate git/gitolite server with slack I have mentioned how you could add a hook to your git/gitolite repository that would notify a slack channel about pushes made on your git server. But what about when you need to have multiple post-receive git hooks?

For example in one of the projects I have been working I wanted when a developer pushes something to develop branch of a specific repository a jenkins job should be triggered but at the same time a specific slack channel should be notified. I should either modify the git-slack-hook provided by Chris Eldredge or have a script named post-receive which would be used as a wrapper script that would call other hooks.

What I thought is that I should put each post-receive hook that I wanted to be executed named as post-receive-XX and then have a simple for loop that would call all those post-receive hooks. In my case I had a hook called post-receive-jenkins and one called post-receive-slack.

The post-receive script should look like this and put in the hooks directory of the project’s repository

#!/bin/sh
for i in `find . -name "post-receive-*"  -type f -executable` 
 do 
 $i
done

As you may notice the script only searches for files under hooks directory which are named like post-receive-* and are executables and executes them with name order. So it will execute first the post-receive-jenkins and then the post-receive-slack hook.
This can work with multiple git hooks.

Links
http://stackoverflow.com/questions/3448333/multiple-commands-are-not-working-in-git-post-receive
http://www.lukefitzgerald.co.uk/2011/01/multiple-post-receive-hooks-for-git/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s