-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.sh
executable file
·354 lines (284 loc) · 6.22 KB
/
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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env bash
NAME="Deploy"
DESCRIPTION="Deploys by running a git deploy and an app build."
VERSION="0.2.4"
set -o errexit
set -o pipefail
# ----------
# Initialization
# ----------
# Script Defaults
GIT_BRANCH=${DEPLOY_GIT_BRANCH:-master}
GIT_TAG=${DEPLOY_GIT_TAG}
GIT_WORK_TREE=${DEPLOY_GIT_WORK_TREE:-}
GIT_DIR=${DEPLOY_GIT_DIR:-}
DEPLOY_ALLOWED_BRANCHES=("master" "develop")
# Script Variables
CURRENT_DIR=`pwd`
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
me=$(basename "$0")
GIT_ARGS=()
# ----------
# Functions
# ----------
function parseArgs()
{
while [[ $# -gt 0 ]]; do
opt="$1"
shift
case "$opt" in
-h|\?|--help)
showHelp
exit 0
;;
-b|--git-branch)
GIT_BRANCH="$1"
shift
;;
-t|--git-tag)
GIT_TAG="$1"
shift
;;
-w|--git-work-tree)
GIT_WORK_TREE="$1"
shift
;;
-d|--git-dir)
GIT_DIR="$1"
shift
;;
--bare)
BARE=true
;;
--dry-run)
DRY_RUN=true
;;
*)
errorExit 1 "illegal option $opt"
showHelp
;;
esac
done
}
function showHelp()
{
cat << EOF
options:
-h, --help Show this help.
-b, --git-branch
-t, --git-tag
-w, --git-work-tree
-d, --git-dir
--bare
--dry-run
EOF
}
function defineVariables()
{
# Add GIT_WORK_TREE to GIT_ARGS
if [ -z "${GIT_WORK_TREE}" ]; then
GIT_WORK_TREE=${CURRENT_DIR}
fi
GIT_ARGS+=("--work-tree=${GIT_WORK_TREE}")
# Add GIT_DIR to GIT_ARGS
if [ -z "${GIT_DIR}" ]; then
GIT_DIR=${CURRENT_DIR}
fi
GIT_ARGS+=("--git-dir=${GIT_DIR}")
}
function showOptions()
{
# Tell user the options they've selected
echo
echo "---------------------"
echo "$me: Options"
echo "---------------------"
# Echo if using GIT_TAG
if ! [ -z "${GIT_TAG}" ]; then
echo "Tag: ${GIT_TAG}"
fi
# Echo Git Branch
echo "Branch: ${GIT_BRANCH}"
# Echo Git Work Tree
echo "Git Work Tree: ${GIT_WORK_TREE}"
# Echo Git Directory
echo "Git Directory: ${GIT_DIR}"
# Dry Run
if [ -n "${DRY_RUN}" ]; then
echo "Dry Run: true"
fi
}
function processManagementStart()
{
echo
echo "---------------------"
echo "$me: Process Management"
echo "---------------------"
# @see - http://unix.stackexchange.com/questions/174028/killing-a-shell-script-running-in-background
# Create process daemon folder
mkdir -p /var/run/deploy
if [ -f /var/run/deploy/deploy.pid ]; then
echo "Process already running."
processID=$(cat /var/run/deploy/deploy.pid)
if ! [ -z "${processID}" ]; then
kill -9 ${processID}
fi
rm -f /var/run/deploy/deploy.pid
fi
echo `pidof $$` > /var/run/deploy/deploy.pid
}
function processManagementEnd()
{
rm -f /var/run/deploy/deploy.pid
}
function gitDeploy()
{
echo
echo "---------------------"
echo "$me: Git Deploy"
echo "---------------------"
# Make sure directory exists. Maybe its deployed for the first time.
mkdir -p "${GIT_WORK_TREE}"
# Put us in the right directory
cd "${GIT_WORK_TREE}"
if [ $(contains "${DEPLOY_ALLOWED_BRANCHES[@]}" "${GIT_BRANCH}") != "y" ]; then
echo
echo "Error: Branch ${GIT_BRANCH} is not allowed to deploy"
finished
exit 1
fi
# Pre Deploy
if ! [ -z "${PRE_DEPLOY}" ]; then
gitDeployPre
fi
# Determine type of deploy
if ! [ -z "${BARE}" ]; then
gitDeployBare
else
gitDeployFull
fi
# Post Deploy
if ! [ -z "${POST_DEPLOY}" ]; then
gitDeployPost
fi
}
function gitDeployPre()
{
echo
echo "Git Deploy: Pre Commands"
echo "CMD: '${PRE_DEPLOY}'"
if [ ! -n "${DRY_RUN}" ]; then
eval $PRE_DEPLOY || exit 1
fi
}
function gitDeployPost()
{
echo
echo "Git Deploy: Post Commands"
echo "CMD: '${POST_DEPLOY}'"
if [ ! -n "${DRY_RUN}" ]; then
eval $POST_DEPLOY || exit 1
fi
}
function gitDeployBare()
{
echo
echo "Git Deploy: Checkout"
echo "CMD: 'git ${GIT_ARGS[@]} checkout -f ${GIT_BRANCH}'"
if [ ! -n "${DRY_RUN}" ]; then
git ${GIT_ARGS[@]} checkout -f ${GIT_BRANCH}
fi
}
function gitDeployFull()
{
# GIT_TAG is not empty
if ! [ -z "${GIT_TAG}" ]; then
# Git Fetch
echo
echo "Git Deploy: Fetch"
echo "CMD: 'git ${GIT_ARGS[@]} fetch --depth 1 --tags origin ${GIT_BRANCH}'"
if [ ! -n "${DRY_RUN}" ]; then
git ${GIT_ARGS[@]} fetch --depth 1 --tags origin ${GIT_BRANCH}
fi
# Git Checkout
echo
echo "Git Deploy: Checkout"
echo "CMD: 'git ${GIT_ARGS[@]} checkout --force \"tags/${GIT_TAG}\"'"
if [ ! -n "${DRY_RUN}" ]; then
git ${GIT_ARGS[@]} checkout --force "tags/${GIT_TAG}"
fi
else
# Git Fetch
echo
echo "Git Deploy: Fetch"
echo "CMD: 'git ${GIT_ARGS[@]} fetch --depth 1 origin ${GIT_BRANCH}'"
if [ ! -n "${DRY_RUN}" ]; then
git ${GIT_ARGS[@]} fetch --depth 1 origin ${GIT_BRANCH}
fi
# Git Checkout
echo
echo "Git Deploy: Checkout"
echo "CMD: 'git ${GIT_ARGS[@]} checkout --force \"origin/${GIT_BRANCH}\"'"
if [ ! -n "${DRY_RUN}" ]; then
git ${GIT_ARGS[@]} checkout --force "origin/${GIT_BRANCH}"
fi
fi
}
function setup()
{
if [[ -n "$CURRENT_DIR" && -e "$CURRENT_DIR/.deploy" ]]; then
. "$CURRENT_DIR/.deploy"
fi
if [[ -n "$CURRENT_DIR" && -e "$CURRENT_DIR/.env" ]]; then
. "$CURRENT_DIR/.env"
fi
}
function start()
{
echo
echo "--------------------------------------------------"
echo "$me: ${NAME} v${VERSION} - ${DESCRIPTION}"
echo "$me: STARTED `date`"
echo "--------------------------------------------------"
}
function finished()
{
cd "${CURRENT_DIR}"
echo
echo "--------------------------------------------------"
echo "$me: FINISHED `date`"
echo "--------------------------------------------------"
echo
exit
}
function errorExit()
{
exitCode=$1
shift
echo "$me: $@" > /dev/null >&2
exit $exitCode
}
function contains() {
local n=$#
local value=${!n}
for ((i=1;i < $#;i++)) {
if [ "${!i}" == "${value}" ]; then
echo "y"
return 0
fi
}
echo "n"
return 1
}
# ----------
# Main
# ----------
parseArgs "$@"
start
setup
defineVariables
showOptions
processManagementStart
gitDeploy
processManagementEnd
finished