Skip to content

Instantly share code, notes, and snippets.

@atmoz
Created June 20, 2012 19:47
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 atmoz/2961801 to your computer and use it in GitHub Desktop.
Save atmoz/2961801 to your computer and use it in GitHub Desktop.
Git hook script for deploying code with push
#!/bin/bash
#
# Deploy on push. Use different strategies on different branches.
#
function deploy_branch() {
# set branch path
case "$1" in
"master")
export GIT_WORK_TREE=/home/web/example.com ;;
"develop")
export GIT_WORK_TREE=/home/web/test.example.com ;;
*)
return 0 ;;
esac
# create folder if necessary
if [ ! -d "$GIT_WORK_TREE" ]; then
mkdir "$GIT_WORK_TREE"
fi
git checkout -f -q $1 # switch branch
git reset --hard -q # update to HEAD
echo "NOTICE: $1 branch is deployed to $GIT_WORK_TREE"
}
function refname2branch() {
# get branch from refname
echo `echo $1 | cut -d '/' -f 3`;
}
# Get input from terminal or stdin
if [ -n "$1" ]; then
deploy_branch $(refname2branch $1)
else
while read oldrev newrev refname
do
deploy_branch $(refname2branch $refname)
done
fi
@atmoz
Copy link
Author

atmoz commented Jun 21, 2012

Updated to use git reset instead, making sure rm and mv actions also gets applied.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment