Skip to content

Instantly share code, notes, and snippets.

@mdrmike
Last active April 19, 2016 18:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdrmike/870aedb66e0c991e12a9 to your computer and use it in GitHub Desktop.
Save mdrmike/870aedb66e0c991e12a9 to your computer and use it in GitHub Desktop.
post-recieve hook to automatically deploy a git branch to either staging or production. (or do nothing)
#!/bin/bash
# git/hooks/post-receive
# add this script to a git repo on a 'remote' server
# to automatically deploy a git branch to either staging or production (or do nothing)
WORK_TREE_PROD=$HOME/production # assumes logged in user HOME/production path is webroot
WORK_TREE_STAGE=$HOME/staging # assumes logged in user HOME/staging path is webroot
GIT_DIR=$HOME/git # assumes remote git repo is in HOME/git (no dot - not hidden)
# The post-receive hook can receive multiple branches at once (for example if someone does a git push --all), so we also need to wrap the read in a while loop.
while read oldrev newrev ref
do
branch=`echo $ref | cut -d/ -f3`
case $branch in
"production")
echo "Production ref received. Deploying production branch to $WORK_TREE_PROD ..."
[ -d "$WORK_TREE_PROD" ] && git --work-tree="$WORK_TREE_PROD" checkout -f && echo "success"
;;
"master")
echo "Master ref received. Deploying staging branch to $WORK_TREE_STAGE ..."
[ -d "$WORK_TREE_STAGE" ] && git --work-tree="$WORK_TREE_STAGE" checkout -f && echo "success"
;;
"")
echo "Branch (=${branch}) is empty. hmm. that ain't right. time to debug"
;;
*)
echo "Ref $branch successfully received. Nothing to do since only the production branch may be deployed on this server."
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment