-
Notifications
You must be signed in to change notification settings - Fork 62
/
autoRelease
executable file
·289 lines (244 loc) · 8.18 KB
/
autoRelease
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
#!/bin/bash -e
declare SCRIPT_NAME="${0##*/}"
declare SCRIPT_DIR="$(cd ${0%/*} ; pwd)"
declare START_DIR="$(pwd)"
# usage
if [ $# != 2 ] ; then
echo -e "Usage: ${0} <major|minor|update> <release_branch>"
exit 1
fi
## Make sure no changes
if ! git diff-index --quiet HEAD -- ; then
echo -e "ERROR: You have unsaved changes."
exit 1
fi
updateType="${1}"
releaseBranch="${2}"
logFile="${START_DIR}/../autoRelease.log"
echo -e "INFO: Log is at ${logFile}"
if ! [[ -d "${START_DIR}/target" ]] ; then
if ! mkdir "${START_DIR}/target/" ; then
echo -e "ERROR: Failed to create target directory"
exit 1
fi
fi
lastTag=""
lastUpdateVersion=""
initialTag=false
currentPomVersion=$(xmllint --xpath "/*[local-name() = 'project']/*[local-name() = 'version']/text()" pom.xml | tr -d '[:space:]')
if [[ "${currentPomVersion}" == "XPath set is empty" ]] ; then
currentPomVersion=$(xmllint --xpath "/*[local-name() = 'project']/*[local-name() = 'parent']/*[local-name() = 'version']/text()" pom.xml | tr -d '[:space:]')
if [[ "${currentPomVersion}" == "XPath set is empty" ]] ; then
echo -e "ERROR: Failed to parse current pom version!"
exit 1
fi
fi
echo -e "INFO: Current pom version is ${currentPomVersion}"
# find which major/minor version we are building
releaseVersion=$(echo -n "$releaseBranch" | cut -d '/' -f 2)
echo -e "INFO: releaseVersion: ${releaseVersion}"
if [[ "${releaseVersion}" == "${releaseBranch}" ]] ; then
lastTag=$(git for-each-ref --format="%(refname)" --sort=-taggerdate --count=1 refs/tags | cut -d '/' -f 3)
if [[ "${lastTag}" == "" ]] ; then
echo -e "WARN: No tag yet, using pom version ${currentPomVersion} without -SNAPSHOT"
lastTag=${currentPomVersion/-SNAPSHOT/}
initialTag=true
else
echo -e "INFO: On non-release branch ${releaseBranch}, using last tag ${lastTag}"
fi
else
expectedMajorVersion=$(echo -n "$releaseVersion" | cut -d '.' -f 1)
expectedMinorVersion=$(echo -n "$releaseVersion" | cut -d '.' -f 2)
## find last tag with same major and minor
for t in $(git tag) ; do
# ignore wrong major version
tMa=$(echo -n "$t" | cut -d '.' -f 1)
if [[ "${tMa}" != "${expectedMajorVersion}" ]] ; then
continue
fi
# ignore wrong minor version
tMi=$(echo -n "$t" | cut -d '.' -f 2)
if [[ "${tMi}" != "${expectedMinorVersion}" ]] ; then
continue
fi
tUp=$(echo -n "$t" | cut -d '.' -f 3)
if [[ "${lastUpdateVersion}" != "" ]] && [[ "$tUp" -le "${lastUpdateVersion}" ]] ; then
continue
fi
lastUpdateVersion="$tUp"
lastTag="${t}"
done
if [[ "${lastTag}" == "" ]] ; then
echo -e "WARN: There is no last tag for releaseVersion: ${releaseVersion}, thus using initial version ${releaseVersion}"
lastTag=${releaseVersion}
updateType="NONE"
else
echo -e "INFO: Previous tag: ${lastTag}"
fi
fi
## Increment updateVersion
majorVersion=$(echo -n "$lastTag" | cut -d '.' -f 1)
minorVersion=$(echo -n "$lastTag" | cut -d '.' -f 2)
updateVersion=$(echo -n "$lastTag" | cut -d '.' -f 3)
if [[ "${updateVersion}" == "" ]] ; then
updateVersion=0
fi
if [[ "${updateType}" == "major" ]] ; then
echo -e "INFO: Incrementing major version from ${majorVersion} to $((majorVersion+1)). Resetting minor and update versions."
if $initialTag ; then
majorVersion=${majorVersion}
else
majorVersion=$((majorVersion+1))
fi
minorVersion=0
updateVersion=0
elif [[ "${updateType}" == "minor" ]]; then
echo -e "INFO: Incrementing minor version from ${minorVersion} to $((minorVersion+1)). Resetting update version."
if $initialTag ; then
minorVersion=${minorVersion}
else
minorVersion=$((minorVersion+1))
fi
if [[ "${updateVersion}" == "" && "${updateType}" == "update" ]] ; then
updateVersion=0
fi
elif [[ "${updateType}" == "update" ]]; then
echo -e "INFO: Incrementing update version from ${updateVersion} to $((updateVersion+1))."
if $initialTag ; then
updateVersion=${updateVersion}
else
updateVersion=$((updateVersion+1))
fi
fi
# build new version
newVersion="${majorVersion}.${minorVersion}.${updateVersion}"
# cleanup trap
function cleanup {
echo -e "\nINFO: Cleaning up..."
git checkout "${releaseBranch}"
git checkout .
git branch -D temp
}
trap cleanup EXIT
# Confirm
echo -e "INFO: Do you want to create release/tag ${newVersion} from release branch ${releaseBranch}? [Y/n]"
read a
if [[ "${a}" != "" ]] && [[ "${a}" != "y" ]] ; then
exit 0
fi
# validate tag does not exist
echo -e "INFO: Fetching tags and branches and validating they do not exist..."
if ! git fetch --all --tags > /dev/null ; then
echo -e "ERROR: Tags and branches could not be fetched!"
exit 1
fi
if git tag | grep "${newVersion}" > /dev/null ; then
echo -e "ERROR: Tag already exists!"
exit 1
fi
# make sure gpg-agent is available and loaded
echo -e "\nINFO: Searching for gpg-agent..."
if ! command -v gpg-agent ; then
echo -e "ERROR: gpg-agent missing!"
fi
if ! gpg-agent 2>&1 | grep "running and available" ; then
echo -e "WARN: gpg-agent not running, trying to start..."
if ! gpg-agent --daemon ; then
echo -e "ERROR: Failed to initialize gpg-agent, please make sure it is up and running before continuing!"
exit 1
fi
if ! gpg-agent 2>&1 | grep "running and available" ; then
echo -e "ERROR: Failed to initialize gpg-agent, please make sure it is up and running before continuing!"
exit 1
fi
fi
# Checkout release branch
echo -e "\nINFO: Checking out release branch ${releaseBranch}"
if ! git show-ref -q "${releaseBranch}" ; then
if ! git checkout -b "${releaseBranch}" ; then
echo -e "ERROR: Failed to checkout branch ${releaseBranch}"
exit 1
fi
if ! git push origin "${releaseBranch}" ; then
echo -e "ERROR: Failed to push new release branch ${releaseBranch} to origin!"
exit 1
fi
elif ! git checkout "${releaseBranch}" ; then
echo -e "ERROR: Failed to checkout branch ${releaseBranch}"
exit 1
fi
# create temp branch
echo -e "\nINFO: Creating temp branch..."
if ! git checkout -b temp ; then
echo -e "ERROR: Failed to create temp branch!"
exit 1
fi
# set release version
echo -e "\nINFO: Setting version..."
if ! mvn versions:set -DgenerateBackupPoms=false -DnewVersion="${newVersion}" > "${logFile}" ; then
echo -e "ERROR: Failed to set new version!"
exit 1
fi
# build
echo -e "\nINFO: Doing a build with new version..."
if ! mvn clean package -DskipTests "${MVN_PROFILES}" > "${logFile}" ; then
echo -e "ERROR: Failed to build with new version!"
exit 1
fi
# commit to tag
echo -e "\nINFO: Creating tag..."
if ! git add . ; then
echo -e "ERROR: Failed to git add"
exit 1
fi
if ! git commit -m "[Project] Set new version ${newVersion}" ; then
echo -e "ERROR: Failed to git commit"
exit 1
fi
if ! git tag --sign --message "[Project] New Version ${newVersion}" "${newVersion}" ; then
echo -e "ERROR: Failed to git tag"
exit 1
fi
# local install
echo -e "\nINFO: Installing new version..."
if ! git checkout "${newVersion}" ; then
echo -e "ERROR: Failed to checkout tag ${newVersion}"
exit 1
fi
if ! mvn source:jar install -DskipTests "${MVN_PROFILES}" > /dev/null ; then
echo -e "ERROR: Failed to install new version!"
exit 1
fi
# rebase tag into release branch
if ! git checkout "${releaseBranch}" ; then
echo -e "ERROR: Failed to checkout release branch ${releaseBranch}"
fi
if ! git rebase temp ; then
echo -e "ERROR: Failed to rebase tag into release branch!"
fi
# set last POM version if it was a SNAPSHOT version
if [[ ${currentPomVersion} == *-SNAPSHOT ]] ; then
echo -e "\nINFO: Setting POM version to previous version ${currentPomVersion}..."
if ! mvn versions:set -DgenerateBackupPoms=false -DnewVersion="${currentPomVersion}" > "${logFile}" ; then
echo -e "ERROR: Failed to set new version!"
exit 1
fi
if ! git add . ; then
echo -e "ERROR: Failed to git add"
exit 1
fi
if ! git commit -m "[Project] Set release SNAPSHOT version ${newVersion}" ; then
echo -e "ERROR: Failed to git commit"
exit 1
fi
fi
# git push
echo -e "INFO: Pushing to origin..."
if ! git push origin "${releaseBranch}" --follow-tags ; then
echo -e "ERROR: Failed to push changes"
exit 1
fi
echo -e "\nINFO: Pushed Release/Tag ${newVersion}"
rm -f "${logFile}"
echo -e "\nINFO: Release/Tag ${newVersion} created."
exit 0