Created
June 20, 2012 19:47
-
-
Save atmoz/2961801 to your computer and use it in GitHub Desktop.
Git hook script for deploying code with push
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to use git reset instead, making sure rm and mv actions also gets applied.