forked from sunny/git-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-deploy.sh
executable file
·85 lines (64 loc) · 2.34 KB
/
git-deploy.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/bash
# Git push then pull over ssh
# Script based on https://github.com/sunny/git-deploy
set -e
info(){
echo -e "* \033[1m""$@""\033[0m"
}
# set variables from parameters
REMOTE_LOCAL_NAME=${1:-origin} # 1st argument or default to remote origin
ENV=${2:-staging} # 2nd argument or default to staging
REMOTE_SERVER_NAME="origin" # name of the git remote on deployed server
# If you need to run local scripts before deployment (knock, for example)
if [ -f __scripts/deploy_before ]; then
info "__scripts/deploy_before $ENV"
__scripts/deploy_before "$ENV"
fi
BRANCH=`git branch 2> /dev/null | sed -n '/^\*/s/^\* //p'` # local branch to send to REMOTE
REMOTE_URL=`git config --get remote.$REMOTE_LOCAL_NAME.url` # remote url
HOST=${REMOTE_URL%%:*} # HOST=user@remote_server
GIT_DIR=${REMOTE_URL#*:} # bare repository location on REMOTE
ENV_DIR="${GIT_DIR%/*}"/$ENV # project env dir on REMOTE
info "Sending branch $BRANCH"
git push $REMOTE_LOCAL_NAME HEAD
# ssh to HOST and execute all commands between ""
ssh -T $HOST "
# Force your script to exit on error from where the 1st error occurred
set -e
cd $ENV_DIR
git fetch $REMOTE_SERVER_NAME
CURRENT_BRANCH=\`git branch 2> /dev/null | sed -n '/^\*/s/^\* //p'\`
(git branch 2> /dev/null | grep -n '^ $BRANCH') && HAS_BRANCH=1 || HAS_BRANCH=0
# Change branch
if [ $BRANCH != \$CURRENT_BRANCH ]; then
# Create remote tracked branch
if [ \$HAS_BRANCH != 1 ]; then
echo '* Creating $BRANCH branch in $ENV'
git checkout -t $REMOTE_SERVER_NAME/$BRANCH
# Switch to it
else
echo '* Switching to $BRANCH branch in $ENV'
git checkout $BRANCH --
fi
# Launch post-merge if file .git/hooks/post-merge is executable
[ -x .git/hooks/post-merge ] && .git/hooks/post-merge
# Pull
else
echo '* Pulling $BRANCH in $ENV'
git merge $REMOTE_SERVER_NAME/$BRANCH --ff-only
fi
"
# For compile scripts (Compass, CoffeeScript, etc…)
if [ -f __scripts/compile ]; then
info "__scripts/compile $ENV"
ssh -T $HOST "cd $ENV_DIR && __scripts/compile $ENV"
fi
# For local extra deploy scripts (database, for example)
if [ -f __scripts/deploy ]; then
info "__scripts/deploy $ENV"
__scripts/deploy "$ENV"
fi
info "Tag $ENV"
git tag -f $ENV
git push -f $REMOTE_LOCAL_NAME HEAD
echo "Oh yeah! 👍"