I'm using gitolite with a post-receive hook to deploy a website.
Currently the script is very simple, but we want to add more complex stuff, like testing, deployment, compilation steps, etc.
In order to do this, I'd like to split up the post-receive hook into multiple separate files. i.e. post-receive.deploy, post-receive.compile, post-receive.test, etc. And one central post-receive hook to call them in sequence. This way we could more easily manage the steps.
Question is; how do I call the separate (sub-)hooks from the central post-receive hook? I can't even get them to execute, and as I understand, I need to pass along STDIN to the sub-hooks. I'm pretty much a n00b as far as shell scripting is concerned.
You can have your post-receive
hook be a wrapper script like the following:
#!/bin/sh
log() { printf %s\\n "$*"; }
warn() { log "WARNING: $@" >&2; }
mydir=${0%/*}
add_hook() {
case $1 in
/*) h=$1;;
*) h=${mydir}/$1;;
esac
[ -x "${h}" ] || {
warn "ignoring hook '$1' (not executable)"
continue
}
# XXX ${h} must not contain anything in ${IFS}
hooks="${hooks} ${h}"
}
run_hooks() {
# read input from Git, preserving trailing newlines if any
input=$(cat && echo eoi)
input=${input%eoi}
for h in ${hooks}; do
# no need to break the loop if a hook returns error -- the
# exit codes are ignored
printf %s "${input}" | "${h}" "$@"
done
}
add_hook git_multimail.py
add_hook some-other-post-receive-hook
add_hook yet-another-post-receive-hook
run_hooks "$@"
With this wrapper you declare whatever hooks you want to run via the add_hook
function. When the run_hooks
function is called, the wrapper reads the input from Git and saves it in a variable so that it can replay the input for each declared hook.