-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease-nodejs
executable file
·373 lines (301 loc) · 11.2 KB
/
release-nodejs
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/bin/sh
# MIT License
#
# Copyright (c) 2018 SciSpike, LLC
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# This script implements the release branch workflow for node.js projects.
#
# Requirements:
# git
# docker (unless ymlx & match are available on the path as reported by `which`)
if [ -n "$RELEASE_DEBUG" ]; then
set -x
fi
ORIGIN=${ORIGIN:-origin}
MASTER=${MASTER:-master}
TAG_PREFIX=${TAG_PREFIX:-''}
TAG_SUFFIX=${TAG_SUFFIX:-''}
BRANCH_PREFIX=${BRANCH_PREFIX:-'v'}
BRANCH_SUFFIX=${BRANCH_SUFFIX:-''} # '.x' is a common one
if [ -z "$NO_GIT_DISCOVERY_ACROSS_FILESYSTEM" ]; then
GIT_DISCOVERY_ACROSS_FILESYSTEM=1 # needed when running in a docker container
fi
# support custom "pre" tokens
PRE=${PRE:-pre}
PRE_USAGE=pre
if [ "$PRE" != "pre" ]; then
PRE_USAGE="$PRE_USAGE|$PRE"
fi
# support custom "rc" tokens
RC=${RC:-rc}
RC_USAGE=rc
if [ "$RC" != "rc" ]; then
RC_USAGE="$RC_USAGE|$RC"
fi
if [ "$PRE" == "$RC" ]; then
echo "ERROR: PRE ($PRE) cannot be the same as RC ($RC)" >&2
exit 1
fi
usage() {
cat<<EOF
usage:
if on $MASTER branch: release $PRE_USAGE|$RC_USAGE
if on release branch: release major|minor|patch|$PRE_USAGE
optional supported environment variables:
variable description
-------- -----------
ORIGIN name of git remote, default 'origin'
MASTER name of master branch, default 'master'
TAG_PREFIX prefix for tags, default ''
TAG_SUFFIX suffix for tags, default ''
BRANCH_PREFIX prefix for release branches, default 'v'
BRANCH_SUFFIX suffix for release branches, default '' ('.x' is common)
NO_GIT_DISCOVERY_ACROSS_FILESYSTEM whether to not set GIT_DISCOVERY_ACROSS_FILESYSTEMS, default ''
PACKAGE_JSON name of package.json file
EOF
}
RELEASE_LEVEL="$1"
case "$RELEASE_LEVEL" in
major|minor|patch|pre|rc|$PRE|$RC)
# ok
;;
h|he|hel|help)
usage
exit 0
;;
*)
if [ -n "$1" ]; then
echo "ERROR: specify release level of 'pre' or '$PRE' for prerelease, 'rc' or '$RC' for release candidate, 'patch', 'minor', or 'major'" >&2
fi
usage
exit 1
;;
esac
FX=fx
if [ -n "$NO_USE_LOCAL_FX" ] || ! which $FX; then
FX="docker run --rm -i matthewadams12/fx fx"
fi
MATCH=match
if [ -n "$NO_USE_LOCAL_MATCH" ] || ! which $MATCH; then
MATCH="docker run --rm -i matthewadams12/match"
fi
PACKAGE_JSON="${PACKAGE_JSON:-package.json}"
echo "INFO: using package file: $PACKAGE_JSON"
getVersion() {
cat "$PACKAGE_JSON" | $FX this.version | sed 's/"//g'
}
# usage: setVersion version
setVersion() {
V=$1
PACKAGE_JSON_CONTENT="$(cat $PACKAGE_JSON | $FX "it => { it.version = \"$V\"; return it; }")"
echo "$PACKAGE_JSON_CONTENT" | sed 's|\\|\\\\|g' > $PACKAGE_JSON
echo "INFO: set 'version' attribute in $PACKAGE_JSON to $V"
echo "$PACKAGE_JSON is now:"
cat "$PACKAGE_JSON"
echo "INFO: running npm install to update package-lock.json"
if [ -f /.dockerenv ]; then
NODE_MODULES_WORKDIR=/tmp/node-work
mkdir -p $NODE_MODULES_WORKDIR
PREFIX_ARG="--prefix $NODE_MODULES_WORKDIR"
fi
npm install $PREFIX_ARG
}
echo "INFO: checking required preconditions"
git pull
if ! git diff --exit-code --no-patch; then
echo 'ERROR: you have modified tracked files; only release from clean directories!' >&2
exit 3
else
echo 'INFO: no modified tracked files'
fi
if ! git diff --cached --exit-code --no-patch; then
echo 'ERROR: you have cached modified tracked files; only release from clean directories!' >&2
exit 3
else
echo 'INFO: no cached modified tracked files'
fi
if [ -n "$(git status -s)" ]; then
echo 'ERROR: You have unignored untracked files; only release from clean directories!' >&2
exit 3
else
echo 'INFO: no unignored untracked files'
fi
BRANCH="$(git status | head -n 1 | awk '{ print $3 }')"
if ! $MATCH "^(master|$BRANCH_PREFIX[0-9]{1,}\.[0-9]{1,}$BRANCH_SUFFIX)$" "$BRANCH"; then # it is not a master or a release branch
echo 'ERROR: you can only release from the master branch or release branches!' >&2
exit 3
else
echo "INFO: on branch $BRANCH, from which releases are allowed"
fi
if ! git diff --exit-code -no-patch $BRANCH $ORIGIN/$BRANCH; then
echo "ERROR: Local branch $BRANCH differs from remote branch $ORIGIN/$BRANCH" >&2
exit 3
else
echo "INFO: no differences between local & remote branch $BRANCH"
fi
if [ "$BRANCH" = "$MASTER" ]; then
case "$RELEASE_LEVEL" in
pre|rc|$PRE|$RC)
# ok
;;
*)
echo "ERROR: only 'pre'/'$PRE' or 'rc'/'$RC' releases are permitted from the $MASTER branch." >&2
exit 6
;;
esac
else # this is a release branch
case "$RELEASE_LEVEL" in
rc|patch|minor|major)
# ok
;;
*)
echo "ERROR: only 'rc'/'$RC', 'patch', 'minor', or 'major' releases are permitted from a release branch." >&2
exit 7
;;
esac
fi
echo "INFO: ok to proceed with $RELEASE_LEVEL from branch $BRANCH"
echo "INFO: determining current version from $PACKAGE_JSON"
VERSION="$(getVersion)"
if ! $MATCH "\-($PRE|$RC)\.[0-9]{1,}$" "$VERSION"; then
echo "ERROR: repository is in an inconsistent state: version does NOT end in prerelease suffix $PRE!" >&2
exit 3
fi
echo "INFO: current version is $VERSION"
# usage: apply message [tag [remote branch]]
applyChanges() {
git add .
git commit --allow-empty -m "$1"
echo "INFO: committed changes with message: $1"
MSG="INFO: pushed commits"
if [ -n "$2" ]; then
tag="$TAG_PREFIX$2$TAG_SUFFIX"
git tag "$tag"
echo "INFO: tagged $tag"
MSG="$MSG & tags"
fi
SET_UPSTREAM_ARGS=
if [ -n "$3" ] && [ -n "$4" ]; then
SET_UPSTREAM_ARGS="-u $3 $4"
MSG="$MSG & set tracked upstream to $3/$4"
fi
git push $SET_UPSTREAM_ARGS
git push --tags
echo "$MSG"
}
if [ "$BRANCH" = "$MASTER" ]; then # this will be either an rc, resulting in a new release branch, or a pre
MATCHES="$($MATCH "^([0-9]{1,})\.([0-9]{1,})\.0\-$PRE\.([0-9]{1,})$" "$VERSION")"
if [ -z "$MATCHES" ]; then
echo "ERROR: the version does not match the format of major.minor.0-$PRE.n required in the $MASTER branch." >&2
exit 8
else
echo "INFO: version $VERSION matches expected format for branch $BRANCH"
fi
# create release branch
MAJOR="$(echo "$MATCHES" | awk '{ print $2 }')"
MINOR="$(echo "$MATCHES" | awk '{ print $3 }')"
PATCH=0
PRERELEASE="$(echo "$MATCHES" | awk '{ print $4 }')"
case "$RELEASE_LEVEL" in
rc|$RC) # then it's time to create a new release branch
NEW_RELEASE_BRANCH="$BRANCH_PREFIX$MAJOR.$MINOR$BRANCH_SUFFIX"
git checkout -b $NEW_RELEASE_BRANCH
NEW_RELEASE_BRANCH_VERSION="$MAJOR.$MINOR.0-$RC.0"
setVersion $NEW_RELEASE_BRANCH_VERSION
applyChanges "release $NEW_RELEASE_BRANCH_VERSION" $NEW_RELEASE_BRANCH_VERSION $ORIGIN $NEW_RELEASE_BRANCH
echo "INFO: created release branch $NEW_RELEASE_BRANCH and tagged $NEW_RELEASE_BRANCH_VERSION for release"
# return to master branch
git checkout $MASTER
echo "INFO: checked out $MASTER"
git cherry-pick $NEW_RELEASE_BRANCH # cherry pick from release branch to get release candidate commit in master
echo "INFO: cherry-picked $NEW_RELEASE_BRANCH $RC commit into $MASTER"
# advance master version
NEXT_VERSION="$MAJOR.$(($MINOR+1)).0-$PRE.0"
setVersion $NEXT_VERSION $DOCKER_BUILD_STEP_NAMES
applyChanges "bump to $NEXT_VERSION [skip ci]"
# return to release branch & prepare for next prerelease
git checkout $NEW_RELEASE_BRANCH
echo "INFO: checked out $NEW_RELEASE_BRANCH"
NEXT_RELEASE_BRANCH_VERSION="$MAJOR.$MINOR.0-$RC.1"
setVersion $NEXT_RELEASE_BRANCH_VERSION $DOCKER_BUILD_STEP_NAMES
applyChanges "bump to $NEXT_RELEASE_BRANCH_VERSION [skip ci]"
exit 0
;;
pre|$PRE)
setVersion $VERSION
applyChanges "release $VERSION" $VERSION
NEXT_VERSION=$MAJOR.$MINOR.$PATCH-$PRE.$((PRERELEASE+1))
setVersion $NEXT_VERSION
applyChanges "bump to $NEXT_VERSION [skip ci]"
exit 0
;;
esac
fi
# If we get this far, we are releasing something from a release branch.
MATCHES="$($MATCH "^([0-9]{1,})\.([0-9]{1,})\.([0-9]{1,})\-$RC\.([0-9]{1,})$" "$VERSION")"
if [ -z "$MATCHES" ]; then
echo "ERROR: the version does not match the format of major.minor.patch-$RC.n required in the release branch." >&2
exit 8
else
echo "INFO: version $VERSION matches expected format for branch $BRANCH"
fi
MAJOR="$(echo "$MATCHES" | awk '{ print $2 }')"
MINOR="$(echo "$MATCHES" | awk '{ print $3 }')"
PATCH="$(echo "$MATCHES" | awk '{ print $4 }')"
PRERELEASE="$(echo "$MATCHES" | awk '{ print $5 }')"
case "$RELEASE_LEVEL" in
major|minor|patch)
# NOTE: if RELEASE_LEVEL is 'minor' & we're prepped for a major release, no harm, no foul.
# A major release is the same as a minor release, only that the minor version is 0.
if [ $RELEASE_LEVEL = major ] && [ $MINOR != 0 ]; then
echo "ERROR: this branch is not prepared for a major release because the minor version is $MINOR, not 0." >&2
exit 10
else
NEXT_RELEASE_BRANCH_VERSION="$MAJOR.$MINOR.1-$RC.0"
fi
if [ $RELEASE_LEVEL = minor ] && [ $PATCH != 0 ]; then
echo "ERROR: a minor release has already been performed in this release branch; only patch releases are allowed here now." >&2
exit 11
else
NEXT_RELEASE_BRANCH_VERSION="$MAJOR.$MINOR.1-$RC.0"
fi
if [ $RELEASE_LEVEL = patch ] && [ $PATCH = 0 ]; then
echo "ERROR: you must release a minor release before releasing a patch in this release branch." >&2
exit 12
else
NEXT_RELEASE_BRANCH_VERSION="$MAJOR.$MINOR.$((PATCH+1))-$RC.0"
fi
echo "INFO: $RELEASE_LEVEL ok in branch $BRANCH"
RELEASE_VERSION="$MAJOR.$MINOR.$PATCH"
setVersion $RELEASE_VERSION
applyChanges "release $RELEASE_VERSION" $RELEASE_VERSION
setVersion $NEXT_RELEASE_BRANCH_VERSION
applyChanges "bump to $NEXT_RELEASE_BRANCH_VERSION [skip ci]"
exit 0
;;
rc|$RC)
setVersion $VERSION
applyChanges "release $VERSION" $VERSION
NEXT_RELEASE_BRANCH_VERSION="$MAJOR.$MINOR.$PATCH-$RC.$((PRERELEASE+1))"
setVersion $NEXT_RELEASE_BRANCH_VERSION
applyChanges "bump to $NEXT_RELEASE_BRANCH_VERSION [skip ci]"
exit 0
;;
esac