-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwp-git-sync.sh
419 lines (370 loc) · 10.1 KB
/
wp-git-sync.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/bin/bash
# WordPress SSH Git CI Script
# v1.2.1
# Set argument parameters on start
BRANCH=live
DEVBRANCH=dev
STAGEBRANCH=stage
PROJDIR=wordpress
ERRORMESSAGES=""
TRACKEDONLY="false"
COMMIT="true"
SOFTERROR="false"
FETCH="false"
while :; do
case "$1" in
-b|--branch) #optional
if [[ $2 ]]; then
BRANCH=$2
shift
else
ERRORMESSAGES="${ERRORMESSAGES} ERROR: '-b | --branch' requires a non-empty option argument."
fi
;;
-c|--commit) #optional -- default value is true
if [[ $2 == "no" || $2 == "n" ]] ; then
COMMIT="false"
shift
fi
;;
-d|--devbranch) #optional
if [[ $2 ]]; then
DEVBRANCH=$2
shift
else
ERRORMESSAGES="ERROR: '-d | --devbranch' requires a non-empty option argument."
fi
;;
-fe|--fetch) #optional
FETCH="true"
shift
;;
-fo|--fullorigin) #optional
if [[ $2 ]]; then
ORIGIN=$2
shift
else
ERRORMESSAGES="ERROR: '-f | --fullorigin' requires a non-empty option argument."
fi
;;
-g|--gitrepo) #semi-optional -- if empty ORIGIN string is required
if [[ $2 ]]; then
GITREPO=$2
shift
else
ERRORMESSAGES="ERROR: '-g | --gitrepo' requires a non-empty option argument."
fi
;;
-m|--message) #optional
if [[ $2 ]]; then
MESSAGE=$2
shift
else
ERRORMESSAGES="ERROR: '-m | --message' requires a non-empty option argument."
fi
;;
-o|--onchange) #optional
if [[ $2 ]]; then
ONCHANGE=$2
shift
else
ERRORMESSAGES="ERROR: '-o | --onchange' requires a non-empty option argument."
fi
;;
-pd|--projectdir) #optional
if [[ $2 ]]; then
PROJDIR=$2
shift
else
ERRORMESSAGES="ERROR: '-p | --projectdir' requires a non-empty option argument."
fi
;;
-sb|--stagebranch) #optional
if [[ $2 ]]; then
STAGEBRANCH=$2
shift
else
ERRORMESSAGES="ERROR: '-s | --stagebranch' requires a non-empty option argument."
fi
;;
-se|--softerror) #optional
SOFTERROR="true"
shift
;;
-to|--token) #semi-optional -- if empty ORIGIN string is required
if [[ $2 ]]; then
TOKEN=$2
shift
else
ERRORMESSAGES="ERROR: '-t | --token' requires a non-empty option argument."
fi
;;
-tr|--tracked) #optional
TRACKEDONLY="true"
shift
;;
-u|--tokenuser) #semi-optional -- if empty ORIGIN string is required
if [[ $2 ]]; then
TOKENUSER=$2
shift
else
ERRORMESSAGES="ERROR: '-u | --tokenuser' requires a non-empty option argument."
fi
;;
--)
shift
break
;;
*)
break
esac
shift
done
# Remote connection methods
add_devops_remote() {
git remote add devops "$ORIGIN"
git remote update devops 2>&1
log status "Remote devops addded"
}
rm_devops_remote() {
git remote rm devops
log status "Remote devops removed"
}
add_or_remove_devops() {
output=$(git remote )
case $output in
*devops*)
log warning "Remote DevOps found"
remoteAdded="true"
;;
*)
log warning "Remote DevOps not found"
remoteAdded="false"
;;
esac
case $1 in
add)
if [[ $remoteAdded == "false" ]] ; then
add_devops_remote
fi
;;
rm)
if [[ $remoteAdded == "true" ]] ; then
rm_devops_remote
fi
;;
esac
}
# Handling changes on WordPress server
## Branches
checkout_branch() {
if [[ "$3" ]] ; then
git checkout -B "$DEVBRANCH" "devops/$DEVBRANCH" 2>&1
return
fi
if [[ -z "$2" ]] ; then
git checkout "$1" 2>&1
else
git checkout devops "$2" 2>&1
fi
}
rm_branch() {
git branch -D "$1" 2>&1
}
go_live() {
git checkout -B "$BRANCH" 2>&1
if [[ $1 ]] ; then
rm_branch "$1"
fi
}
## Stashes
is_stashes() {
output=$(git stash list )
case $1 in
check) stashes="false";;
esac
case $output in
*"stash"*)
stashes="true";;
esac
}
git_stash() {
is_stashes "check"
if [[ $stashes == "true" ]] ; then
case $1 in
clear)
git stash clear 2>&1;;
pop)
git stash pop 2>&1;;
u)
git stash -u 2>&1;;
*)
git stash 2>&1;;
esac
fi
return
}
## Committing changes
commit_git() {
if [[ $COMMIT == "true" ]] ; then
checkout_branch "devops" "$DEVBRANCH" "true"
git add -A . 2>&1
git commit -a -m "$MESSAGE"
log status "Commited on $DEVBRANCH with message:
$MESSAGE"
else
log status "$BRANCH changes were not commited"
fi
}
## Pull branch from remote
pull_branch() {
msg="Pull branch "
if [[ $2 ]] ; then
output=$(git pull "$1/$2")
msg=$msg$2
else
git pull devops "$1" 2>&1
msg=$msg$1
fi
log status "$msg is complete."
}
## Push branch to remote
push_branch() {
msg="Push branch "
if [[ -z "$2" ]] ; then
git push "$1" 2>&1
msg=$msg$1
else
git push devops "$2" 2>&1
msg=$msg$2
fi
log status "$msg is complete."
}
# Utilities
error3() {
add_or_remove_devops "rm"
exit 3
}
throw_errors() {
if [[ ${#ERRORMESSAGES} -gt 0 ]] ; then
for ERRORMSG in ${ERRORMESSAGES}; do
log error "${ERRORMSG}"
done
error3
fi
}
set_defaults() {
# If error messages from defaults, list them and exit out
throw_errors
# Set origin url to use
if [[ -z $ORIGIN ]] ; then
# if origin isn't set and the other arguments needed to make an origin aren't set, throw an error!
if [[ -z $TOKENUSER || -z $TOKEN || -z $GITREPO ]] ; then
error3
fi
ORIGIN="https://${TOKENUSER}:${TOKEN}@${GITREPO}"
fi
# Set the default commit message
if [[ -z $MESSAGE ]]; then
MESSAGE="Server Side Commit
This was commited from the WordPress server"
fi
# Set if branch has changes; and if so, how to handle those changes
if [[ $TRACKEDONLY == "true" ]] ; then
output=$(git status --untracked-files=no --porcelain)
else
output=$(git status --porcelain)
fi
if [[ -z $output ]] ; then
DIRTYBRANCH="false"
else
if [[ $ONCHANGE = "Stop" || $ONCHANGE = "stop" ]] ; then
error3
else
if [[ -n $ONCHANGE ]] && [[ $ONCHANGE != "commit" && $ONCHANGE != "Commit" ]]; then
log error '-o or --onchange has an incorrect value. Please use commit or stop for your selection. If you leave it blank, commit is the default.'
error3
else
ONCHANGE="commit"
DIRTYBRANCH="true"
fi
fi
fi
}
# Method models
dev_branch_push() {
output=$(git symbolic-ref --short HEAD )
case $output in
*"dev"*)
push_branch "devops" "$DEVBRANCH";;
esac
}
get_new_release() {
# Make sure it's not missing a push back
dev_branch_push
if [[ $FETCH == "true" ]]; then
return;
fi
log status "Getting latest release now"
# Check if branch is on dev branch
# If so, exit
# Checkout staging release branch
checkout_branch "$STAGEBRANCH"
# Pull new release changes
pull_branch "$STAGEBRANCH"
# Checkout a live branch, based on the staging release branch
go_live
}
clean_repository() {
# Stash changes
log status "Stashing changes"
git_stash "clear"
git_stash "u"
# Get latest release from Azure DevOps
get_new_release
# Add changes back
log status "Finished updating, adding changes back"
# Switching to dev branch to add changes
checkout_branch "devops" "$DEVBRANCH" "true"
pull_branch "$STAGEBRANCH"
# Pop changes back in
git_stash "pop"
# If soft error isn't enabled, add files and commit them
if [[ $SOFTERROR == "false" ]] ; then
commit_git
# Push changes back to dev branch
push_branch "devops" "$DEVBRANCH"
else
log error "Changes on server, soft error selected. Release is pulled, changes are popped back, branch is waiting user intervention."
error3
fi
log status "Changes on the WordPress site have been commited, and were pushed to $DEVBRANCH branch"
}
## Print messages in colored text
log() {
case "$1" in
# Greeen status message usually signifying success
status) tput setaf 2 ;;
# Yellow message usually signifying a warning message of some kind
warning) tput setaf 3 ;;
# Red indicates an error and action should be taken
error) tput setaf 1 ;;
# Called log with something other than status|warning|error
*) log error "Internal error: invalid log level"; exit 1 ;;
esac
echo "$2"
}
# Methods and properties set, now start script logic models
cd "$PROJDIR" || exit 1
set_defaults
add_or_remove_devops "add"
# Option selected to just query for changes
if [[ $DIRTYBRANCH == "false" ]] ; then
# Working directory clean
log status "Working directory clean"
get_new_release
else
log warning "Uncommitted changes! Starting to clean..."
clean_repository
fi
add_or_remove_devops "rm"