-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitdeploy.sh
More file actions
134 lines (119 loc) · 3.15 KB
/
gitdeploy.sh
File metadata and controls
134 lines (119 loc) · 3.15 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
CURRENTDIR="$( cd "$(dirname "$0")" ; pwd -P )"
DEPLOYGIT=""
DEPLOYDIR=""
HOST=$(curl -4sL icanhazip.net)
USER=$(whoami)
DEPLOYBRANCH="master"
function usage {
echo -e "Usage: $0 --git <example.git> --deploy <deploy_here> {--user | --host | --parent-directory | --branch}\n"
echo " Required: "
echo " -g, --git Bare Git Repository"
echo " -d, --deploy Deploy Location/Directory"
echo " Optional: "
echo " -u, --user SSH User (Default is current user)"
echo " -a, --host Host IP/Domain (Default is public IP)"
echo " -p, --parent-directory Specify Parent Directory (Default is currnet directory)"
echo " -b, --branch Specify Deploy Branch (Default is master)"
echo " -h, --help Displays Help Information"
}
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-g|--git)
DEPLOYGIT="$2"
shift
shift
;;
-d|--deploy)
DEPLOYDIR="$2"
shift
shift
;;
-u|--user)
USER="$2"
shift
shift
;;
-h|--host)
HOST="$2"
shift
shift
;;
-p|--parent-directory)
CURRENTDIR="$2"
shift
shift
;;
-b|--branch)
DEPLOYBRANCH="$2"
shift
shift
;;
--help)
usage
exit
shift
;;
*)
usage
exit
shift
;;
esac
done
if [ -z "$DEPLOYGIT" ] || [ -z "$DEPLOYDIR" ]
then
usage
exit
fi
echo "================================================="
echo "Git Repository : $DEPLOYGIT"
echo "Deploy Location : $DEPLOYDIR"
echo "Parent Directory : $CURRENTDIR"
echo "User : $USER"
echo "Host : $HOST"
echo "Deploy Branch : $DEPLOYBRANCH"
echo "================================================="
(cd $DIR
if [ -d "$CURRENTDIR/$DEPLOYGIT" ]
then
rm -rf $CURRENTDIR/$DEPLOYGIT
fi
if [ ! -d "$CURRENTDIR/$DEPLOYDIR" ]
then
mkdir $CURRENTDIR/$DEPLOYDIR
fi
echo -e "\n\nInitializing Git Repo"
echo "================================================="
git init --bare $CURRENTDIR/$DEPLOYGIT
echo -e "\n\nWriting post-receive hook"
echo "================================================="
tee -a $CURRENTDIR/$DEPLOYGIT/hooks/post-receive << EOF
#!/bin/bash
TARGET="$CURRENTDIR/$DEPLOYDIR"
GIT_DIR="$CURRENTDIR/$DEPLOYGIT"
BRANCH="$DEPLOYBRANCH"
while read oldrev newrev ref
do
if [[ \$ref = refs/heads/\$BRANCH ]];
then
echo "Ref \$ref received. Deploying \${BRANCH} branch to production..."
if [ -d "\$TARGET" ]
then
rm -rf "\$TARGET"
fi
mkdir -p "\$TARGET"
git --work-tree="\$TARGET" --git-dir="\$GIT_DIR" checkout -f
else
echo "Ref \$ref received. Doing nothing: only the \${BRANCH} branch may be deployed on this server."
fi
done
EOF
chmod +x $CURRENTDIR/$DEPLOYGIT/hooks/post-receive
)
echo -e "\n\n"
echo "================================================="
echo "git remote add production $USER@$HOST:$CURRENTDIR/$DEPLOYGIT"
echo "git push production master"
echo "================================================="