-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy
148 lines (131 loc) · 4.53 KB
/
deploy
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/sh
# Author: msAshlee 2017
# V 0.1.1
#
# post-receive hook for deploying a git branch to a web root
#
# git/gitolite user must be able to write to PR_www_dir
# it is assumed user will have access to repo's since this script
# should only be being run by the git/gitolite user
#
#conf
#folder locations, be sure to leave off the trailing spaces.
# Currently there is no support here for spaces in directory names or repo
# names, and at this point is not a likely addition.
#repository directory
PR_repo_dir=/home/git/repositories
#Apache directory that will be serving the repos' content
PR_www_dir=/var/www/html
#Admin message sent to remote upon failures
PR_ADMIN="Please contact your admin"
#uncomment to below to enable
# enabled behavior | disabled behavior
#Show full web root in response messages | Show only web path relative to web root
PR_show_www=
#Deploy master or dev branches | Only deploy master
PR_dev_site=
###############################
while read PR_oldrev PR_newrev PR_ref
do
echo "post-receive deploy v 0.1.1 hook hit: $(date +%Y%m%d)"
echo "old rev: $PR_oldrev"
echo "new rev: $PR_newrev"
echo "ref: $PR_ref"
if [ -d $PR_repo_dir ] && [ -d $PR_www_dir ]; then
#repo/www paths are good or symlink
if [ ! -L $PR_repo_dir ] && [ ! -L $PR_www_dir ]; then
#Not symbolic link
#First check if we are on master (or dev) branches, if not exit.
PR_branch=${PR_ref#"refs/heads/"}
echo "Branch $PR_branch..."
if [ $PR_branch = "master" ]; then
PR_MSG="Master Branch"
elif [ $PR_branch = "dev" ]; then
if [ ! -z ${PR_dev_site+x} ]; then
#dev branch is on
PR_MSG="Dev branch"
else
PR_ERR="Dev branch, however dev sites are not enabled."
fi
else
PR_ERR="Branch not supported for deployment"
unset PR_ADMIN
fi
if [ ! -z ${PR_MSG+x} ]; then
echo "Starting deployment of $PR_MSG"
#next get current (repo's) directory
if [ -z ${PWD+x} ]; then
#var unset
PR_DIR=$(pwd $GIT_DIR)
PR_MSG="\$PWD not set, \$PR_DIR set from \$GIT_DIR as:"
else
#var is set
PR_DIR=$PWD
PR_MSG="\$PWD var is set, \$PR_DIR set from \$PWD as:"
fi
echo $PR_MSG $PR_DIR
#remove repo directory and .git from current path to get the www location
PR_site_dir=${PR_DIR#$PR_repo_dir}
PR_site_dir=${PR_site_dir%".git"}
PR_site_dir="$PR_site_dir/$PR_branch"
echo "Site directory set to $PR_site_dir"
#check if the site dir exists, if not make it
if [ ! -z ${PR_show_www+x} ]; then PR_MSG=$PR_www_dir$PR_site_dir
else PR_MSG=$PR_site_dir
fi
if [ -d $PR_www_dir$PR_site_dir ]; then
#site exists, this is just a clone
echo "Folder $PR_MSG exists."
else
echo "Folder $PR_MSG doesn't exist..."
echo "Attempting to create it now...."
if mkdir -p $PR_www_dir$PR_site_dir ; then
echo "Success"
else
echo "Failed"
PR_ERR="Couldn't create site directory: $PR_MSG"
fi
fi
#folder should exist now, lets clone & thats all we need to do
if [ -z ${PR_ERR+x} ]; then
echo "Starting deployment...$PR_ERR"
GIT_WORK_TREE=$PR_www_dir$PR_site_dir git checkout -f
if [ $? -eq 0 ]; then
echo "git exited 0"
echo "Deployment successful"
else
PR_ERR="Failure: Git exited $?"
fi
else
echo "Error found..."
fi
fi
#end of good things.
else
#symbolic link
if [ -L $PR_repo_dir ]; then
$PR_ERR="Repository"
else
$PR_ERR="Web"
fi
PR_ERR=$PR_ERR" directory is a symlink and not yet supported"
fi
else
#bad dir for www or repos, exit and just take push (nothing more to do)
if [ -d $PR_repo_dir ]; then
$PR_ERR="Repository"
else
$PR_ERR="Web"
fi
PR_ERR=$PR_ERR" directory doesn't exist"
fi
#Error message if PR_ERR is set
if [ ! -z ${PR_ERR+x} ]; then
echo "Deployment has been halted..."
echo $PR_ERR
[ ! -z ${PR_ADMIN+x} ] && echo $PR_ADMIN
exit 1
fi
done
unset PR_ERR PR_MSG PR_ADMIN PR_www_dir PR_site_dir PR_repo_dir PR_show_www PR_dev_site PR_oldrev PR_newrev PR_ref PR_branch PR_DIR
exit 0