-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathdbbzBuilderUtils.sh
341 lines (286 loc) · 12.8 KB
/
dbbzBuilderUtils.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
# internal veriables
mainBranchSegment=""
secondBranchSegment=""
baselineReferenceFile=""
segmentName=""
mergeBaseCommit=""
baseBranch=""
computeBuildConfiguration() {
# unset variables
HLQ=""
### computes the following environment variables for the dbbBuild.sh script
# Lifecycle - the zBuilder lifecyle configuration,
# e.q. impact --baselineRef release/rel-1.1.4
# HLQ - the high level qualifier to use
# zBuilderConfigOverrides - Config file containing variable overrides
# e.q. mainBuildBranch
##DEBUG ## echo -e "App name \t: ${App}"
##DEBUG ## echo -e "Branch name \t: ${Branch}"
# Compute HLQ preix and application name
HLQ=$(echo ${HLQPrefix}.${App:0:8} | tr '[:lower:]' '[:upper:]' | tr -d '-')
# Locate the baseline reference file based on the baselineReferenceLocation config in pipelineBackend.config
baselineReferenceFile="${AppDir}/$baselineReferenceLocation"
if [ ! -f "${baselineReferenceFile}" ]; then
rc=8
ERRMSG=$PGM": [ERROR] Applications baseline reference configuration file (${baselineReferenceFile}) was not found. rc="$rc
echo $ERRMSG
fi
branchConvention=(${Branch//// })
if [ $rc -eq 0 ]; then
if [ ${#branchConvention[@]} -gt 3 ]; then
rc=8
ERRMSG=$PGM": [ERROR] Script is only managing branch name with up to 3 segments (${Branch}) . rc="$rc
echo $ERRMSG
fi
fi
if [ $rc -eq 0 ]; then
# split the segments
mainBranchSegment=$(echo ${Branch} | awk -F "/" ' { print $1 }')
secondBranchSegment=$(echo ${Branch} | awk -F "/" ' { print $2 }')
thirdBranchSegment=$(echo ${Branch} | awk -F "/" ' { print $3 }')
# remove chars (. -) from the name
mainBranchSegmentTrimmed=$(echo ${mainBranchSegment} | tr -d '.-' | tr '[:lower:]' '[:upper:]')
# evaluate main segment
case $mainBranchSegmentTrimmed in
REL* | EPIC* | PROJ*)
# Release maintenance, epic and project branches are integration branches,
# that derive dependency information from mainBuildBranch configuration.
# evaluate third segment
if [ ! -z "${thirdBranchSegment}" ]; then
rc=8
ERRMSG=$PGM": [ERROR] Branch (${Branch}) does not follow standard naming conventions . rc="$rc
echo $ERRMSG
else
# feature branches for next planned release
computeSegmentName $secondBranchSegment
HLQ="${HLQ}.${mainBranchSegmentTrimmed:0:1}${segmentName:0:7}"
fi
if [ -z "${Lifecycle}" ]; then
Lifecycle="impact"
# obtain the baselineRef from file
getBaselineReference
Lifecycle="${Lifecycle} --baselineRef ${baselineRef}"
# Release maintenance / epic / project branch clones the dependency information from the main build branch
writezBuilderOverride "${mainBranchSegment}"
# appending the --debug flag to compile with TEST options
# Lifecycle="${Lifecycle} --debug"
fi
;;
FEATURE*)
# all feature branche start with F
# evaluate third segment
if [ ! -z "${thirdBranchSegment}" ]; then
# feature branches for EPIC workflow
# // skipped for simplicity
# computeSegmentName $secondBranchSegment
# HLQ="${HLQ}.E${segmentName:0:7}"
computeSegmentName $thirdBranchSegment
HLQ="${HLQ}.F${segmentName:0:7}"
writezBuilderOverride "epic/${secondBranchSegment}"
else
# feature branches for next planned release
computeSegmentName $secondBranchSegment
HLQ="${HLQ}.F${segmentName:0:7}"
fi
if [ -z "${Lifecycle}" ]; then
Lifecycle="impact"
# appending the --debug flag to compile with TEST options
# Lifecycle="${Lifecycle} --debug"
## evaluate the feature branch build behaviour
# compute the base branchName
if [ ! -z "${thirdBranchSegment}" ]; then
# epic branch workflow
baseBranch="origin/epic/${secondBranchSegment}"
else
# default dev workflow
baseBranch="origin/main"
fi
# assess the featureBranchBuildBehaviour setting
case $featureBranchBuildBehaviour in
cumulative)
Lifecycle="${Lifecycle} --baselineRef $baseBranch"
;;
merge-base)
getMergeBaseCommit
Lifecycle="${Lifecycle} --baselineRef $mergeBaseCommit"
;;
*)
## nothing to do
;;
esac
fi
;;
HOTFIX*)
# evaluate third segment
if [ ! -z "${thirdBranchSegment}" ]; then
# feature branches for hotfix workflow
# // skipped for simplicity
# computeSegmentName $secondBranchSegment
# HLQ="${HLQ}.R${segmentName:0:7}"
computeSegmentName $thirdBranchSegment
HLQ="${HLQ}.H${segmentName:0:7}"
else
rc=8
ERRMSG=$PGM": [ERROR] Hotfix branch (${Branch}) does not follow naming conventions . rc="$rc
echo $ERRMSG
fi
if [ -z "${Lifecycle}" ]; then
Lifecycle="impact"
writezBuilderOverride "release/${secondBranchSegment}"
# evaluate the feature branch build behaviour
if [ ! -z "${thirdBranchSegment}" ]; then
# define baseline reference
baseBranch="origin/release/${secondBranchSegment}"
else
echo $PGM": [WARNING] [Utilities/dbbBuildUtils.sh/computeBuildConfiguration] The hotfix branch name (${Branch}) does not match any case of the recommended naming conventions for branches. Performing an impact build."
echo $PGM": Read about our recommended naming conventions at https://ibm.github.io/z-devops-acceleration-program/docs/git-branching-model-for-mainframe-dev/#naming-conventions ."
fi
# assess the featureBranchBuildBehaviour setting
case $featureBranchBuildBehaviour in
cumulative)
Lifecycle="${Lifecycle} --baselineRef $baseBranch"
;;
merge-base)
getMergeBaseCommit
Lifecycle="${Lifecycle} --baselineRef $mergeBaseCommit"
;;
*)
## nothing to do
;;
esac
fi
;;
"PROD" | "MASTER" | "MAIN")
getBaselineReference
if [ -z "${Lifecycle}" ]; then
Lifecycle="impact"
Lifecycle="${Lifecycle} --baselineRef ${baselineRef}"
fi
if [ "${PipelineType}" == "release" ]; then
HLQ="${HLQ}.${mainBranchSegmentTrimmed:0:8}.REL"
else
HLQ="${HLQ}.${mainBranchSegmentTrimmed:0:8}.BLD"
# appending the --debug flag to compile with TEST options
# Lifecycle="${Lifecycle} --debug"
fi
;;
*)
# User did not follow the recommended naming conventions for branches. The branch name does not match any case of the recommended naming conventions.
# See https://ibm.github.io/z-devops-acceleration-program/docs/git-branching-model-for-mainframe-dev/#naming-conventions
rc=12
echo $PGM": [ERROR] [Utilities/dbbBuildUtils.sh/computeBuildConfiguration] The branch name (${Branch}) does not match any case of the recommended naming conventions for branches. Framework exits rc="$rc
echo $PGM": Read about our recommended naming conventions at https://ibm.github.io/z-devops-acceleration-program/docs/git-branching-model-for-mainframe-dev/#naming-conventions ."
;;
esac
# append pipeline preview if specified
if [ "${PipelineType}" == "preview" ]; then
if [ -z "${userDefinedLifecycle}" ]; then
Lifecycle="${Lifecycle} --preview"
fi
fi
# print computed values
##DEBUG ## echo -e "Computed hlq \t: ${HLQ}"
##DEBUG ## echo -e "Build option \t: ${Lifecycle}"
# unset internal variables
baselineRef=""
mainBranchSegment=""
mainBranchSegmentTrimmed=""
secondBranchSegment=""
secondBranchSegmentTrimmed=""
thirdBranchSegment=""
thirdBranchSegmentTrimmed=""
branchConvention=""
segmentName=""
mergeBaseCommit=""
baseBranch=""
fi
}
# Private method to retrieve the baseline reference from the configuration file
getBaselineReference() {
baselineRef=""
case $(echo $mainBranchSegment | tr '[:lower:]' '[:upper:]') in
"RELEASE" | "EPIC")
baselineRef=$(cat "${baselineReferenceFile}" | grep "^${mainBranchSegment}/${secondBranchSegment}" | awk -F "=" ' { print $2 }')
;;
"MAIN")
baselineRef=$(cat "${baselineReferenceFile}" | grep "^${mainBranchSegment}" | awk -F "=" ' { print $2 }')
;;
*)
rc=8
ERRMSG=$PGM": [ERROR] Branch name ${Branch} does not follow the recommended naming conventions to compute the baseline reference. Received '${mainBranchSegment}' which does not fall into the conventions of release, epic or main. rc="$rc
echo $ERRMSG
;;
esac
if [ -z "${baselineRef}" ]; then
rc=8
ERRMSG=$PGM": [ERROR] No baseline ref was found for branch name ${Branch} in ${baselineReferenceFile}. rc="$rc
echo $ERRMSG
fi
##DEBUG ## echo -e "baselineRef \t: ${baselineRef}" ## DEBUG
}
# Private method to retrive the merge-base as the baseline reference
# Requires the baseBranch to be computed
getMergeBaseCommit() {
# Execute Git cmd to obtain merge-base
if [ -z "${baseBranch}" ]; then
rc=8
ERRMSG=$PGM": [ERROR] To compute the merge base commit, it requires to define the baseBranch variable. rc="$rc
echo $ERRMSG
fi
# Execute Git cmd to obtain merge-base
CMD="git -C ${AppDir} merge-base ${Branch} ${baseBranch}"
mergeBaseCommit=$($CMD)
rc=$?
if [ $rc -ne 0 ]; then
ERRMSG=$PGM": [ERROR] Command ($CMD) failed. Git command to obtain the merge base commit failed for feature branch ${Branch}. See above error log. rc="$rc
echo $ERRMSG
fi
if [ $rc -eq 0 ]; then
if [ -z "${mergeBaseCommit}" ]; then
rc=8
ERRMSG=$PGM": [ERROR] Computation of Merge base commit failed for feature branch ${Branch}. rc="$rc
echo $ERRMSG
fi
fi
}
#
# computation of branch segments
# captured cases
#
# - containing numbers, assuming to be an work-item-id
# - containing strings and words separated by dashes, return first characters of each string
# - none of the above - return segment name in upper case w/o underscores
#
computeSegmentName() {
segmentName=$1
if [ ! -z $(echo "$segmentName" | tr -dc '0-9') ]; then
# "contains numbers"
retval=$(echo "$segmentName" | tr -dc '0-9')
elif [[ $segmentName == *"-"* ]]; then
# contains dashes
segmentNameTrimmed=$(echo "$segmentName" | awk -F "-" '{ for(i=1; i <= NF;i++) print($i) }' | cut -c 1-1)
segment1=$(echo "$segmentNameTrimmed" | tr -d '\n')
retval=$(echo "$segment1" | tr '[:lower:]' '[:upper:]')
else
retval=$(echo "$segmentName" | tr -d '_' | tr '[:lower:]' '[:upper:]')
fi
segmentName=$(echo "$retval")
}
#
# writezBuilderOverride
# writes file to log dir
# overrides the mainBuildBranch variable for MetadataInit task
#
writezBuilderOverride() {
mainBuildBranch=$1
echo "version: 1.0.0" > $zBuilderConfigOverrides
echo "application:" >> $zBuilderConfigOverrides
echo " name: ${App}" >> $zBuilderConfigOverrides
echo " tasks:" >> $zBuilderConfigOverrides
echo " # override mainBuildBranch" >> $zBuilderConfigOverrides
echo " - task: MetadataInit" >> $zBuilderConfigOverrides
echo " variables:" >> $zBuilderConfigOverrides
echo " - name: mainBuildBranch" >> $zBuilderConfigOverrides
echo " value: ${mainBuildBranch}" >> $zBuilderConfigOverrides
## debug #cat $zBuilderConfigOverrides
}