1
1
#! /bin/bash
2
2
3
3
# WordPress SSH Git CI Script
4
- # v1.0.2
4
+ # v1.2.0
5
5
6
6
# Set argument parameters on start
7
7
BRANCH=live
@@ -11,9 +11,10 @@ PROJDIR=wordpress
11
11
ERRORMESSAGES=" "
12
12
TRACKEDONLY=" false"
13
13
COMMIT=" true"
14
+ SOFTERROR=" false"
15
+ FETCH=" false"
14
16
while : ; do
15
17
case " $1 " in
16
-
17
18
-b|--branch) # optional
18
19
if [[ $2 ]]; then
19
20
BRANCH=$2
@@ -23,12 +24,9 @@ while :; do
23
24
fi
24
25
;;
25
26
-c|--commit) # optional -- default value is true
26
- if [[ $2 = " no" || $2 = " n" ]] ; then
27
+ if [[ $2 == " no" || $2 = = " n" ]] ; then
27
28
COMMIT=" false"
28
29
shift
29
- else
30
- COMMIT=" true"
31
- shift
32
30
fi
33
31
;;
34
32
-d|--devbranch) # optional
@@ -39,7 +37,11 @@ while :; do
39
37
ERRORMESSAGES=" ERROR: '-d | --devbranch' requires a non-empty option argument."
40
38
fi
41
39
;;
42
- -f|--fullorigin) # optional
40
+ -fe|--fetch) # optional
41
+ FETCH=" true"
42
+ shift
43
+ ;;
44
+ -fo|--fullorigin) # optional
43
45
if [[ $2 ]]; then
44
46
ORIGIN=$2
45
47
shift
@@ -71,15 +73,15 @@ while :; do
71
73
ERRORMESSAGES=" ERROR: '-o | --onchange' requires a non-empty option argument."
72
74
fi
73
75
;;
74
- -p |--projectdir) # optional
76
+ -pd |--projectdir) # optional
75
77
if [[ $2 ]]; then
76
78
PROJDIR=$2
77
79
shift
78
80
else
79
81
ERRORMESSAGES=" ERROR: '-p | --projectdir' requires a non-empty option argument."
80
82
fi
81
83
;;
82
- -s |--stagebranch) # optional
84
+ -sb |--stagebranch) # optional
83
85
if [[ $2 ]]; then
84
86
STAGEBRANCH=$2
85
87
shift
@@ -91,7 +93,7 @@ while :; do
91
93
SOFTERROR=" true"
92
94
shift
93
95
;;
94
- -t |--token) # semi-optional -- if empty ORIGIN string is required
96
+ -to |--token) # semi-optional -- if empty ORIGIN string is required
95
97
if [[ $2 ]]; then
96
98
TOKEN=$2
97
99
shift
@@ -124,35 +126,39 @@ done
124
126
# Remote connection methods
125
127
126
128
add_devops_remote () {
127
- git remote add devops " $ORIGIN " 2>&1
128
- git remote update 2>&1
129
- git remote -v 2>&1
129
+ git remote add devops " $ORIGIN "
130
+ git remote update devops 2>&1
130
131
print_status_msg " Remote devops addded"
131
132
}
132
133
133
134
rm_devops_remote () {
134
- git remote rm devops 2>&1
135
- git remote -v 2>&1
135
+ git remote rm devops
136
136
print_status_msg " Remote devops removed"
137
137
}
138
138
139
139
add_or_remove_devops () {
140
140
output=$( git remote )
141
141
case $output in
142
- * +devops* )
143
- remoteAdded=" true" ;;
142
+ * devops* )
143
+ print_warning_msg " Remote DevOps found"
144
+ remoteAdded=" true"
145
+ ;;
144
146
* )
145
- remoteAdded=" false" ;;
147
+ print_warning_msg " Remote DevOps not found"
148
+ remoteAdded=" false"
149
+ ;;
146
150
esac
147
151
case $1 in
148
152
add)
149
- if [ $remoteAdded == " false" ] ; then
153
+ if [[ $remoteAdded == " false" ] ] ; then
150
154
add_devops_remote
151
- fi ;;
155
+ fi
156
+ ;;
152
157
rm)
153
- if [ $remoteAdded == " true" ] ; then
158
+ if [[ $remoteAdded == " true" ] ] ; then
154
159
rm_devops_remote
155
- fi ;;
160
+ fi
161
+ ;;
156
162
esac
157
163
}
158
164
@@ -161,27 +167,37 @@ add_or_remove_devops() {
161
167
# # Branches
162
168
163
169
checkout_branch () {
164
- if [ -z " $2 " ] ; then
170
+ if [[ " $3 " ]] ; then
171
+ git checkout -B " $DEVBRANCH " " devops/$DEVBRANCH " 2>&1
172
+ return
173
+ fi
174
+ if [[ -z " $2 " ]] ; then
165
175
git checkout " $1 " 2>&1
166
176
else
167
177
git checkout devops " $2 " 2>&1
168
178
fi
169
179
}
170
180
171
181
rm_branch () {
172
- git branch -D " $1 "
182
+ git branch -D " $1 " 2>&1
173
183
}
174
184
185
+ go_live () {
186
+ git checkout -B " $BRANCH " 2>&1
187
+ if [[ $1 ]] ; then
188
+ rm_branch " $1 "
189
+ fi
190
+ }
175
191
# # Stashes
176
192
177
193
is_stashes () {
178
194
output=$( git stash list )
179
195
case $1 in
180
- check) stashes=0 ;;
196
+ check) stashes=" false " ;;
181
197
esac
182
198
case $output in
183
- * + " stash" * )
184
- stashes=1 ;;
199
+ * " stash" * )
200
+ stashes=" true " ;;
185
201
esac
186
202
}
187
203
@@ -205,10 +221,11 @@ git_stash() {
205
221
# # Committing changes
206
222
207
223
commit_git () {
208
- if [[ $COMMIT == " commit" ]] ; then
224
+ if [[ $COMMIT == " true" ]] ; then
225
+ checkout_branch " devops" " $DEVBRANCH " " true"
209
226
git add -A . 2>&1
210
- git commit -m " $MESSAGE " 2>&1
211
- print_status_msg " Commited on $BRANCH with message:
227
+ git commit -a - m " $MESSAGE "
228
+ print_status_msg " Commited on $DEVBRANCH with message:
212
229
$MESSAGE "
213
230
else
214
231
print_status_msg " $BRANCH changes were not commited"
@@ -217,27 +234,14 @@ commit_git() {
217
234
218
235
# # Pull branch from remote
219
236
220
- create_or_rm_branch () {
221
- case $1 in
222
- add)
223
- if [[ $remoteAdded == " true" ]] ; then
224
- add_devops_remote
225
- fi ;;
226
- rm)
227
- if [[ $remoteAdded == " true" ]] ; then
228
- rm_devops_remote
229
- fi ;;
230
- esac
231
- }
232
-
233
237
pull_branch () {
234
238
msg=" Pull branch "
235
- if [[ -z $2 ]] ; then
236
- git pull " $1 " 2>&1
237
- msg=$msg $1
238
- else
239
- git pull devops " $2 " 2>&1
239
+ if [[ $2 ]] ; then
240
+ output=$( git pull " $1 /$2 " )
240
241
msg=$msg $2
242
+ else
243
+ git pull devops " $1 " 2>&1
244
+ msg=$msg $1
241
245
fi
242
246
print_status_msg " $msg is complete."
243
247
}
@@ -247,7 +251,7 @@ pull_branch() {
247
251
248
252
push_branch () {
249
253
msg=" Push branch "
250
- if [ -z " $2 " ] ; then
254
+ if [[ -z " $2 " ] ] ; then
251
255
git push " $1 " 2>&1
252
256
msg=$msg $1
253
257
else
@@ -265,7 +269,7 @@ error3() {
265
269
}
266
270
267
271
throw_errors () {
268
- if [ ${# ERRORMESSAGES} -gt 0 ] ; then
272
+ if [[ ${# ERRORMESSAGES} -gt 0 ] ] ; then
269
273
for ERRORMSG in ${ERRORMESSAGES} ; do
270
274
print_error_msg " ${ERRORMSG} "
271
275
done
@@ -300,7 +304,7 @@ set_defaults() {
300
304
output=$( git status --porcelain)
301
305
fi
302
306
303
- if [ -z " $output " ] ; then
307
+ if [[ -z $output ] ] ; then
304
308
DIRTYBRANCH=" false"
305
309
else
306
310
if [[ $ONCHANGE = " Stop" || $ONCHANGE = " stop" ]] ; then
@@ -319,24 +323,32 @@ set_defaults() {
319
323
320
324
# Method models
321
325
326
+ dev_branch_push () {
327
+ output=$( git branch --show-current )
328
+ case $output in
329
+ * " dev" * )
330
+ push_branch " devops" " $DEVBRANCH " ;;
331
+ esac
332
+ }
333
+
322
334
get_new_release () {
335
+ if [[ $FETCH == " true" ]]; then
336
+ return ;
337
+ fi
323
338
print_status_msg " Getting latest release now"
339
+
340
+ # Check if branch is on dev branch
341
+ # If so, exit
324
342
# Checkout staging release branch
325
343
checkout_branch " $STAGEBRANCH "
326
344
327
345
# Pull new release changes
328
- pull_branch " devops" " $STAGEBRANCH "
329
-
330
- # Remove the previous live branch
331
- rm_branch " $BRANCH "
332
-
333
- # Re-create and checkout a live branch, based on the staging release branch
334
- git branch " $BRANCH " 2>&1
335
- checkout_branch " $BRANCH "
346
+ pull_branch " $STAGEBRANCH "
336
347
348
+ # Checkout a live branch, based on the staging release branch
349
+ go_live
337
350
}
338
351
339
-
340
352
clean_repository () {
341
353
# Stash changes
342
354
print_status_msg " Stashing changes"
@@ -345,13 +357,18 @@ clean_repository() {
345
357
346
358
# Get latest release from Azure DevOps
347
359
get_new_release
348
-
360
+
349
361
# Add changes back
350
362
print_status_msg " Finished updating, adding changes back"
363
+
364
+ # Switching to dev branch to add changes
365
+ checkout_branch " devops" " $DEVBRANCH " " true"
366
+ pull_branch " $STAGEBRANCH "
367
+ # Pop changes back in
351
368
git_stash " pop"
352
369
353
370
# If soft error isn't enabled, add files and commit them
354
- if [ $SOFTERROR == " false" ] ; then
371
+ if [[ $SOFTERROR == " false" ] ] ; then
355
372
commit_git
356
373
# Push changes back to dev branch
357
374
push_branch " devops" " $DEVBRANCH "
@@ -360,6 +377,8 @@ clean_repository() {
360
377
error3
361
378
fi
362
379
380
+ # Create new live
381
+ go_live " $DEVBRANCH "
363
382
364
383
print_status_msg " Changes on the WordPress site have been commited, and were pushed to $DEVBRANCH branch"
365
384
}
@@ -390,10 +409,12 @@ add_or_remove_devops "add"
390
409
391
410
# Option selected to just query for changes
392
411
393
- if [[ $DIRTYBRANCH == " true" ]] ; then
394
- # Working directory clean
395
- print_status_msg " Working directory clean"
396
- get_new_release
412
+ if [[ $DIRTYBRANCH == " false" ]] ; then
413
+ # Make sure it's not missing a push back
414
+ dev_branch_push
415
+ # Working directory clean
416
+ print_status_msg " Working directory clean"
417
+ get_new_release
397
418
else
398
419
print_warning_msg " Uncommitted changes! Starting to clean..."
399
420
clean_repository
0 commit comments