Skip to content

Commit c1c9dee

Browse files
authored
✨🎨 added more options and improved reliability (#29)
* ✨🎨 added more options and improved reliability closes #18 improvement: option to delete remote Live branch if no merge closes #15 improvement: break up methods and optargs to support actual choices closes #28 enhancement: Add more validation for option arguments we aren't going to delete any branches remotely closes #16 improvement: add ability to set commit message using optarg closes #14 improvement: add more checks for required arguments to launch closes #10 Add a use back to the wildcard OPTARG * 🍻 removing wildcard handler, still having issues related to Add a use back to the wildcard OPTARG #10 related to improvement: break up methods and optargs to support actual choices #15
1 parent 8201b01 commit c1c9dee

File tree

1 file changed

+252
-67
lines changed

1 file changed

+252
-67
lines changed

wp-git-sync.sh

Lines changed: 252 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,134 @@
1-
#!/bin/sh
1+
#!/bin/bash
22

33
# WordPress SSH Git CI Script
44
# v1.0.2
55

66
# Set argument parameters on start
7-
8-
TOKENUSER="NONE"
9-
TOKEN="NONE"
10-
11-
while getopts b:d:g:p:r:t:u: option; do
12-
case "${option}" in
13-
b) BRANCH=${OPTARG};;
14-
d) DEVBRANCH=${OPTARG};;
15-
g) GITREPO=${OPTARG};;
16-
p) GITPATH=${OPTARG};;
17-
r) RUNTYPE=${OPTARG};;
18-
t) TOKEN=${OPTARG};;
19-
u) TOKENUSER=${OPTARG};;
20-
*) RUNTYPE="push";;
7+
BRANCH=live
8+
DEVBRANCH=dev
9+
STAGEBRANCH=stage
10+
PROJDIR=wordpress
11+
SOFTERROR=0
12+
ERRORMESSAGES=""
13+
while :; do
14+
case "$1" in
15+
16+
-b|--branch) #optional
17+
if [[ $2 ]]; then
18+
BRANCH=$2
19+
shift
20+
else
21+
ERRORMESSAGES="${ERRORMESSAGES} ERROR: '-b | --branch' requires a non-empty option argument."
22+
fi
23+
;;
24+
-c|--commit) #optional -- default value is true
25+
if [[ $2 ]] && [[ $2 = "no" || $2 = "n" ]] ; then
26+
COMMIT=0
27+
shift
28+
else
29+
COMMIT=1
30+
shift
31+
fi
32+
;;
33+
-d|--devbranch) #optional
34+
if [[ $2 ]]; then
35+
DEVBRANCH=$2
36+
shift
37+
else
38+
ERRORMESSAGES="ERROR: '-d | --devbranch' requires a non-empty option argument."
39+
fi
40+
;;
41+
-f|--fullorigin) #optional
42+
if [[ $2 ]]; then
43+
ORIGIN=$2
44+
shift
45+
else
46+
ERRORMESSAGES="ERROR: '-f | --fullorigin' requires a non-empty option argument."
47+
fi
48+
;;
49+
-g|--gitrepo) #semi-optional -- if empty ORIGIN string is required
50+
if [[ $2 ]]; then
51+
GITREPO=$2
52+
shift
53+
else
54+
ERRORMESSAGES="ERROR: '-g | --gitrepo' requires a non-empty option argument."
55+
fi
56+
;;
57+
-m|--message) #optional
58+
if [[ $2 ]]; then
59+
MESSAGE=$2
60+
shift
61+
else
62+
ERRORMESSAGES="ERROR: '-m | --message' requires a non-empty option argument."
63+
fi
64+
;;
65+
-o|--onchange) #optional
66+
if [[ $2 ]]; then
67+
ONCHANGE=$2
68+
shift
69+
else
70+
ERRORMESSAGES="ERROR: '-o | --onchange' requires a non-empty option argument."
71+
fi
72+
;;
73+
-p|--projectdir) #optional
74+
if [[ $2 ]]; then
75+
PROJDIR=$2
76+
shift
77+
else
78+
ERRORMESSAGES="ERROR: '-p | --projectdir' requires a non-empty option argument."
79+
fi
80+
;;
81+
-s|--stagebranch) #optional
82+
if [[ $2 ]]; then
83+
STAGEBRANCH=$2
84+
shift
85+
else
86+
ERRORMESSAGES="ERROR: '-s | --stagebranch' requires a non-empty option argument."
87+
fi
88+
;;
89+
-se|--softerror) #optional
90+
SOFTERROR=1
91+
shift
92+
;;
93+
-t|--token) #semi-optional -- if empty ORIGIN string is required
94+
if [[ $2 ]]; then
95+
TOKEN=$2
96+
shift
97+
else
98+
ERRORMESSAGES="ERROR: '-t | --token' requires a non-empty option argument."
99+
fi
100+
;;
101+
-u|--tokenuser) #semi-optional -- if empty ORIGIN string is required
102+
if [[ $2 ]]; then
103+
TOKENUSER=$2
104+
shift
105+
else
106+
ERRORMESSAGES="ERROR: '-u | --tokenuser' requires a non-empty option argument."
107+
fi
108+
;;
109+
--)
110+
shift
111+
break
112+
;;
113+
*)
114+
break
21115
esac
116+
shift
22117
done
23118

24119
# Remote connection methods
25120

26121
add_devops_remote() {
27-
git remote add devops "https://${TOKENUSER}:${TOKEN}@${GITREPO}" 2>&1
28-
print_status_msg "Remote devops addded"
122+
git remote add devops "$ORIGIN" 2>&1
123+
git remote update 2>&1
29124
git remote -v 2>&1
125+
print_status_msg "Remote devops addded"
30126
}
31127

32128
rm_devops_remote() {
33129
git remote rm devops 2>&1
34-
print_status_msg "Remote devops removed"
35130
git remote -v 2>&1
131+
print_status_msg "Remote devops removed"
36132
}
37133

38134
add_or_remove_devops() {
@@ -57,7 +153,21 @@ add_or_remove_devops() {
57153

58154
# Handling changes on WordPress server
59155

60-
## Stashing changes
156+
## Branches
157+
158+
checkout_branch() {
159+
if [ -z "$2" ] ; then
160+
git checkout "$1" 2>&1
161+
else
162+
git checkout devops "$2" 2>&1
163+
fi
164+
}
165+
166+
rm_branch() {
167+
git branch -D "$1"
168+
}
169+
170+
## Stashes
61171

62172
is_stashes() {
63173
output=$(git stash list )
@@ -72,7 +182,7 @@ is_stashes() {
72182

73183
git_stash() {
74184
is_stashes "check"
75-
if [ "$stashes" -eq 1 ] ; then
185+
if [[ $stashes -eq 1 ]] ; then
76186
case $1 in
77187
clear)
78188
git stash clear 2>&1;;
@@ -87,73 +197,144 @@ git_stash() {
87197
return
88198
}
89199

90-
## Commit changes
200+
## Committing changes
91201

92202
commit_git() {
93-
git add -A . 2>&1
94-
git commit -m "Server Side Commit
95-
96-
This was commited from SiteGround during release push" 2>&1
203+
if [[ $COMMIT -eq 1 ]] ; then
204+
git add -A . 2>&1
205+
git commit -m "$MESSAGE" 2>&1
206+
print_status_msg "Commited on $BRANCH with message:
207+
$MESSAGE"
208+
else
209+
print_status_msg "$BRANCH changes were not commited"
210+
fi
97211
}
98212

99-
## Merge changes
213+
## Pull branch from remote
100214

101-
merge_from_devops() {
102-
git merge "${DEVBRANCH}" 2>&1
103-
git branch -D "${DEVBRANCH}" 2>&1
215+
pull_branch() {
216+
msg="Pull branch "
217+
if [ -z "$2" ] ; then
218+
git pull "$1" 2>&1
219+
msg=$msg$1
220+
else
221+
git pull devops "$2" 2>&1
222+
msg=$msg$2
223+
fi
224+
print_status_msg "$msg is complete."
104225
}
105226

106-
# Pull from repository methods
227+
## Push branch to remote
107228

108-
pull_from_devops() {
109-
add_or_remove_devops "add"
110-
git_stash "clear"
111-
git_stash "u"
112-
git remote update 2>&1
113-
git checkout "${DEVBRANCH}" 2>&1
114-
git pull devops "${DEVBRANCH}" 2>&1
115-
git checkout "${BRANCH}" 2>&1
116-
git pull devops "${BRANCH}" 2>&1
117-
merge_from_devops
118-
git_stash "pop"
119-
commit_git
120-
add_or_remove_devops "rm"
121-
print_status_msg "pull_from_dev function was executed successfully."
229+
230+
push_branch() {
231+
msg="Push branch "
232+
if [ -z "$2" ] ; then
233+
git push "$1" 2>&1
234+
msg=$msg$1
235+
else
236+
git push devops "$2" 2>&1
237+
msg=$msg$2
238+
fi
239+
print_status_msg "$msg is complete."
122240
}
123241

124-
# Push to repository methods
242+
# Utilities
125243

126-
push_to_devops() {
127-
pull_from_devops
128-
add_or_remove_devops "add"
129-
git push devops "$BRANCH" 2>&1
130-
print_status_msg "push_to_dev function was executed successfully."
244+
error3() {
131245
add_or_remove_devops "rm"
246+
exit 3
132247
}
133248

134-
# Utilities
249+
throw_errors() {
250+
if [ ${#ERRORMESSAGES} -gt 0 ] ; then
251+
for ERRORMSG in ${ERRORMESSAGES}; do
252+
print_error_msg "${ERRORMSG}"
253+
done
254+
error3
255+
fi
256+
}
135257

136258
set_defaults() {
137-
if [ -z "$GITPATH" ]; then
138-
print_error_msg "-p is empty. Please provide a path to git directory for repository & try again."
139-
exit 2
259+
# If error messages from defaults, list them and exit out
260+
throw_errors
261+
262+
# Set origin url to use
263+
if [ -z "$ORIGIN" ] ; then
264+
# if origin isn't set and the other arguments needed to make an origin aren't set, throw an error!
265+
if [[ -z $TOKENUSER || -z $TOKEN || -z $GITREPO ]] ; then
266+
error3
267+
fi
268+
ORIGIN="https://${TOKENUSER}:${TOKEN}@${GITREPO}"
140269
fi
141-
if [ -z "$RUNTYPE" ]; then
142-
RUNTYPE='push'
270+
271+
# Set the default commit message
272+
if [[ -z $MESSAGE ]]; then
273+
MESSAGE="Server Side Commit
274+
275+
This was commited from the WordPress server"
143276
fi
277+
278+
# Set if branch has changes; and if so, how to handle those changes
279+
if output=$(git status --untracked-files=no --porcelain) && [ -z "$output" ] ; then
280+
DIRTYBRANCH=0
281+
else
282+
if [[ $ONCHANGE = "Stop" || $ONCHANGE = "stop" ]] ; then
283+
error3
284+
else
285+
if [[ -n $ONCHANGE ]] && [[ $ONCHANGE != "commit" && $ONCHANGE != "Commit" ]]; then
286+
print_error_msg '-o or --onchange has an incorrect value. Please use commit or stop for your selection. If you leave it blank, commit is the default.'
287+
error3
288+
else
289+
ONCHANGE="commit"
290+
DIRTYBRANCH=1
291+
fi
292+
fi
293+
fi
294+
}
295+
296+
# Method models
297+
298+
get_new_release() {
299+
# Checkout staging release branch
300+
checkout_branch "$STAGEBRANCH"
301+
302+
# Pull new release changes
303+
pull_branch "devops" "$STAGEBRANCH"
304+
305+
# Remove the previous live branch
306+
rm_branch "$BRANCH"
307+
308+
# Re-create and checkout a live branch, based on the staging release branch
309+
git branch "$BRANCH" 2>&1
310+
checkout_branch "$BRANCH"
311+
144312
}
145313

314+
146315
clean_repository() {
147-
# code
148-
if [ -n "$TOKEN" ] || [ "$TOKEN" != "NONE" ] || [ -n "$TOKENUSER" ] || [ "$TOKENUSER" != "NONE" ]; then
149-
push_to_devops
316+
# Stash changes
317+
git_stash "clear"
318+
git_stash "u"
319+
320+
# Get latest release from Azure DevOps
321+
get_new_release
322+
323+
# Add changes back
324+
git_stash "pop"
325+
326+
# If soft error isn't enabled, add files and commit them
327+
if [ $SOFTERROR -eq 0 ] ; then
150328
commit_git
151-
push_to_devops
152-
print_status_msg "clean_rep function was executed successfully."
153329
else
154-
print_error_msg "You need to provide a token using -t and a username for it using -u"
155-
exit 2
330+
print_error_msg "Changes on server, soft error selected. Release is pulled, changes are popped back, branch is waiting user intervention."
331+
error3
156332
fi
333+
334+
# Push changes back to dev branch
335+
push_branch "devops" "$DEVBRANCH"
336+
337+
print_status_msg "Changes on the WordPress site have been commited, and were pushed to $DEVBRANCH branch"
157338
}
158339

159340
## Print messages in colored text
@@ -175,16 +356,20 @@ print_error_msg() {
175356

176357
# Methods and properties set, now start script logic models
177358

359+
cd "$PROJDIR" || exit 1
178360
set_defaults
179361

180-
cd "${GITPATH}" || exit 1
362+
add_or_remove_devops "add"
363+
364+
# Option selected to just query for changes
181365

182-
if output=$(git status --untracked-files=no --porcelain) && [ -z "$output" ] && [ "$RUNTYPE" != "Clean" ] && [ "$RUNTYPE" != "clean" ] ; then
366+
if [ $DIRTYBRANCH -eq 0 ] ; then
183367
# Working directory clean
184368
print_status_msg "Working directory clean"
185-
push_to_devops
369+
get_new_release
186370
else
187371
print_warning_msg "Uncommitted changes! Starting to clean..."
188372
clean_repository
189373
fi
190-
export DEFAULT
374+
375+
add_or_remove_devops "rm"

0 commit comments

Comments
 (0)