Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Last active May 28, 2019 18:34
Show Gist options
  • Save miguelmota/9595095 to your computer and use it in GitHub Desktop.
Save miguelmota/9595095 to your computer and use it in GitHub Desktop.
Git post-receive hook example. Blog post: http://www.miguelmota.com/blog/deployment-with-git/
#!/bin/bash
SITE_NAME="Foo"
SITE_PATH="/var/www/foo.com"
BRANCH="production"
echo "**** $SITE_NAME [post-receive] hook received."
while read oldrev newrev ref
do
branch_received=`echo $ref | cut -d/ -f3`
echo "**** Received [$branch_received] branch."
# Making sure we received the branch we want.
if [ $branch_received = $BRANCH ]; then
cd $SITE_PATH
# Unset to use current working directory.
unset GIT_DIR
echo "**** Pulling changes."
git pull origin $BRANCH
# Instead of pulling we can also do a checkout.
: '
echo "**** Checking out branch."
GIT_WORK_TREE=$SITE_PATH git checkout -f $BRANCH
'
# Or we can also do a fetch and reset.
: '
echo "**** Fetching and reseting."
git fetch --all
git reset --hard origin/$BRANCH
'
else
echo "**** Invalid branch, aborting."
exit 0
fi
done
# [Restart/reload webserver stuff here]
echo "**** Done."
exec git-update-server-info
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment