Skip to content

Instantly share code, notes, and snippets.

@andruby
Created January 11, 2014 11:00
Show Gist options
  • Save andruby/8369510 to your computer and use it in GitHub Desktop.
Save andruby/8369510 to your computer and use it in GitHub Desktop.
post receive git hook that runs post_receive.rb if it exists in the repository
#!/bin/bash
# Shell script that should be installed on the origin server as post-receive hook
export GIT_SOURCE=$PWD
# Extract args and set environment variables for each ref pushed
while read oldrev newrev ref
do
export BEFORE=$oldrev
export AFTER=$newrev
export REF=$ref
export BRANCH=`echo $ref | cut -d/ -f3`
echo "create tmp directory"
export TMP_DIR=$(mktemp -d)
echo "make a clone of the pushed branch"
git clone --branch $BRANCH $GIT_SOURCE $TMP_DIR
if [ -f $TMP_DIR/post_receive.rb ]; then
echo "run post_receveive.rb"
( cd $TMP_DIR && ruby post_receive.rb )
else
echo "no post_receive.rb in this branch"
fi
echo "remove tmp directory"
rm -rf $TMPDIR
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment