-
Notifications
You must be signed in to change notification settings - Fork 3
/
release.sh
executable file
·222 lines (173 loc) · 6.63 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
#!/bin/bash
#
# Release script. Replicates the functionality of mvn release but avoids all the buggy behaviour we've seen
# under mvn3 (especially with code-signing)
#
function die_with() {
echo "$*" >&2
exit 1
}
function has_command() {
which "$1" >/dev/null 2>/dev/null || return 1
return 0
}
function has_xmllint_with_xpath() {
if [ "$(xmllint 2>&1 | grep xpath | wc -l)" = "0" ] ; then
return 1
else
return 0
fi
}
function die_unless_xmllint_has_xpath() {
has_command xmllint || die_with "Missing xmllint command, please install it (from libxml2)"
has_xmllint_with_xpath || die_with "xmllint command is missing the --xpath option, please install the libxml2 version"
}
function die_without_command() {
while [ -n "$1" ]
do
has_command "$1" || die_with "Missing required command: $1"
shift
done
}
function rollback_and_die_with() {
echo "$*" >&2
echo "Resetting release commit to return you to the same working state as before attempting a deploy"
echo "> git reset --hard HEAD^1"
git reset --hard HEAD^1 || echo "Git reset command failed!"
exit 1
}
function usage() {
echo "Maven git release script v1.0 (c) 2014 Peter Wright"
echo ""
echo "Usage:"
echo " $0 [-a | [ -r RELEASE_VERSION ] [ -n NEXT_DEV_VERSION ] ] [ -c ASSUMED_POM_VERSION ] [ -s ]"
echo "Updates release version, then builds and commits it"
echo ""
echo " -a Shorthand for -a auto -n auto"
echo " -r Sets the release version number to use ('auto' to use the version in pom.xml)"
echo " -n Sets the next development version number to use (or 'auto' to increment release version)"
echo " -c Assume this as pom.xml version without inspecting it with xmllint"
echo ""
echo " -h For this message"
echo ""
}
###############################
# HANDLE COMMAND-LINE OPTIONS #
###############################
while getopts "ahr:n:c:" o; do
case "${o}" in
a)
RELEASE_VERSION="auto"
NEXT_VERSION="auto"
;;
r)
RELEASE_VERSION="${OPTARG}"
;;
n)
NEXT_VERSION="${OPTARG}"
;;
c)
CURRENT_VERSION="${OPTARG}"
;;
h)
usage
exit 0
;;
*)
usage
die_with "Unrecognised option ${o}"
;;
esac
done
shift $((OPTIND-1))
###############################################
# MAKE SURE SCRIPT DEPENDENCIES ARE INSTALLED #
###############################################
die_without_command git perl wc
if [ -z "$MVN" ] ; then
die_without_command mvn
MVN=mvn
else
die_without_command $MVN
fi
echo "Using maven command: $MVN"
#########################################
# BAIL IF THERE ARE UNCOMMITTED CHANGES #
#########################################
# If there are any uncommitted changes we must abort immediately
if [ $(git status -s | wc -l) != "0" ] ; then
git status -s
die_with "There are uncommitted changes, please commit or stash them to continue with the release:"
else
echo "Good, no uncommitted changes found"
fi
#################################################################
# FIGURE OUT RELEASE VERSION NUMBER AND NEXT DEV VERSION NUMBER #
#################################################################
if [ -z "$CURRENT_VERSION" ] ; then
# Extract the current version (requires xmlllint with xpath suport)
die_unless_xmllint_has_xpath
CURRENT_VERSION=$(xmllint --xpath "/*[local-name() = 'project']/*[local-name() = 'version']/text()" pom.xml)
fi
echo "Current pom.xml version: $CURRENT_VERSION"
echo ""
# Prompt for release version (or compute it automatically if requested)
RELEASE_VERSION_DEFAULT=$(echo "$CURRENT_VERSION" | perl -pe 's/-SNAPSHOT//')
if [ -z "$RELEASE_VERSION" ] ; then
read -p "Version to release [${RELEASE_VERSION_DEFAULT}] " RELEASE_VERSION
if [ -z "$RELEASE_VERSION" ] ; then
RELEASE_VERSION=$RELEASE_VERSION_DEFAULT
fi
elif [ "$RELEASE_VERSION" = "auto" ] ; then
RELEASE_VERSION=$RELEASE_VERSION_DEFAULT
fi
if [ "$RELEASE_VERSION" = "$CURRENT_VERSION" ] ; then
die_with "Release version requested is exactly the same as the current pom.xml version (${CURRENT_VERSION})! Is the version in pom.xml definitely a -SNAPSHOT version?"
fi
# Prompt for next version (or compute it automatically if requested)
NEXT_VERSION_DEFAULT=$(echo "$RELEASE_VERSION" | perl -pe 's{^(([0-9]+\.)+)?([0-9]+)$}{$1 . ($3 + 1)}e')
if [ -z "$NEXT_VERSION" ] ; then
read -p "Next snapshot version [${NEXT_VERSION_DEFAULT}] " NEXT_VERSION
if [ -z "$NEXT_VERSION" ] ; then
NEXT_VERSION=$NEXT_VERSION_DEFAULT
fi
elif [ "$NEXT_VERSION" = "auto" ] ; then
NEXT_VERSION=$NEXT_VERSION_DEFAULT
fi
# Add -SNAPSHOT to the end (and make sure we don't accidentally have it twice)
NEXT_VERSION="$(echo "$NEXT_VERSION" | perl -pe 's/-SNAPSHOT//gi')-SNAPSHOT"
if [ "$NEXT_VERSION" = "${RELEASE_VERSION}-SNAPSHOT" ] ; then
die_with "Release version and next version are the same version!"
fi
echo ""
echo "Using $RELEASE_VERSION for release"
echo "Using $NEXT_VERSION for next development version"
#############################
# START THE RELEASE PROCESS #
#############################
VCS_RELEASE_TAG="v${RELEASE_VERSION}"
# if a release tag of this version already exists then abort immediately
if [ $(git tag -l "${VCS_RELEASE_TAG}" | wc -l) != "0" ] ; then
die_with "A tag already exists ${VCS_RELEASE_TAG} for the release version ${RELEASE_VERSION}"
fi
# Update the pom.xml versions
$MVN versions:set -DgenerateBackupPoms=false "-DnewVersion=$RELEASE_VERSION" || die_with "Failed to set release version on pom.xml files"
# Commit the updated pom.xml files
git commit -a -m "Release version ${RELEASE_VERSION}" || die_with "Failed to commit updated pom.xml versions for release!"
echo ""
echo " Starting build and deploy"
echo ""
# build and deploy the release
$MVN -DperformRelease=true -Pstdlib-release clean deploy -pl "!user-manager/service" || rollback_and_die_with "Build/Deploy failure. Release failed."
# tag the release (N.B. should this be before perform the release?)
git tag "v${RELEASE_VERSION}" || die_with "Failed to create tag ${RELEASE_VERSION}! Release has been deployed, however"
# Clean up anything Maven has left in the working tree (occasionally seeing folders with \020 in their name being created)
git reset --hard
git clean -fd
######################################
# START THE NEXT DEVELOPMENT PROCESS #
######################################
$MVN versions:set -DgenerateBackupPoms=false "-DnewVersion=${NEXT_VERSION}" || die_with "Failed to set next dev version on pom.xml files, please do this manually"
git commit -a -m "Start next development version ${NEXT_VERSION}" || die_with "Failed to commit updated pom.xml versions for next dev version! Please do this manually"
git push || die_with "Failed to push commits. Please do this manually"
git push --tags || die_with "Failed to push tags. Please do this manually"