-
Notifications
You must be signed in to change notification settings - Fork 644
/
release.sh
executable file
·319 lines (262 loc) · 9.15 KB
/
release.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
#!/bin/bash
# ======================================
# Release script for docker-maven-plugin
# ======================================
# Exit if any error occurs
# Fail on a single failed command in a pipeline (if supported)
set -o pipefail
# Save global script args, use "build" as default
if [ -z "$1" ]; then
ARGS=("")
else
ARGS=("$@")
fi
# Fail on error and undefined vars (please don't use global vars, but evaluation of functions for return values)
set -eu
usage() {
cat - <<EOT
Release docker-maven-plugin
-n --dry-run Dry run, which performs the whole build but does no tagging, artefact
upload or pushing Docker images
--release-version <ver> Version to release (e.g. "1.2.1"). One version arg is mandatory
--snapshot-release Snapshot release which can be created on a daily basis.
--settings <file> Path to a custom settings.xml to use for the release.
This file must contain all the credentials to be used for Sonatype.
By default ~/.m2/settings.xml is used.
--local-maven-repo <dir> Local dir for holding the local Maven repo cache. If not given, then a new
temporary directory will be used (and removed after the release)
--no-git-push Don't push the release tag (and symbolic major.minor tag) at the end
--git-remote Name of the git remote to push to. If not given, its trying to be pushed
to the git remote to which the currently checked out branch is attached to.
Works only when on a branch, not when checked out directly.
--log <log-file> Write full log to <log-file>, only print progress to screen
EOT
}
# Dir where this script is located
basedir() {
# Default is current directory
local script=${BASH_SOURCE[0]}
# Resolve symbolic links
if [ -L $script ]; then
if readlink -f $script >/dev/null 2>&1; then
script=$(readlink -f $script)
elif readlink $script >/dev/null 2>&1; then
script=$(readlink $script)
elif realpath $script >/dev/null 2>&1; then
script=$(realpath $script)
else
echo "ERROR: Cannot resolve symbolic link $script"
exit 1
fi
fi
local dir=$(dirname "$script")
local full_dir=$(cd "${dir}" && pwd)
echo ${full_dir}
}
# Checks if a flag is present in the arguments.
hasflag() {
filters="$@"
for var in "${ARGS[@]}"; do
for filter in $filters; do
if [ "$var" = "$filter" ]; then
echo 'true'
return
fi
done
done
}
# Read the value of an option.
readopt() {
filters="$@"
next=false
for var in "${ARGS[@]}"; do
if $next; then
echo $var
break;
fi
for filter in $filters; do
if [[ "$var" = ${filter}* ]]; then
local value="${var//${filter}=/}"
if [ "$value" != "$var" ]; then
echo $value
return
fi
next=true
fi
done
done
}
check_error() {
local msg="$*"
if [ "${msg//ERROR/}" != "${msg}" ]; then
echo "==============================================================="
echo $msg
exit 1
fi
}
get_release_version() {
if [ $(hasflag --snapshot-release) ]; then
echo $(calc_timestamp_version)
return
fi
local release_version=$(readopt --release-version)
if [ -z "${release_version}" ]; then
echo "ERROR: Please specify --release-version"
return
fi
echo $release_version
}
calc_timestamp_version() {
# ./mvnw -N help:evaluate -Dexpression="project.version"
local pom_version=$(./mvnw -N help:evaluate -Dexpression="project.version" | grep '^[0-9]' | sed -e 's/\([0-9]*\.[0-9]*\).*/\1/')
if [ -z "${pom_version}" ]; then
echo "ERROR: Cannot extract version from pom.xml"
exit 1
fi
local patch_level=$(git tag | grep ^$pom_version | grep -v '-' | grep '[0-9]*\.[0-9]*\.' | sed -e s/${pom_version}.// | sort -n -r | head -1)
echo "${pom_version}.$((patch_level+1))-$(date '+%Y%m%d')"
}
check_git_clean() {
echo "==== Checking for clean Git Repo"
set +e
git diff-index --quiet HEAD --
local git_uncommitted=$?
set -e
if [ $git_uncommitted != 0 ]; then
echo "Untracked or changed files exist. Please run release on a clean repo"
git status
exit 1
fi
}
update_pom_versions() {
local version="$1"
local maven_opts="$2"
echo "==== Updating pom.xml versions to $version"
./mvnw ${maven_opts} versions:set -DnewVersion=$version -DprocessAllModules=true -DgenerateBackupPoms=false
}
extract_maven_opts() {
local maven_opts="-Dmaven.repo.local=$1 --batch-mode"
local settings_xml=$(readopt --settings-xml --settings)
if [ -n "${settings_xml}" ]; then
maven_opts="$maven_opts -s $settings_xml"
fi
echo $maven_opts
}
mvn_clean_install() {
local maven_opts="$1"
echo "==== Running 'mvn clean install'"
./mvnw ${maven_opts} clean install -DskipTests
}
build_and_stage_artefacts() {
local maven_opts="$1"
if [ $(hasflag --snapshot-release) ]; then
echo "==== Building locally (--no-maven-release)"
./mvnw ${maven_opts} clean install -Pflash
else
echo "==== Building and staging Maven artefacts to Sonatype"
./mvnw ${maven_opts} -Prelease clean deploy -DstagingDescription="Staging Syndesis for $(readopt --release-version)"
fi
}
drop_staging_repo() {
local maven_opts="$1"
if [ $(hasflag --snapshot-release) ]; then
return
fi
echo "==== Dropping Sonatype staging repo"
./mvnw ${maven_opts} nexus-staging:drop -Prelease -DstagingDescription="Dropping repo"
}
release_staging_repo() {
local maven_opts="$1"
if [ $(hasflag --snapshot-release) ]; then
return
fi
echo "==== Releasing Sonatype staging repo"
./mvnw ${maven_opts} -Prelease nexus-staging:release -DstagingDescription="Releasing $(readopt --release-version)"
}
git_commit_files() {
local version=$1
echo "==== Committing files to local git"
git_commit pom.xml "Update pom.xmls to $version"
}
git_tag_release() {
local release_version=${1}
echo "==== Tagging version $release_version"
git tag -f "$release_version"
}
git_push() {
local release_version=${1:-}
if [ ! $(hasflag --no-git-push) ] && [ ! $(hasflag --dry-run -n) ]; then
local remote=$(readopt --git-remote)
if [ -z "${remote}" ]; then
# Push to the remote attached to the local checkout branch
remote=$(git for-each-ref --format='%(upstream:short)' $(git symbolic-ref -q HEAD) | sed -e 's/\([^\/]*\)\/.*/\1/')
if [ -z "${remote}" ]; then
echo "ERROR: Cannot find remote repository to git push to"
exit 1
fi
fi
echo "==== Pushing to GitHub"
if [ -n "$release_version" ]; then
echo "* Pushing $release_version"
if [ $(hasflag --snapshot-release) ]; then
# Force push to allow multiple releases per day
git push -f -u $remote $release_version
else
git push -u $remote $release_version
fi
fi
fi
}
# ===================================================================================
if [ $(hasflag --help -h) ]; then
usage
exit 0
fi
cd $(basedir)
release_version=$(get_release_version)
check_error "$release_version"
# Write to logfile if requested
if [ $(readopt --log) ]; then
logfile=$(readopt --log)
touch $logfile
tail -f $logfile > >(grep ^====) &
tail_pid=$!
trap "kill $tail_pid" EXIT
exec >>$logfile 2>&1
sleep 1
fi
# Verify that there are no modified file in git repo
check_git_clean
# Temporary local repository to guarantee a clean build
local_maven_repo=$(readopt --local-maven-repo)
if [ -z "$local_maven_repo" ]; then
local_maven_repo=$(mktemp -d 2>/dev/null || mktemp -d -t 'maven_repo')
trap "echo 'Removing temp maven repo $local_maven_repo' && rm -rf $local_maven_repo" "EXIT"
fi
# Calculate common maven options
maven_opts="$(extract_maven_opts $local_maven_repo)"
check_error $maven_opts
# Set pom.xml version to the given release_version
update_pom_versions "$release_version" "$maven_opts"
# Make a clean install
mvn_clean_install "$maven_opts"
# Build and stage artefacts to Sonatype
build_and_stage_artefacts "$maven_opts"
# For a test run, we are done
if [ $(hasflag --dry-run -n) ]; then
drop_staging_repo "$maven_opts"
echo "==== Dry run finished, nothing has been committed"
echo "==== Use 'git reset --hard' to cleanup"
exit 0
fi
# ========================================================================
# Commit, tag, release, push
# --------------------------
# Git Commit all changed files
git_commit_files "$release_version"
# Tag the release version
git_tag_release "$release_version"
# Push everything (if configured)
git_push "$release_version"
# Release staging repo
release_staging_repo "$maven_opts"