-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease.sh
More file actions
executable file
·372 lines (306 loc) · 8.68 KB
/
Copy pathrelease.sh
File metadata and controls
executable file
·372 lines (306 loc) · 8.68 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env bash
set -euo pipefail
DRY_RUN=false
GITHUB_RELEASE=false
INIT_CHANGELOG=false
VERSION=""
VERSION_FILE="VERSION"
README_FILE="README.md"
CHANGELOG_FILE="CHANGELOG.md"
ORIG_VERSION_CONTENT=""
ORIG_README_CONTENT=""
# Shows usage.
show_usage() {
cat <<'USAGE'
Usage:
./release.sh [--dry-run] [--github-release] [--init-changelog] <version>
Examples:
./release.sh 0.1.2
./release.sh --dry-run 0.1.2
./release.sh --github-release 0.1.2
./release.sh --init-changelog 0.1.2
What it does:
1. Verifies git working tree is clean
2. Verifies required files exist
3. Syncs with origin/main for live releases
4. Verifies tag v<version> does not already exist
5. Updates VERSION
6. Updates README version badge when present
7. Verifies CHANGELOG.md contains the version
8. Shows a diff preview
9. Creates a release commit
10. Creates annotated tag v<version>
11. Pushes main and the new tag to origin
12. Regenerates and pushes wiki Posts-Reference (warn-only)
13. Optionally creates a GitHub Release via gh CLI
Special mode:
--init-changelog
Creates a changelog template for the requested version at the top of
CHANGELOG.md, then exits without commit/tag/push.
Safety:
- --dry-run performs local checks and file updates, shows the diff,
then rolls changes back and exits without fetch/commit/tag/push.
- If the script aborts before commit, VERSION and README.md are restored.
USAGE
}
# Handles log step.
log_step() {
printf '==> %s\n' "$1"
}
# Handles error.
error() {
printf 'ERROR: %s\n' "$1" >&2
}
# Handles rollback local changes.
rollback_local_changes() {
local rolled_back=false
if [[ -n "${ORIG_VERSION_CONTENT}" && -f "${VERSION_FILE}" ]]; then
printf '%s' "${ORIG_VERSION_CONTENT}" > "${VERSION_FILE}"
rolled_back=true
fi
if [[ -n "${ORIG_README_CONTENT}" && -f "${README_FILE}" ]]; then
printf '%s' "${ORIG_README_CONTENT}" > "${README_FILE}"
rolled_back=true
fi
if [[ "${rolled_back}" == true ]]; then
log_step "Rolled back local file changes"
fi
}
# Handles on error.
on_error() {
error "Release command failed with exit code: $?"
rollback_local_changes || true
}
trap on_error ERR
# Handles require clean tree.
require_clean_tree() {
if ! git diff --quiet || ! git diff --cached --quiet; then
error "Git working tree is not clean. Commit or stash changes first."
exit 1
fi
if [[ -n "$(git ls-files --others --exclude-standard)" ]]; then
error "Untracked files detected. Commit, remove, or ignore them first."
exit 1
fi
}
# Handles require file.
require_file() {
local file="$1"
[[ -f "$file" ]] || { error "Required file missing: $file"; exit 1; }
}
# Handles require changelog version.
require_changelog_version() {
local version="$1"
if ! grep -Eq "^\## \[${version}\]" "${CHANGELOG_FILE}"; then
error "${CHANGELOG_FILE} does not appear to contain a section for version ${version}"
exit 1
fi
}
# Handles update version file.
update_version_file() {
local version="$1"
log_step "Updating VERSION -> ${version}"
ORIG_VERSION_CONTENT="$(cat "${VERSION_FILE}")"
printf '%s\n' "${version}" > "${VERSION_FILE}"
}
# Handles update readme badge.
update_readme_badge() {
local version="$1"
ORIG_README_CONTENT="$(cat "${README_FILE}")"
if grep -Eq 'badge/version-[0-9]+\.[0-9]+\.[0-9]+' "${README_FILE}"; then
log_step "Updating README version badge -> ${version}"
perl -0pi -e "s/badge\/version-[0-9]+\.[0-9]+\.[0-9]+/badge\/version-${version}/g" "${README_FILE}"
else
log_step "Updating README version badge -> ${version}"
printf 'README version badge not found; skipping\n'
fi
}
# Handles init changelog section.
init_changelog_section() {
local version="$1"
local today tmp_file
today="$(date +%F)"
require_file "${CHANGELOG_FILE}"
if grep -Eq "^\## \[${version}\]" "${CHANGELOG_FILE}"; then
printf 'CHANGELOG already contains version %s\n' "${version}"
return 0
fi
tmp_file="$(mktemp)"
{
printf '## [%s] - %s\n\n' "${version}" "${today}"
printf '### Added\n'
printf -- '- \n\n'
printf '### Changed\n'
printf -- '- \n\n'
printf '### Fixed\n'
printf -- '- \n\n'
cat "${CHANGELOG_FILE}"
} > "${tmp_file}"
mv "${tmp_file}" "${CHANGELOG_FILE}"
printf 'Initialized CHANGELOG.md template for version %s\n' "${version}"
}
# Prints summary.
print_summary() {
local tag="v${VERSION}"
cat <<EOF_SUMMARY
Release summary
---------------
Version : ${VERSION}
Tag : ${tag}
Branch : main
Files : ${VERSION_FILE}, ${README_FILE}, ${CHANGELOG_FILE}
GitHub : $( [[ "${GITHUB_RELEASE}" == true ]] && echo enabled || echo disabled )
EOF_SUMMARY
}
# Regenerates Posts-Reference.md and pushes to the GitHub Wiki.
update_wiki_posts_ref() {
local version="$1"
local generator="${BASH_SOURCE[0]%/*}/tools/generate-wiki-posts-ref.sh"
local wiki_tmp
wiki_tmp="$(mktemp -d)"
if [[ ! -x "$generator" ]]; then
printf '[wiki] generator not found, skipping wiki update\n'
return 0
fi
log_step "Updating wiki Posts-Reference"
if ! "$generator" >/dev/null 2>&1; then
printf '[wiki] generator failed, skipping wiki push\n'
rm -rf "$wiki_tmp"
return 0
fi
local generated="$HOME/mcamner-journal.wiki/Posts-Reference.md"
if [[ ! -f "$generated" ]]; then
printf '[wiki] Posts-Reference.md not found after generation, skipping wiki push\n'
rm -rf "$wiki_tmp"
return 0
fi
if ! git clone --quiet git@github.com:MCamner/mcamner-journal.wiki.git "$wiki_tmp" 2>/dev/null; then
printf '[wiki] could not clone wiki repo, skipping wiki push\n'
rm -rf "$wiki_tmp"
return 0
fi
cp "$generated" "$wiki_tmp/Posts-Reference.md"
cd "$wiki_tmp"
git add Posts-Reference.md
if git diff --cached --quiet; then
printf '[wiki] Posts-Reference unchanged, no push needed\n'
else
git commit -m "Update Posts-Reference for v${version}"
git push --quiet
printf '[wiki] Posts-Reference pushed\n'
fi
cd - >/dev/null
rm -rf "$wiki_tmp"
}
# Handles create release commit and tag.
create_release_commit_and_tag() {
local version="$1"
local tag="v${version}"
git add "${VERSION_FILE}" "${README_FILE}" "${CHANGELOG_FILE}"
git commit -m "Prepare ${tag} release"
git tag -a "${tag}" -m "${tag}"
}
# Handles push release.
push_release() {
local version="$1"
local tag="v${version}"
git push origin main
git push origin "${tag}"
}
# Handles create github release.
create_github_release() {
local version="$1"
local tag="v${version}"
command -v gh >/dev/null 2>&1 || {
error "gh CLI is required for --github-release"
exit 1
}
gh release create "${tag}" \
--title "${tag}" \
--notes-file "${CHANGELOG_FILE}"
}
while [[ $# -gt 0 ]]; do
case "$1" in
--dry-run)
DRY_RUN=true
shift
;;
--github-release)
GITHUB_RELEASE=true
shift
;;
--init-changelog)
INIT_CHANGELOG=true
shift
;;
-h|--help)
show_usage
exit 0
;;
-*)
error "Unknown option: $1"
show_usage
exit 1
;;
*)
if [[ -n "${VERSION}" ]]; then
error "Only one version argument is allowed."
show_usage
exit 1
fi
VERSION="$1"
shift
;;
esac
done
if [[ -z "${VERSION}" ]]; then
show_usage
printf '\nRelease aborted.\n'
exit 1
fi
require_file "${VERSION_FILE}"
require_file "${README_FILE}"
require_file "${CHANGELOG_FILE}"
if [[ "${INIT_CHANGELOG}" == true ]]; then
init_changelog_section "${VERSION}"
exit 0
fi
require_clean_tree
print_summary
printf '\n'
if [[ "${DRY_RUN}" == false ]]; then
log_step "Syncing with origin/main"
git fetch origin main
git checkout main >/dev/null 2>&1 || true
git pull --ff-only origin main
fi
if git rev-parse "v${VERSION}" >/dev/null 2>&1; then
error "Tag v${VERSION} already exists locally."
exit 1
fi
if git ls-remote --tags origin | grep -q "refs/tags/v${VERSION}$"; then
error "Tag v${VERSION} already exists on origin."
exit 1
fi
update_version_file "${VERSION}"
update_readme_badge "${VERSION}"
log_step "Verifying CHANGELOG contains version ${VERSION}"
require_changelog_version "${VERSION}"
log_step "Showing diff preview"
git --no-pager diff -- "${VERSION_FILE}" "${README_FILE}" "${CHANGELOG_FILE}" || true
if [[ "${DRY_RUN}" == true ]]; then
printf '\nDry run complete. No commit, tag, or push performed.\n'
rollback_local_changes
exit 0
fi
log_step "Creating release commit and tag"
create_release_commit_and_tag "${VERSION}"
log_step "Pushing main and tag"
push_release "${VERSION}"
update_wiki_posts_ref "${VERSION}"
if [[ "${GITHUB_RELEASE}" == true ]]; then
log_step "Creating GitHub release"
create_github_release "${VERSION}"
fi
trap - ERR
printf '\nRelease completed successfully.\n'