-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpre-build-phase-1.sh
More file actions
294 lines (247 loc) · 11.7 KB
/
pre-build-phase-1.sh
File metadata and controls
294 lines (247 loc) · 11.7 KB
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
if [[ -z $ISPULLREQUEST ]]; then
>&2 echo 'ENV Error: The environment variable ISPULLREQUEST is not set.'
exit 1
fi
isPullRequest="${ISPULLREQUEST,,}"
if ! [[ $isPullRequest =~ (true|false) ]]; then
>&2 echo 'ENV Error: The environment variable ISPULLREQUEST is not a boolean.'
exit 1
fi
if [[ -z $BUILD_SOURCEBRANCH ]]; then
>&2 echo 'ENV Error: The environment variable BUILD_SOURCEBRANCH is not set.'
exit 1
fi
# refs/heads/master
# refs/pull/1/merge
# refs/tags/your-tag-name
activeBranch=$BUILD_SOURCEBRANCH
releaseGlob="[[:digit:]]*.[[:digit:]]*.[[:digit:]]*"
releaseGrepRegex="^[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+\(.[[:digit:]]\+\)\?$"
releaseCandidateGrepRegex="^[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+\(.[[:digit:]]\+\)\?-rc[[:digit:]]\+$"
# get the latest tag on the current branch
latestReleaseTag=$(git tag --list $releaseGlob --sort=-creatordate | grep $releaseGrepRegex | head -n 1)
# get the tag before the latest on the current branch
previousReleaseTag=$(git tag --list $releaseGlob --sort=-creatordate | grep $releaseGrepRegex | head -n 2 | tail -n 1)
# get the latest release-candidate tag on the current branch
latestReleaseCandidateTag=$(git tag --list $releaseGlob --sort=-creatordate | grep $releaseCandidateGrepRegex | head -n 1)
# get the candidate tag before the latest on the current branch
previousReleaseCandidateTag=$(git tag --list $releaseGlob --sort=-creatordate | grep $releaseCandidateGrepRegex | head -n 2 | tail -n 1)
# get the latest release tag on the main branch
latestReleaseMainTag=$(git tag --list $releaseGlob --merged main --sort=-creatordate | grep $releaseGrepRegex | head -n 1)
# get the latest release-candidate tag on the main branch
latestReleaseCandidateMainTag=$(git tag --list $releaseGlob --merged main --sort=-creatordate | grep $releaseCandidateGrepRegex | head -n 1)
releaseTagOnLatestCommit=$(git tag --contains HEAD --list $releaseGlob | grep $releaseGrepRegex | head -n 1)
releaseCandidateTagOnLatestCommit=$(git tag --contains HEAD --list $releaseGlob | grep $releaseCandidateGrepRegex | head -n 1)
echo "Latest Release: ${latestReleaseTag:-empty}"
echo "Previous Release: ${previousReleaseTag:-empty}"
echo "Latest RC: ${latestReleaseCandidateTag:-empty}"
echo "Previous RC: ${previousReleaseCandidateTag:-empty}"
echo "Latest Main Release: ${latestReleaseMainTag:-empty}"
echo "Latest Main RC: ${latestReleaseCandidateMainTag:-empty}"
echo "Latest Commit Release: ${releaseTagOnLatestCommit:-empty}"
echo "Latest Commit RC: ${releaseCandidateTagOnLatestCommit:-empty}"
echo "Active Branch: $activeBranch"
if [[ ! -z $releaseTagOnLatestCommit && ! -z $releaseCandidateTagOnLatestCommit ]]; then
>&2 echo "Tag Error: The build can be marked as release or release-candidate (not both)."
exit 1
fi
isMarketplaceRelease=false
isReleaseCandidate=false
if [[ ! -z $releaseTagOnLatestCommit || ! -z $releaseCandidateTagOnLatestCommit ]]; then
if [[ $activeBranch = "refs/heads/main" || $activeBranch = "refs/tags/$releaseTagOnLatestCommit" || $activeBranch = "refs/tags/$releaseCandidateTagOnLatestCommit" ]]; then
if [[ ! -z $releaseTagOnLatestCommit && $releaseTagOnLatestCommit = $latestReleaseMainTag && $isPullRequest = false ]]; then
# this is release-tag branch, e.g. tags/1.0.0 (azure pipelines tag trigger)
# or main branch with the latest release-tag (azure pipelines manual trigger)
isMarketplaceRelease=true
elif [[ ! -z $releaseTagOnLatestCommit && ! $releaseTagOnLatestCommit = $latestReleaseMainTag ]]; then
>&2 echo "Tag Error: Marking a build as realease is only allowed on the main branch (a tag branch but version is not equal to main)."
exit 1
fi
if [[ ! -z $releaseCandidateTagOnLatestCommit && $releaseCandidateTagOnLatestCommit = $latestReleaseCandidateMainTag && $isPullRequest = false ]]; then
isReleaseCandidate=true
elif [[ ! -z $releaseCandidateTagOnLatestCommit && ! $releaseCandidateTagOnLatestCommit = $latestReleaseCandidateMainTag ]]; then
>&2 echo "Tag Error: Marking a build as realease-candidate is only allowed on the main branch (a tag branch but version is not equal to main)."
exit 1
fi
else
if [[ ! -z $releaseTagOnLatestCommit ]]; then
>&2 echo "Tag Error: Marking a build as realease is only allowed on the main branch (not main or tag branch)."
exit 1
fi
if [[ ! -z $releaseCandidateTagOnLatestCommit ]]; then
>&2 echo "Tag Error: Marking a build as realease-candidate is only allowed on the main branch (not main or tag branch)."
exit 1
fi
fi
fi
echo "Is Marketplace-Ready: $isMarketplaceRelease"
echo "Is Release Candidate: $isReleaseCandidate"
currentMajor=`echo $latestReleaseTag | cut -d. -f1`
currentMinor=`echo $latestReleaseTag | cut -d. -f2`
currentPatch=`echo $latestReleaseTag | cut -d. -f3`
currentBuild=`echo $latestReleaseTag | cut -d. -f4`
if [[ -z $currentBuild ]]; then
currentBuild=0
elif (( currentBuild <= 0 )); then
>&2 echo "Tag Error: Do not set the fourth version number (build) when it's zero, use x.y.z instead (not x.y.z.0)."
fi
previousMajor=`echo $previousReleaseTag | cut -d. -f1`
previousMinor=`echo $previousReleaseTag | cut -d. -f2`
previousPatch=`echo $previousReleaseTag | cut -d. -f3`
previousBuild=`echo $previousReleaseTag | cut -d. -f4`
if [[ -z $previousBuild ]]; then
previousBuild=0
fi
echo "Current Major: $currentMajor"
echo "Current Minor: $currentMinor"
echo "Current Patch: $currentPatch"
echo "Current Build: $currentBuild"
echo "Previous Major: $previousMajor"
echo "Previous Minor: $previousMinor"
echo "Previous Patch: $previousPatch"
echo "Previous Build: $previousBuild"
latestReleaseCandidateTag="${latestReleaseCandidateTag//-rc/.}"
currentRcMajor=`echo $latestReleaseCandidateTag | cut -d. -f1`
currentRcMinor=`echo $latestReleaseCandidateTag | cut -d. -f2`
currentRcPatch=`echo $latestReleaseCandidateTag | cut -d. -f3`
currentRcBuild=`echo $latestReleaseCandidateTag | cut -d. -f4`
currentRc=`echo $latestReleaseCandidateTag | cut -d. -f5`
if [[ -z $currentRc ]]; then
currentRc=$currentRcBuild
currentRcBuild=0
elif (( currentRcBuild <= 0 )); then
>&2 echo "Tag Error: (RC) Do not set the fourth version number (build) when it's zero, use x.y.z instead (not x.y.z.0)."
fi
previousReleaseCandidateTag="${previousReleaseCandidateTag//-rc/.}"
previousRcMajor=`echo $previousReleaseCandidateTag | cut -d. -f1`
previousRcMinor=`echo $previousReleaseCandidateTag | cut -d. -f2`
previousRcPatch=`echo $previousReleaseCandidateTag | cut -d. -f3`
previousRcBuild=`echo $previousReleaseCandidateTag | cut -d. -f4`
previousRc=`echo $previousReleaseCandidateTag | cut -d. -f5`
if [[ -z $previousRc ]]; then
previousRc=$previousRcBuild
previousRcBuild=0
fi
echo "Current RC Major: $currentRcMajor"
echo "Current RC Minor: $currentRcMinor"
echo "Current RC Patch: $currentRcPatch"
echo "Current RC Build: $currentRcBuild"
echo "Current RC: $currentRc"
echo "Previous RC Major: $previousRcMajor"
echo "Previous RC Minor: $previousRcMinor"
echo "Previous RC Patch: $previousRcPatch"
echo "Previous RC Build: $previousRcBuild"
echo "Previous RC: $previousRc"
isRcGreater=false
isRcEqual=false
if (( currentMajor < currentRcMajor )); then
isRcGreater=true
else
if (( currentMinor < currentRcMinor )); then
isRcGreater=true
else
if (( currentPatch < currentRcPatch )); then
isRcGreater=true
else
if (( currentBuild < currentRcBuild )); then
isRcGreater=true
elif (( currentBuild == currentRcBuild )); then
isRcEqual=true
fi
fi
fi
fi
echo "Is RC Greater: $isRcGreater"
echo "Is RC Equal : $isRcEqual"
isPreviousRcGreater=false
if (( previousRcMajor <= currentRcMajor )); then
isPreviousRcGreater=true
else
if (( previousRcMinor <= currentRcMinor )); then
isPreviousRcGreater=true
else
if (( previousRcPatch <= currentRcPatch )); then
isPreviousRcGreater=true
else
if (( previousRcBuild < currentRcBuild )); then
isPreviousRcGreater=true
fi
fi
fi
fi
echo "Is Previous RC Greater: $isPreviousRcGreater"
if [[ $isRcGreater = true ]]; then
currentMajor=$currentRcMajor
currentMinor=$currentRcMinor
currentPatch=$currentRcPatch
currentBuild=$currentRcBuild
fi
if [[ $isRcGreater = true ]] && [[ $isPreviousRcGreater = true ]]; then
previousMajor=$previousRcMajor
previousMinor=$previousRcMinor
previousPatch=$previousRcPatch
previousBuild=$previousRcBuild
fi
echo "Current Version : $currentMajor.$currentMinor.$currentPatch.$currentBuild"
echo "Previous Version: $previousMajor.$previousMinor.$previousPatch.$previousBuild"
if (( currentMajor == previousMajor )); then
if (( currentMinor == previousMinor )); then
if (( currentPatch == previousPatch )); then
if (( currentBuild < previousBuild )); then
>&2 echo "Versioning Error: Build version is less than the previous one."
exit 1
elif [[ $currentBuild = $previousBuild ]]; then
if (( currentRc < previousRc )); then
>&2 echo "Versioning Error: Release candidate version is less than the previous one."
exit 1
elif (( currentRc != previousRc && currentRc != previousRc + 1 )); then
>&2 echo "Versioning Error: Release candidate version is increased but not incremented."
exit 1
fi
elif (( currentBuild != previousBuild + 1 )); then
>&2 echo "Versioning Error: Build version is increased but not incremented."
exit 1
fi
else
if (( currentPatch <= previousPatch )); then
>&2 echo "Versioning Error: Patch version is less than the previous one."
exit 1
elif (( currentPatch != previousPatch + 1 )); then
>&2 echo "Versioning Error: Patch version is increased but not incremented."
exit 1
fi
fi
else
if (( currentMinor <= previousMinor )); then
>&2 echo "Versioning Error: Minor version is less than the previous one."
exit 1
elif (( currentMinor != previousMinor + 1 )); then
>&2 echo "Versioning Error: Minor version is increased but not incremented."
exit 1
fi
fi
else
if (( currentMajor < previouseMajor )); then
>&2 echo "Versioning Error: Major version is increased but not incremented."
exit 1;
elif (( currentMajor != previousMajor + 1 )); then
>&2 echo "Versioning Error: Major version is increased but not incremented."
exit 1
fi
fi
echo "##vso[task.setvariable variable=activeBranch;isOutput=true]$activeBranch"
echo "##vso[task.setvariable variable=isMarketplaceRelease;isOutput=true]$isMarketplaceRelease"
echo "##vso[task.setvariable variable=isReleaseCandidate;isOutput=true]$isReleaseCandidate"
echo "##vso[task.setvariable variable=latestMajor;isOutput=true]$currentMajor"
echo "##vso[task.setvariable variable=latestMinor;isOutput=true]$currentMinor"
echo "##vso[task.setvariable variable=latestPatch;isOutput=true]$currentPatch"
echo "##vso[task.setvariable variable=latestBuild;isOutput=true]$currentBuild"
if [[ $isMarketplaceRelease = true ]]; then
echo "##vso[build.addbuildtag]marketplace-release"
fi
if [[ $isReleaseCandidate = true ]]; then
echo "##vso[build.addbuildtag]release-candidate"
fi
if [[ $isReleaseCandidate = false && $isMarketplaceRelease = false ]]; then
echo "##vso[build.addbuildtag]continious-integration"
fi