-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease.sh
More file actions
executable file
·353 lines (326 loc) · 10.3 KB
/
release.sh
File metadata and controls
executable file
·353 lines (326 loc) · 10.3 KB
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
#!/bin/bash
# Release script for the Reckon project.
USAGE="Usage: release.sh [options]";
HELP_TEXT=$(cat << EOS
Handles release versions of the Reckon project.
${USAGE}
Run this script from a development version of the master branch to create a release candidate.
The release candidate is committed and pushed to the staging branch. You can then check the
built packages and finally confirm the release by running this script again from the staging
branch, which will merge it into master and create the public release.
Options:
[--bump-version] <IDENT> Bump the project version, where IDENT is either 'M' for major,
'm' for minor or 'p' for patch. This option will result in the
project version being incremented at the corresponding level.
[-?|--help] Show this help message.
EOS
)
# Arg flags
ARG_BUMP_VERSION=false;
ARG_SHOW_HELP=false;
# Arg helper vars
arg_check_optarg=false;
arg_optarg_key="";
arg_optarg_required="";
arg_opt_arg="";
# Parse all arguments given to this script
for arg in "$@"; do
if [[ $arg_check_optarg == true ]]; then
arg_check_optarg=false;
if [[ "$arg" != -* ]]; then
arg_opt_arg="$arg";
arg_optarg_required="";
shift;
continue;
fi
fi
if [ -n "$arg_optarg_required" ]; then
echo "Missing required option argument '${arg_optarg_required}'";
exit 1;
fi
case $arg in
--bump-version)
ARG_BUMP_VERSION=true;
arg_check_optarg=true;
arg_optarg_required="IDENT";
shift
;;
-\?|--help)
ARG_SHOW_HELP=true;
shift
;;
*)
# Unknown Argument
echo "Unknown argument: '$arg'";
echo "$USAGE";
echo "";
echo "Run 'release.sh --help' for more information";
exit 1;
;;
esac
done
# Check if help is triggered
if [[ $ARG_SHOW_HELP == true ]]; then
echo "$HELP_TEXT";
exit 0;
fi
if [ -n "$arg_optarg_required" ]; then
echo "Missing required option argument '${arg_optarg_required}'";
exit 1;
fi
git_current_status=$(git status --porcelain);
if [ -n "$git_current_status" ]; then
echo "Warning: You have uncommitted or unstaged changes in your working tree.";
echo "Please first commit or discard the following changes:";
echo "$git_current_status";
exit 1;
fi
readonly FILE_VERSION="VERSION";
readonly STAGING_BRANCH="staging";
readonly FILE_CHANGELOG="CHANGELOG.md";
readonly FILE_RELEASE_NOTES=".github/release-notes.md";
VERSION="?.?.?";
VERSION_BASE="$VERSION";
IS_DEV_VERSION=false;
NEW_VERSION="";
NEW_VERSION_BASE="";
function prompt_confirmation() {
local answer="";
read -p "[INPUT] " answer;
if [ -z "$answer" ]; then
return 1;
fi
case "$answer" in
1|YES|yes|Yes|Y|y|true|True)
return 0;
;;
0|NO|no|No|N|n|false|False)
return 1;
;;
*)
echo "Invalid input";
return 1;
;;
esac
}
function read_version() {
if [ -r "$FILE_VERSION" ]; then
VERSION=$(head -n 1 "$FILE_VERSION");
VERSION_BASE="${VERSION%%-*}";
fi
local version_re="^[0-9]+\.[0-9]+\.[0-9]+(-dev)?$";
if ! [[ $VERSION =~ $version_re ]]; then
echo "Warning: Version specified in file '${FILE_VERSION}' has an invalid format. Abort.";
exit 1;
fi
IS_DEV_VERSION=false;
if [[ "$VERSION" == *-dev ]]; then
IS_DEV_VERSION=true;
fi
}
function update_version() {
local version_change_index="$1";
local version_array=( ${VERSION_BASE//./ } );
local version_major="${version_array[0]}";
local version_minor="${version_array[1]}";
local version_patch="${version_array[2]}";
if [ -n "$version_change_index" ]; then
local version_new_value=$(( ${version_array[$version_change_index]} + 1 ));
version_array[version_change_index]="$version_new_value";
while (( version_change_index < 2 )); do
((++version_change_index));
version_array[version_change_index]="0";
done
fi
local new_version="${version_array[0]}.${version_array[1]}.${version_array[2]}";
local new_version_base="$new_version";
if [[ $IS_DEV_VERSION == true ]]; then
new_version="${new_version}-dev";
fi
NEW_VERSION="$new_version";
NEW_VERSION_BASE="$new_version_base";
}
function write_version() {
echo -n "$NEW_VERSION" > "$FILE_VERSION";
sed -i -e "s/VERSION ${VERSION_BASE}/VERSION ${NEW_VERSION_BASE}/" "CMakeLists.txt" || exit 1;
sed -i -e "s/\x22${VERSION}\x22/\x22${NEW_VERSION}\x22/" "docs/Doxyfile" || exit 1;
}
function commit_release_candidate() {
read_version;
IS_DEV_VERSION=false;
update_version;
if [ -r "$FILE_CHANGELOG" ]; then
local changelog_top=$(head -n 1 "$FILE_CHANGELOG");
if [[ "$changelog_top" != "#### ${NEW_VERSION}" ]]; then
echo "Warning: No log entry found for the new version at the top of the changelog.";
echo "Missing entry for version $NEW_VERSION";
echo "Please check file '${FILE_CHANGELOG}'";
exit 1;
fi
fi
if [ -r "$FILE_RELEASE_NOTES" ]; then
local release_notes_top=$(head -n 1 "$FILE_RELEASE_NOTES");
if [[ "$release_notes_top" != "#### Release notes:" ]]; then
echo "Malformed release notes file.";
echo "Please check file '${FILE_RELEASE_NOTES}'";
exit 1;
fi
local release_notes_bottom=$(tail -n 1 "$FILE_RELEASE_NOTES");
if [[ "$release_notes_bottom" != *"/blob/v${NEW_VERSION}/CHANGELOG.md)." ]]; then
echo "Release notes file is referring to wrong version.";
echo "Please check file '${FILE_RELEASE_NOTES}'";
exit 1;
fi
fi
echo "You are about to create a release candidate for version ${NEW_VERSION}";
echo "Do you want to continue? (y/N)";
if ! prompt_confirmation; then
echo "Abort";
exit 2;
fi
echo "Creating a release candidate";
if git show-ref --verify --quiet "refs/heads/${STAGING_BRANCH}" \
|| git ls-remote --exit-code --heads origin "$STAGING_BRANCH" &> /dev/null; then
echo "Warning: Branch '${STAGING_BRANCH}' already exists.";
echo "A release candidate has already been prepared.";
echo "You have to either cancel the ongoing release process by deleting the staging branch,";
echo "or confirm the release by running this script from within the staging branch.";
exit 1;
fi
if ! git checkout -b "${STAGING_BRANCH}"; then
echo "Error: Failed to create '${STAGING_BRANCH}' branch";
exit 1;
fi
echo "Setting version of release candidate to ${NEW_VERSION}";
write_version;
echo "Committing version change";
if ! git commit -a -m "Release v${NEW_VERSION}" --no-signoff; then
echo "Error: Failed to commit changes";
exit 1;
fi
echo "Pushing staging branch";
if ! git push --set-upstream origin "${STAGING_BRANCH}" &> /dev/null; then
echo "Error: Failed to push changes";
exit 1;
fi
local workflow_label="Staging Workflow";
local workflow_url="https://github.com/raven-computing/reckon/actions/workflows/staging.yaml";
local workflow_link="\e]8;;${workflow_url}\e\\\\${workflow_label}\e]8;;\e\\\\";
echo "";
echo "The release candidate for v${NEW_VERSION} has been prepared.";
echo "The current branch is '${STAGING_BRANCH}'.";
echo "You can now perform manual checks and inspect automated packaging on GitHub.";
echo -e "Check the ${workflow_link}.";
echo "";
echo "To CANCEL the release, delete the staging branch.";
echo "To CONFIRM the release, run the release.sh again while on the staging branch.";
echo "";
}
function commit_release() {
read_version;
echo "You are about to release version ${VERSION} of Reckon";
echo "Do you confirm that this version should be released? (y/N)";
if ! prompt_confirmation; then
echo "Abort";
exit 2;
fi
echo "Committing release version";
if ! git ls-remote --exit-code --heads origin "$STAGING_BRANCH" &> /dev/null; then
echo "Error: No remote branch found for '${STAGING_BRANCH}'";
exit 1;
fi
echo "Switching to master branch";
if ! git checkout master; then
echo "Error: Failed to switch to master branch";
exit 1;
fi
echo "Syncing branch";
if ! git pull; then
echo "Error: Failed to pull from master branch";
exit 1;
fi
local release_tag="v${VERSION}";
if [[ "$(git tag -l ${release_tag})" != "" ]]; then
echo "Error: A commit tag '${release_tag}' already exists";
exit 1;
fi
echo "Merging staging branch into master";
local commit_msg="Release v${VERSION}";
if ! git merge -m "$commit_msg" --no-signoff "$STAGING_BRANCH"; then
echo "Error: Failed to merge staging branch";
exit 1;
fi
echo "Creating tag $release_tag";
if ! git tag -s -m "$commit_msg" "$release_tag"; then
echo "Error: Failed to tag release-version";
exit 1;
fi
echo "Running post-release actions";
local release_version="$VERSION";
IS_DEV_VERSION=true;
update_version;
write_version;
bump_version "p";
echo "Post-release actions completed";
echo "Pushing release";
git push && git push origin "$release_tag";
if (( $? != 0 )); then
echo "Failed to push release";
exit 1;
fi
echo "Deleting staging branch"
if ! git push origin --delete "$STAGING_BRANCH"; then
echo "Failed to delete remote staging branch";
exit 1;
fi
if ! git branch -D "$STAGING_BRANCH"; then
echo "Failed to delete local staging branch";
exit 1;
fi
if ! git fetch --prune; then
echo "Failed to sync";
exit 1;
fi
echo "";
echo "Reckon v${release_version} has been released";
echo "";
}
function bump_version() {
local ident="$1";
local version_ident_index="";
if [[ "$ident" == "M" ]]; then
version_ident_index=0;
elif [[ "$ident" == "m" ]]; then
version_ident_index=1;
elif [[ "$ident" == "p" ]]; then
version_ident_index=2;
else
echo "Error: Argument for option '--bump-version' must be one of ('M', 'm', 'p')";
exit 1;
fi
read_version;
update_version $version_ident_index;
echo "Bumping current version ${VERSION}";
write_version;
echo "The new version is ${NEW_VERSION}";
echo "Committing version change";
git add . && git commit -m "Bump version" --no-signoff;
if (( $? != 0 )); then
echo "Error: Failed to commit changes";
exit 1;
fi
}
CURRENT_BRANCH="$(git branch --show-current)";
if [[ $ARG_BUMP_VERSION == true ]]; then
bump_version "$arg_opt_arg";
exit 0;
fi
if [[ "$CURRENT_BRANCH" == "master" ]]; then
commit_release_candidate;
elif [[ "$CURRENT_BRANCH" == "staging" ]]; then
commit_release;
else
echo "Warning: Can only operate from within the 'master' or '${STAGING_BRANCH}' branch. Abort.";
exit 1;
fi
exit 0;