-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-build-phase-1.sh
241 lines (198 loc) · 9.71 KB
/
pre-build-phase-1.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
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:]]\+$"
releaseCandidateGrepRegex="^[[: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`
previousMajor=`echo $previousReleaseTag | cut -d. -f1`
previousMinor=`echo $previousReleaseTag | cut -d. -f2`
previousPatch=`echo $previousReleaseTag | cut -d. -f3`
echo "Current Major: $currentMajor"
echo "Current Minor: $currentMinor"
echo "Current Patch: $currentPatch"
echo "Previous Major: $previousMajor"
echo "Previous Minor: $previousMinor"
echo "Previous Patch: $previousPatch"
latestReleaseCandidateTag="${latestReleaseCandidateTag//-rc/.}"
currentRcMajor=`echo $latestReleaseCandidateTag | cut -d. -f1`
currentRcMinor=`echo $latestReleaseCandidateTag | cut -d. -f2`
currentRcPatch=`echo $latestReleaseCandidateTag | cut -d. -f3`
currentRc=`echo $latestReleaseCandidateTag | cut -d. -f4`
previousReleaseCandidateTag="${previousReleaseCandidateTag//-rc/.}"
previousRcMajor=`echo $previousReleaseCandidateTag | cut -d. -f1`
previousRcMinor=`echo $previousReleaseCandidateTag | cut -d. -f2`
previousRcPatch=`echo $previousReleaseCandidateTag | cut -d. -f3`
previousRc=`echo $previousReleaseCandidateTag | cut -d. -f4`
echo "Current RC Major: $currentMajor"
echo "Current RC Minor: $currentMinor"
echo "Current RC Patch: $currentPatch"
echo "Previous RC Major: $previousMajor"
echo "Previous RC Minor: $previousMinor"
echo "Previous RC Patch: $previousPatch"
isRcGreater=false
isRcEqual=false
if (( currentMajor <= currentRcMajor )); then
isRcGreater=true
else
if (( currentMinor <= currentRcMinor )); then
isRcGreater=true
else
if (( currentPatch < currentRcPatch )); then
isRcGreater=true
elif (( currentPatch == currentRcPatch )); then
isRcEqual=true
fi
fi
fi
echo "Is RC Greater: $isRcGreater"
echo "Is RC Equal : $isRcEqual"
isPreviousRcGreater=false
if (( previousMajor <= currentRcMajor )); then
isPreviousRcGreater=true
else
if (( previousMinor <= currentRcMinor )); then
isPreviousRcGreater=true
else
if (( previousPatch < currentRcPatch )); then
isPreviousRcGreater=true
fi
fi
fi
echo "Is Previous RC Greater: $isPreviousRcGreater"
if [[ $isRcGreater = true ]]; then
currentMajor=$currentRcMajor
currentMinor=$currentRcMinor
currentPatch=$currentRcPatch
fi
if [[ $isPreviousRcGreater = true ]]; then
previousMajor=$previousRcMajor
previousMinor=$previousRcMinor
previousPatch=$previousRcPatch
fi
echo "Current Version : $currentMajor.$currentMinor.$currentPatch"
echo "Previous Version: $previousMajor.$previousMinor.$previousPatch"
if (( currentMajor == previousMajor )); then
if (( currentMinor == previousMinor )); then
if (( currentPatch < previousPatch )); then
>&2 echo "Versioning Error: Patch version is less than the previous one."
exit 1
elif [[ $currentPatch = $previousPatch ]]; 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 (( currentPatch != previousPatch + 1 )); then
>&2 echo "Versioning Error: Patch version is increased but not incremented."
exit 1
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"
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