-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitClone.sh
executable file
·334 lines (296 loc) · 8.91 KB
/
gitClone.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
#!/bin/env bash
#===================================================================================
# NAME: Git-Clone.sh
#
# DESCRIPTION: The purpose of this script is to perform a git clone of a repository
# from within a Pipeline.
#
# SYNTAX: See Help() Section Below
#
# OPTIONS: See Help() Section Below
#
# RETURNS:
#
# rc - Return Code
#
# RETURN CODES:
#
# 0 - Successful
# 4 - Warning message(s) issued. See Console messages.
# 8 - Error encountered. See Console messages.
#
# NOTE(S):
#
# None
#
# Maintenance Log
#
# Date Who Vers Description
# ---------- --- ----- --------------------------------------------------------------
# 2023/07/06 TLD 1.0.0 Initial Release
# 2023/09/25 DB 1.1.0 Initial Release
#===================================================================================
Help() {
echo $PGM" - Clone Repository ("$PGMVERS") "
echo " "
echo "Description: The purpose of this script is to perform a git "
echo "clone of a repository from within a Pipeline. "
echo " "
echo "Syntax: "
echo " "
echo " "$PGM" [Options] "
echo " "
echo "Options: "
echo " "
echo " -h - Help. "
echo " "
echo " -r <repo> - Application Repository to be cloned. "
echo " Default=None, Required. "
echo " "
echo " Ex: git@<sshurl>:<reponame> "
echo " "
echo " -w <workspace> - Directory Path to a unique "
echo " working directory "
echo " Either an absolute path "
echo " or relative path. "
echo " If a relative path is provided, "
echo " buildRootDir and the workspace "
echo " path are combined "
echo " Default=None, Required. "
echo " "
echo " Ex: MortgageApplication/main/build-1 "
echo " "
echo " Ex: /../dbb-logs "
echo " "
echo " -b <Branch> - Name of the Branch to be cloned. "
echo " Default=None, Required. "
echo " "
echo " Ex: refs/heads/main "
echo " "
exit 0
}
#
# Customization
# Configuration file leveraged by the backend scripts
# Either an absolute path or a relative path to the current working directory
SCRIPT_HOME="`dirname "$0"`"
pipelineConfiguration="${SCRIPT_HOME}/pipelineBackend.config"
# Customization - End
#
# Internal Variables
#set -x # Uncomment to enable shell script debug
#export BASH_XTRACEFD=1 # Write set -x trace to file descriptor
PGM=$(basename "$0")
PGMVERS="1.1.0"
USER=$(whoami)
SYS=$(uname -Ia)
rc=0
ERRMSG=""
Repo=""
WorkDir=""
Branch=""
HELP=$1
if [ "$HELP" = "?" ]; then
Help
fi
# Validate Shell environment
currentShell=$(ps -p $$ | grep bash)
if [ -z "${currentShell}" ]; then
rc=8
ERRMSG=$PGM": [ERROR] The scripts are designed to run in bash. You are running a different shell. rc=${rc}. \n. $(ps -p $$)."
echo $ERRMSG
fi
#
# Print script title
if [ $rc -eq 0 ]; then
echo $PGM": [INFO] Git Clone Wrapper. Version="$PGMVERS
fi
# Read and import pipeline configuration
if [ $rc -eq 0 ]; then
if [ ! -f "${pipelineConfiguration}" ]; then
rc=8
ERRMSG=$PGM": [ERROR] Pipeline Configuration File (${pipelineConfiguration}) was not found. rc="$rc
echo $ERRMSG
else
source $pipelineConfiguration
fi
fi
#
# Get Options
if [ $rc -eq 0 ]; then
while getopts "h:r:w:b:" opt; do
case $opt in
h)
Help
;;
r)
argument="$OPTARG"
nextchar="$(expr substr $argument 1 1)"
if [ -z "$argument" ] || [ "$nextchar" = "-" ]; then
rc=4
ERRMSG=$PGM": [WARNING] Git Repository URL is required. rc="$rc
echo $ERRMSG
break
fi
Repo="$argument"
;;
w)
argument="$OPTARG"
nextchar="$(expr substr $argument 1 1)"
if [ -z "$argument" ] || [ "$nextchar" = "-" ]; then
rc=4
ERRMSG=$PGM": [WARNING] Build Workspace Folder Name is required. rc="$rc
echo $ERRMSG
break
fi
Workspace="$argument"
;;
b)
argument="$OPTARG"
nextchar="$(expr substr $argument 1 1)"
if [ -z "$argument" ] || [ "$nextchar" = "-" ]; then
rc=4
ERRMSG=$PGM": [WARNING] Branch Name is required. rc="$rc
echo $ERRMSG
break
fi
Branch="$argument"
;;
\?)
Help
rc=1
break
;;
:)
rc=4
ERRMSG=$PGM": [WARNING] Option -$OPTARG requires an argument. rc="$rc
echo $ERRMSG
break
;;
esac
done
fi
#
# Validate Options
validateOptions(){
if [ -z "${Repo}" ]; then
rc=8
ERRMSG=$PGM": [ERROR] Application Repository is required. rc="$rc
echo $ERRMSG
fi
if [ -z "${Workspace}" ]; then
rc=8
ERRMSG=$PGM": [ERROR] Unique Workspace Path is required. rc="$rc
echo $ERRMSG
else
if [[ ${Workspace:0:1} != "/" ]] ; then
if [ ! -d "${buildRootDir}" ]; then
rc=8
ERRMSG=$PGM": [ERROR] Workspace Directory (${buildRootDir}) was not found. rc="$rc
echo $ERRMSG
fi
fi
fi
if [ -z "${Branch}" ]; then
rc=8
ERRMSG=$PGM": [ERROR] Branch Name is required. rc="$rc
echo $ERRMSG
fi
}
#
# Call validate Options
if [ $rc -eq 0 ]; then
validateOptions
fi
#
# Fix up the Branch and Workspace Name
if [ $rc -eq 0 ]; then
# Strip the prefix of the branch name for cloning
BranchID=${Branch##*"refs/heads/"}
# Isolate the Workspace Name from the Repo Name
GitDir=$(basename ${Repo})
GitDir=${GitDir%.*}
fi
#
# Ready to go
if [ $rc -eq 0 ]; then
echo $PGM": [INFO] **************************************************************"
echo $PGM": [INFO] ** Start Git Clone on HOST/USER: ${SYS}/${USER}"
echo $PGM": [INFO] ** Repo:" ${Repo}
echo $PGM": [INFO] ** WorkDir:" $(getWorkDirectory)
echo $PGM": [INFO] ** GitDir:" ${GitDir}
echo $PGM": [INFO] ** Branch:" ${Branch} "->" ${BranchID}
echo $PGM": [INFO] **************************************************************"
echo ""
fi
#
# Set up to perform the clone of the Repo
if [ $rc -eq 0 ]; then
if [[ ${Workspace:0:1} != "/" ]] ; then
cd ${buildRootDir}
rc=$?
fi
if [ $rc -eq 0 ]; then
mkdir -p ${Workspace}
rc=$?
fi
if [ $rc -eq 0 ]; then
cd ${Workspace}
rc=$?
if [ $rc -ne 0 ]; then
ERRMSG=$PGM": [ERROR] Unable to change to directory ${Workspace}. rc="$rc
echo $ERRMSG
rc=8
fi
else
ERRMSG=$PGM": [ERROR] Unable to create directory ${Workspace}. rc="$rc
echo $ERRMSG
rc=8
fi
fi
#
# Clone the Repo to z/OS UNIX System Services with a re-Direct of STDERR to STDOUT
if [ $rc -eq 0 ]; then
echo $PGM": [INFO] Preforming Git Clone of Repo ${Repo}, Branch ${BranchID} to $(getWorkDirectory)"
git clone -b ${BranchID} ${Repo} 2>&1
rc=$?
if [ $rc -ne 0 ]; then
ERRMSG=$PGM": [ERROR] Unable to Clone Repo ${Repo}, Branch ${BranchID}. rc="$rc
echo $ERRMSG
rc=8
fi
fi
#
# Check status of the cloned Repo
if [ $rc -eq 0 ]; then
cd ${GitDir}
rc=$?
if [ $rc -eq 0 ]; then
echo $PGM": [INFO] Git Status for ${GitDir}"
git status
rc=$?
if [ $rc -eq 0 ]; then
echo $PGM": [INFO] Git Show-Ref for ${GitDir}"
git show-ref
rc=$?
if [ $rc -ne 0 ]; then
ERRMSG=$PGM": [ERROR] Error show all ref of Git Repo. rc="$rc
echo $ERRMSG
rc=8
fi
else
ERRMSG=$PGM": [ERROR] Error determining status of Git Repo. rc="$rc
echo $ERRMSG
rc=8
fi
else
ERRMSG=$PGM": [ERROR] Unable to change to directory ${GitDir}. rc="$rc
echo $ERRMSG
rc=8
fi
fi
if [ $rc -eq 0 ]; then
ERRMSG=$PGM": [INFO] Clone Repository Complete. rc="$rc
echo $ERRMSG
fi
exit $rc