11#! /bin/bash
22
33# WordPress SSH Git CI Script
4- # v1.0.2
4+ # v1.2.0
55
66# Set argument parameters on start
77BRANCH=live
@@ -11,9 +11,10 @@ PROJDIR=wordpress
1111ERRORMESSAGES=" "
1212TRACKEDONLY=" false"
1313COMMIT=" true"
14+ SOFTERROR=" false"
15+ FETCH=" false"
1416while : ; do
1517 case " $1 " in
16-
1718 -b|--branch) # optional
1819 if [[ $2 ]]; then
1920 BRANCH=$2
@@ -23,12 +24,9 @@ while :; do
2324 fi
2425 ;;
2526 -c|--commit) # optional -- default value is true
26- if [[ $2 = " no" || $2 = " n" ]] ; then
27+ if [[ $2 == " no" || $2 = = " n" ]] ; then
2728 COMMIT=" false"
2829 shift
29- else
30- COMMIT=" true"
31- shift
3230 fi
3331 ;;
3432 -d|--devbranch) # optional
@@ -39,7 +37,11 @@ while :; do
3937 ERRORMESSAGES=" ERROR: '-d | --devbranch' requires a non-empty option argument."
4038 fi
4139 ;;
42- -f|--fullorigin) # optional
40+ -fe|--fetch) # optional
41+ FETCH=" true"
42+ shift
43+ ;;
44+ -fo|--fullorigin) # optional
4345 if [[ $2 ]]; then
4446 ORIGIN=$2
4547 shift
@@ -71,15 +73,15 @@ while :; do
7173 ERRORMESSAGES=" ERROR: '-o | --onchange' requires a non-empty option argument."
7274 fi
7375 ;;
74- -p |--projectdir) # optional
76+ -pd |--projectdir) # optional
7577 if [[ $2 ]]; then
7678 PROJDIR=$2
7779 shift
7880 else
7981 ERRORMESSAGES=" ERROR: '-p | --projectdir' requires a non-empty option argument."
8082 fi
8183 ;;
82- -s |--stagebranch) # optional
84+ -sb |--stagebranch) # optional
8385 if [[ $2 ]]; then
8486 STAGEBRANCH=$2
8587 shift
@@ -91,7 +93,7 @@ while :; do
9193 SOFTERROR=" true"
9294 shift
9395 ;;
94- -t |--token) # semi-optional -- if empty ORIGIN string is required
96+ -to |--token) # semi-optional -- if empty ORIGIN string is required
9597 if [[ $2 ]]; then
9698 TOKEN=$2
9799 shift
@@ -124,35 +126,39 @@ done
124126# Remote connection methods
125127
126128add_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
130131 print_status_msg " Remote devops addded"
131132}
132133
133134rm_devops_remote () {
134- git remote rm devops 2>&1
135- git remote -v 2>&1
135+ git remote rm devops
136136 print_status_msg " Remote devops removed"
137137}
138138
139139add_or_remove_devops () {
140140 output=$( git remote )
141141 case $output in
142- * +devops* )
143- remoteAdded=" true" ;;
142+ * devops* )
143+ print_warning_msg " Remote DevOps found"
144+ remoteAdded=" true"
145+ ;;
144146 * )
145- remoteAdded=" false" ;;
147+ print_warning_msg " Remote DevOps not found"
148+ remoteAdded=" false"
149+ ;;
146150 esac
147151 case $1 in
148152 add)
149- if [ $remoteAdded == " false" ] ; then
153+ if [[ $remoteAdded == " false" ] ] ; then
150154 add_devops_remote
151- fi ;;
155+ fi
156+ ;;
152157 rm)
153- if [ $remoteAdded == " true" ] ; then
158+ if [[ $remoteAdded == " true" ] ] ; then
154159 rm_devops_remote
155- fi ;;
160+ fi
161+ ;;
156162 esac
157163}
158164
@@ -161,27 +167,37 @@ add_or_remove_devops() {
161167# # Branches
162168
163169checkout_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
165175 git checkout " $1 " 2>&1
166176 else
167177 git checkout devops " $2 " 2>&1
168178 fi
169179}
170180
171181rm_branch () {
172- git branch -D " $1 "
182+ git branch -D " $1 " 2>&1
173183}
174184
185+ go_live () {
186+ git checkout -B " $BRANCH " 2>&1
187+ if [[ $1 ]] ; then
188+ rm_branch " $1 "
189+ fi
190+ }
175191# # Stashes
176192
177193is_stashes () {
178194 output=$( git stash list )
179195 case $1 in
180- check) stashes=0 ;;
196+ check) stashes=" false " ;;
181197 esac
182198 case $output in
183- * + " stash" * )
184- stashes=1 ;;
199+ * " stash" * )
200+ stashes=" true " ;;
185201 esac
186202}
187203
@@ -205,10 +221,11 @@ git_stash() {
205221# # Committing changes
206222
207223commit_git () {
208- if [[ $COMMIT == " commit" ]] ; then
224+ if [[ $COMMIT == " true" ]] ; then
225+ checkout_branch " devops" " $DEVBRANCH " " true"
209226 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:
212229 $MESSAGE "
213230 else
214231 print_status_msg " $BRANCH changes were not commited"
@@ -217,27 +234,14 @@ commit_git() {
217234
218235# # Pull branch from remote
219236
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-
233237pull_branch () {
234238 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 " )
240241 msg=$msg $2
242+ else
243+ git pull devops " $1 " 2>&1
244+ msg=$msg $1
241245 fi
242246 print_status_msg " $msg is complete."
243247}
@@ -247,7 +251,7 @@ pull_branch() {
247251
248252push_branch () {
249253 msg=" Push branch "
250- if [ -z " $2 " ] ; then
254+ if [[ -z " $2 " ] ] ; then
251255 git push " $1 " 2>&1
252256 msg=$msg $1
253257 else
@@ -265,7 +269,7 @@ error3() {
265269}
266270
267271throw_errors () {
268- if [ ${# ERRORMESSAGES} -gt 0 ] ; then
272+ if [[ ${# ERRORMESSAGES} -gt 0 ] ] ; then
269273 for ERRORMSG in ${ERRORMESSAGES} ; do
270274 print_error_msg " ${ERRORMSG} "
271275 done
@@ -300,7 +304,7 @@ set_defaults() {
300304 output=$( git status --porcelain)
301305 fi
302306
303- if [ -z " $output " ] ; then
307+ if [[ -z $output ] ] ; then
304308 DIRTYBRANCH=" false"
305309 else
306310 if [[ $ONCHANGE = " Stop" || $ONCHANGE = " stop" ]] ; then
@@ -319,24 +323,32 @@ set_defaults() {
319323
320324# Method models
321325
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+
322334get_new_release () {
335+ if [[ $FETCH == " true" ]]; then
336+ return ;
337+ fi
323338 print_status_msg " Getting latest release now"
339+
340+ # Check if branch is on dev branch
341+ # If so, exit
324342 # Checkout staging release branch
325343 checkout_branch " $STAGEBRANCH "
326344
327345 # 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 "
336347
348+ # Checkout a live branch, based on the staging release branch
349+ go_live
337350}
338351
339-
340352clean_repository () {
341353 # Stash changes
342354 print_status_msg " Stashing changes"
@@ -345,13 +357,18 @@ clean_repository() {
345357
346358 # Get latest release from Azure DevOps
347359 get_new_release
348-
360+
349361 # Add changes back
350362 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
351368 git_stash " pop"
352369
353370 # If soft error isn't enabled, add files and commit them
354- if [ $SOFTERROR == " false" ] ; then
371+ if [[ $SOFTERROR == " false" ] ] ; then
355372 commit_git
356373 # Push changes back to dev branch
357374 push_branch " devops" " $DEVBRANCH "
@@ -360,6 +377,8 @@ clean_repository() {
360377 error3
361378 fi
362379
380+ # Create new live
381+ go_live " $DEVBRANCH "
363382
364383 print_status_msg " Changes on the WordPress site have been commited, and were pushed to $DEVBRANCH branch"
365384}
@@ -390,10 +409,12 @@ add_or_remove_devops "add"
390409
391410# Option selected to just query for changes
392411
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
397418else
398419 print_warning_msg " Uncommitted changes! Starting to clean..."
399420 clean_repository
0 commit comments