-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease.sh
More file actions
executable file
·434 lines (376 loc) · 17.1 KB
/
Copy pathrelease.sh
File metadata and controls
executable file
·434 lines (376 loc) · 17.1 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/bin/bash
# =============================================================================
# RDA Message Board - Release Automation Script
# =============================================================================
# Features:
# - Reads version from platformio.ini (single source of truth)
# - Generates a categorized CHANGELOG.md from git commits
# - Creates an annotated tag with rich commit summary
# - Pushes to both Forgejo (origin) and GitHub (github) remotes
# - Creates a GitHub release via API (requires GITHUB_TOKEN env var)
# - Supports --dry-run, --force, --no-changelog flags
#
# Commit categories (prefix your commits with these for best results):
# feat:, fix:, improve:, docs:, chore:, build:, ci:, refactor:, style:
# Breaking changes: breaking: or BREAKING CHANGE in message
#
# Usage:
# ./release.sh [commit_message] [--force] [--dry-run] [--no-changelog]
#
# GitHub Release creation (optional):
# Export GITHUB_TOKEN before running:
# export GITHUB_TOKEN="your_personal_access_token"
# =============================================================================
set -e
# -----------------------------------------------------------------------------
# Colors
# -----------------------------------------------------------------------------
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
print_status() { echo -e "${BLUE}[INFO]${NC} $1"; }
print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
print_error() { echo -e "${RED}[ERROR]${NC} $1"; }
print_header() { echo -e "${CYAN}$1${NC}"; }
# -----------------------------------------------------------------------------
# Configuration
# -----------------------------------------------------------------------------
REMOTES=("origin" "github") # Remotes to push to
GITHUB_REPO="rdeangel/rda_msg_board" # GitHub user/repo for links and releases
CHANGELOG_FILE="CHANGELOG.md"
# -----------------------------------------------------------------------------
# Argument parsing
# -----------------------------------------------------------------------------
FORCE=false
DRY_RUN=false
NO_CHANGELOG=false
COMMIT_MESSAGE=""
while [[ $# -gt 0 ]]; do
case $1 in
--force|-f)
FORCE=true
shift ;;
--dry-run|-n)
DRY_RUN=true
shift ;;
--no-changelog)
NO_CHANGELOG=true
shift ;;
--help|-h)
echo "Usage: $0 [commit_message] [options]"
echo ""
echo "Options:"
echo " --force, -f Recreate existing tag and trigger a new release"
echo " --dry-run, -n Show what would happen without making changes"
echo " --no-changelog Skip CHANGELOG.md generation"
echo " --help, -h Show this help"
echo ""
echo "Environment variables:"
echo " GITHUB_TOKEN GitHub personal access token for release creation"
echo ""
echo "Examples:"
echo " ./release.sh 'Add sleep mode feature'"
echo " ./release.sh --dry-run"
echo " ./release.sh 'Hotfix' --force"
exit 0 ;;
*)
COMMIT_MESSAGE="$1"
shift ;;
esac
done
# -----------------------------------------------------------------------------
# Helpers
# -----------------------------------------------------------------------------
# Push main branch + optional tag to all configured remotes
push_all() {
local tag="$1"
for remote in "${REMOTES[@]}"; do
if git remote | grep -q "^${remote}$"; then
print_status "Pushing to ${remote}..."
git push "$remote" main
if [ -n "$tag" ]; then
git push "$remote" "$tag"
fi
print_success " ✅ ${remote} done"
else
print_warning "Remote '${remote}' not found, skipping."
fi
done
}
# Categorize commits and return a markdown block
# $1 = commit range, $2 = github_repo (for links, optional)
generate_release_notes() {
local range="$1"
local repo="$2"
# Collect commits as "hash|subject"
local all_commits
all_commits=$(git log --format="%h|%s" "$range" 2>/dev/null || true)
if [ -z "$all_commits" ]; then
echo "_No commits found in range._"
return
fi
# --- Categorize in priority order ---
local breaking security features fixes improvements docs style tests chore build deps refactor removals remaining uncategorized
local opt_scope="(\([^)]*\))?:"
breaking=$( echo "$all_commits" | grep -iE "\|breaking${opt_scope}|\|BREAKING CHANGE|\|.*!:" || true)
security=$( echo "$all_commits" | grep -iE "\|(security|sec)${opt_scope}|\|vulnerability|\|CVE" || true)
remaining=$( echo "$all_commits" | grep -ivE "\|breaking${opt_scope}|\|BREAKING CHANGE|\|.*!:|\|(security|sec)${opt_scope}|\|vulnerability|\|CVE" || true)
features=$( echo "$remaining" | grep -iE "\|(feat|add)${opt_scope}" || true)
fixes=$( echo "$remaining" | grep -iE "\|(fix|bug)${opt_scope}" || true)
remaining=$( echo "$remaining" | grep -ivE "\|(feat|add|fix|bug)${opt_scope}" || true)
docs=$( echo "$remaining" | grep -iE "\|(docs|documentation|readme)${opt_scope}" || true)
style=$( echo "$remaining" | grep -iE "\|(style|format|lint)${opt_scope}" || true)
tests=$( echo "$remaining" | grep -iE "\|(test|tests|spec)${opt_scope}" || true)
chore=$( echo "$remaining" | grep -iE "\|(chore|maintenance)${opt_scope}" || true)
build=$( echo "$remaining" | grep -iE "\|(build|ci|deploy)${opt_scope}" || true)
deps=$( echo "$remaining" | grep -iE "\|(deps|dependencies|package)${opt_scope}" || true)
refactor=$( echo "$remaining" | grep -iE "\|(refactor|restructure)${opt_scope}" || true)
removals=$( echo "$remaining" | grep -iE "\|(remove|delete|clean)${opt_scope}" || true)
remaining=$( echo "$remaining" | grep -ivE "\|(docs|documentation|readme|style|format|lint|test|tests|spec|chore|maintenance|build|ci|deploy|deps|dependencies|package|refactor|restructure|remove|delete|clean)${opt_scope}" || true)
improvements=$(echo "$remaining" | grep -iE "improve|enhance|update|refactor|optimize|debug" || true)
uncategorized=$(echo "$remaining" | grep -ivE "improve|enhance|update|refactor|optimize|debug" || true)
# Strip meta commits
uncategorized=$(echo "$uncategorized" | grep -ivE "\|Release v|\|chore: prepare release|\|chore: update CHANGELOG" || true)
# Write each section to a temp file (avoids nested-function scope / subshell issues)
local tmp
tmp=$(mktemp)
_append_section() {
local icon="$1" title="$2" commits_data="$3" limit="${4:-10}"
[ -z "$commits_data" ] && return
printf "\n### %s %s\n" "$icon" "$title" >> "$tmp"
echo "$commits_data" | head -"$limit" | while IFS='|' read -r hash msg; do
if [ -n "$repo" ]; then
printf -- "- %s - [%s](https://github.com/%s/commit/%s)\n" \
"$msg" "$hash" "$repo" "$hash" >> "$tmp"
else
printf -- "- %s (%s)\n" "$msg" "$hash" >> "$tmp"
fi
done
}
_append_section "⚠️" "Breaking Changes" "$breaking" 5
_append_section "🔒" "Security" "$security" 5
_append_section "✨" "New Features" "$features" 10
_append_section "🐛" "Bug Fixes" "$fixes" 10
_append_section "🚀" "Improvements" "$improvements" 10
_append_section "📚" "Documentation" "$docs" 5
_append_section "♻️" "Refactor" "$refactor" 5
_append_section "🏗️" "Build / CI" "$build" 5
_append_section "📦" "Dependencies" "$deps" 5
_append_section "🎨" "Style" "$style" 5
_append_section "🧪" "Tests" "$tests" 5
_append_section "🗑️" "Removals" "$removals" 5
_append_section "📝" "Chore" "$chore" 5
_append_section "🔀" "Other" "$uncategorized" 10
cat "$tmp"
rm -f "$tmp"
}
# Update or create CHANGELOG.md
update_changelog() {
local version="$1"
local range="$2"
local date_str
date_str=$(date +%Y-%m-%d)
print_status "Updating ${CHANGELOG_FILE}..."
# Create empty changelog if missing
if [ ! -f "$CHANGELOG_FILE" ]; then
cat > "$CHANGELOG_FILE" << 'EOF'
# Changelog
All notable changes to this project will be documented in this file.
<!-- releases -->
EOF
print_status "Created ${CHANGELOG_FILE}"
fi
# Skip if this version is already in the changelog (avoid duplicates on --force reruns)
if grep -q "^## \[${version}\]" "$CHANGELOG_FILE" 2>/dev/null; then
print_status "CHANGELOG.md already has entry for ${version} — skipping"
return
fi
# Generate the new section
local notes_tmp
notes_tmp=$(mktemp)
printf "## [%s] - %s\n" "$version" "$date_str" >> "$notes_tmp"
generate_release_notes "$range" "$GITHUB_REPO" >> "$notes_tmp"
printf "\n" >> "$notes_tmp"
local new_section
new_section=$(cat "$notes_tmp")
rm -f "$notes_tmp"
# Insert after the <!-- releases --> marker (or before first ## heading)
local tmp
tmp=$(mktemp)
if grep -q "<!-- releases -->" "$CHANGELOG_FILE"; then
awk -v section="$new_section" \
'/<!-- releases -->/ { print; print ""; print section; next } { print }' \
"$CHANGELOG_FILE" > "$tmp"
else
awk -v section="$new_section" \
'inserted==0 && /^## / { print section; print ""; inserted=1 } { print }' \
"$CHANGELOG_FILE" > "$tmp"
fi
mv "$tmp" "$CHANGELOG_FILE"
print_success "Updated ${CHANGELOG_FILE} for ${version}"
}
# GitHub release creation is handled automatically by GitHub Actions
# when a tag is pushed. No token management needed locally.
# =============================================================================
# MAIN
# =============================================================================
print_header "============================================"
print_header " RDA Message Board Release Automation"
print_header "============================================"
[ "$DRY_RUN" = true ] && print_warning "DRY RUN MODE — no changes will be made"
echo ""
# --- Navigate to repo root ---
REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || echo "")"
[ -z "$REPO_ROOT" ] && { print_error "Not in a git repository"; exit 1; }
cd "$REPO_ROOT"
# --- Read version ---
VERSION=$(grep -E "^version\s*=" platformio.ini | sed 's/.*=\s*//' | tr -d ' \r')
if [ -z "$VERSION" ]; then
print_error "Could not extract version from platformio.ini"
exit 1
fi
print_status "Version from platformio.ini: ${VERSION}"
if [ -z "$COMMIT_MESSAGE" ]; then
FULL_COMMIT_MSG="Release ${VERSION}"
else
FULL_COMMIT_MSG="Release ${VERSION} - ${COMMIT_MESSAGE}"
fi
# --- Determine commit range ---
# Use git tag --sort=-version:refname to reliably find the previous tag regardless
# of whether HEAD is on a tag boundary. git describe --tags is fragile when HEAD is
# exactly on a release commit or when tags are not on the direct ancestry path.
LAST_TAG=$(git tag --sort=-version:refname 2>/dev/null | grep -v "^${VERSION}$" | head -1 || echo "")
if [ -n "$LAST_TAG" ]; then
COMMIT_RANGE="${LAST_TAG}..HEAD"
print_status "Commits since last tag: ${LAST_TAG}"
else
TOTAL=$(git rev-list --count HEAD 2>/dev/null || echo "0")
if [ "$TOTAL" -le 50 ]; then
COMMIT_RANGE="HEAD"
else
COMMIT_RANGE="HEAD~50..HEAD"
fi
print_status "No previous tag found — using last ${TOTAL} commits"
fi
COMMIT_COUNT=$(git log --oneline $COMMIT_RANGE 2>/dev/null | wc -l | tr -d ' ')
print_status "Commits to include: ${COMMIT_COUNT}"
echo ""
# =============================================================================
# CASE A: Tag already exists
# =============================================================================
if git rev-parse "$VERSION" >/dev/null 2>&1; then
if [ "$FORCE" = false ]; then
print_warning "Tag ${VERSION} already exists — performing regular commit only"
print_warning "Use --force to recreate the tag and trigger a new release"
echo ""
# Check that local branch is not behind any remote before committing
if [ "$DRY_RUN" = false ]; then
for remote in "${REMOTES[@]}"; do
git fetch "$remote" main 2>/dev/null || true
LOCAL=$(git rev-parse HEAD)
REMOTE_REF=$(git rev-parse "$remote/main" 2>/dev/null || echo "")
if [ -n "$REMOTE_REF" ] && [ "$LOCAL" != "$REMOTE_REF" ]; then
BEHIND=$(git rev-list --count HEAD.."$remote/main" 2>/dev/null || echo "0")
if [ "$BEHIND" -gt 0 ]; then
print_error "Local branch is ${BEHIND} commit(s) behind ${remote}/main"
print_error "Run: git pull --rebase ${remote} main — then re-run release.sh"
exit 1
fi
fi
done
fi
if [ "$DRY_RUN" = false ]; then
git add .
if git commit -m "$FULL_COMMIT_MSG"; then
print_success "Changes committed"
else
print_status "Nothing to commit — pushing existing commits"
fi
push_all
else
print_status "[dry-run] Would commit and push to: ${REMOTES[*]}"
fi
else
print_warning "Force mode: recreating tag ${VERSION}"
if [ "$DRY_RUN" = false ]; then
git tag -d "$VERSION" 2>/dev/null || true
for remote in "${REMOTES[@]}"; do
git push "$remote" --delete "$VERSION" 2>/dev/null || true
done
git add .
git commit -m "$FULL_COMMIT_MSG" || \
print_status "No new changes to commit — proceeding with tag"
if [ "$NO_CHANGELOG" = false ]; then
update_changelog "$VERSION" "$COMMIT_RANGE"
git add "$CHANGELOG_FILE"
if ! git diff --cached --quiet "$CHANGELOG_FILE"; then
git commit -m "chore: update CHANGELOG.md for ${VERSION}"
fi
fi
# Rich annotated tag (write to file to preserve all newlines)
tag_tmp=$(mktemp)
printf 'Release %s\n\n' "${VERSION}" >> "$tag_tmp"
generate_release_notes "$COMMIT_RANGE" "$GITHUB_REPO" >> "$tag_tmp"
git tag -a "$VERSION" -F "$tag_tmp"
rm -f "$tag_tmp"
print_success "Recreated tag: ${VERSION}"
push_all "$VERSION"
print_status "GitHub Actions will create the release with build artifacts"
else
print_status "[dry-run] Would force-recreate tag ${VERSION} and push to: ${REMOTES[*]}"
fi
echo ""
print_success "========================================"
print_success " Release ${VERSION} force-recreated!"
print_success "========================================"
fi
# =============================================================================
# CASE B: New version — create release
# =============================================================================
else
print_status "New version detected — creating release ${VERSION}"
echo ""
# Preview changelog
print_header "--- Changelog Preview ---"
generate_release_notes "$COMMIT_RANGE" "$GITHUB_REPO"
echo ""
print_header "-------------------------"
echo ""
if [ "$DRY_RUN" = false ]; then
git add .
if git commit -m "$FULL_COMMIT_MSG"; then
print_success "Changes committed"
else
print_status "No staged changes — continuing with tag creation"
fi
# Update CHANGELOG.md
if [ "$NO_CHANGELOG" = false ]; then
update_changelog "$VERSION" "$COMMIT_RANGE"
git add "$CHANGELOG_FILE"
if ! git diff --cached --quiet "$CHANGELOG_FILE"; then
git commit -m "chore: update CHANGELOG.md for ${VERSION}"
print_success "CHANGELOG.md committed"
fi
fi
# Rich annotated tag (write to file to preserve all newlines)
tag_tmp=$(mktemp)
printf 'Release %s\n\n' "${VERSION}" >> "$tag_tmp"
generate_release_notes "$COMMIT_RANGE" "$GITHUB_REPO" >> "$tag_tmp"
git tag -a "$VERSION" -F "$tag_tmp"
rm -f "$tag_tmp"
print_success "Created tag: ${VERSION}"
push_all "$VERSION"
print_status "GitHub Actions will create the release with build artifacts"
else
print_status "[dry-run] Would commit, tag ${VERSION}, update CHANGELOG, and push to: ${REMOTES[*]}"
fi
echo ""
print_success "========================================"
print_success " Release ${VERSION} created and pushed!"
print_success " GitHub Actions will build the binaries"
print_success "========================================"
fi