Skip to content

Instantly share code, notes, and snippets.

@lakmeer
Created January 30, 2015 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lakmeer/95e87742226c34bf2533 to your computer and use it in GitHub Desktop.
Save lakmeer/95e87742226c34bf2533 to your computer and use it in GitHub Desktop.
Deploy git repo contents when pushed based on pushed branch
#!/bin/sh
# POST-RECEIVE HOOK (copy to repo.git/hooks/post-receive)
#
# Deploy content of this repo to different working dirs
# based on the branch being pushed. Pushing to 'staging'
# checks out your contents to the path at $staging_path,
# and similar for the branch 'live' to $production_path.
#
# When copying this hook, modify the following variables
# but leave the remaining bit untouched. You can comment
# out any of the path variables that you might not need.
production_path=/path/to/live/webroot
staging_path=/path/to/staging/webroot
# -------------- Do not modify from this point ---------------
# Read stdin three times to populate these variables.
# We mainly care about the third one: the reference name
# of the commit that was just received.
while read oldrev newrev refname; do
# Extract the branchname by querying git about this refname
branch=$(git rev-parse --symbolic --abbrev-ref $refname)
# On master branch, do nothing
if [ "$branch" == "master" ]; then
echo "Master branch received. No deployment was triggered"
fi
# On 'staging' branch, check out to staging folder
if [ "$branch" == "staging" ]; then
echo "STAGING received."
if [ -z "$staging_path" ]; then
echo "No staging path set. Nothing deployed."
else
echo "Checking out files to $staging_path"
GIT_WORK_TREE=$staging_path git checkout staging -f
fi
fi
# On 'live' branch, check out to production folder
if [ "$branch" == "live" ]; then
echo "LIVE received."
if [ -z "$production_path" ]; then
echo "No live path set, nothing deployed"
else
echo "Checking out files to $production_path"
GIT_WORK_TREE=$production_path git checkout live -f
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment