Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ docker TAG="local":
release:
./scripts/release.sh

# Exercise reserved-tag retries without creating remote refs.
release-test:
bash ./scripts/release_test.sh

# Regenerate the legal inventory for every shipped Go target and browser app.
notices:
go run ./internal/cmd/notices -root .
Expand Down Expand Up @@ -298,7 +302,7 @@ social-card:
# a settings page that no longer matches the type the loader binds is a
# documented setting the binary would reject, and it should fail here rather
# than be published.
check: fmt-check lint ui-audit ui-check notices-check test ui-test docs-generate-check site-build
check: fmt-check lint release-test ui-audit ui-check notices-check test ui-test docs-generate-check site-build
@echo "All checks passed"

clean:
Expand Down
129 changes: 90 additions & 39 deletions scripts/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,95 @@ die() {
exit 1
}

command -v gh >/dev/null 2>&1 || die "gh is required"
[ "$(git rev-parse --abbrev-ref HEAD)" = "main" ] || die "run from main"
[ -z "$(git status --porcelain)" ] || die "the worktree must be clean"

git fetch origin --tags main
head_before=$(git rev-parse HEAD)
origin_before=$(git rev-parse origin/main)
[ "$head_before" = "$origin_before" ] || die "main is not up to date with origin/main"

# A second tag while the prior release is running can race the moving `latest`
# image and GitHub's latest-release selection.
active=$(gh run list --workflow release.yml --limit 20 \
--json status --jq '[.[] | select(.status != "completed")] | length')
[ "$active" = "0" ] || die "another release workflow is still active"

just check
just test-race

[ -z "$(git status --porcelain)" ] || die "verification changed the worktree"
[ "$(git rev-parse HEAD)" = "$head_before" ] || die "HEAD changed during verification"
git fetch origin --tags main
[ "$(git rev-parse origin/main)" = "$origin_before" ] || die "origin/main changed during verification"

year=$(date +%Y)
month=$(date +%m)
month=${month#0}
series="v${year}.${month}"
last=$(git tag --list "${series}.*" --sort=-v:refname | grep -E "^${series}\.[0-9]+$" | head -1 || true)
if [ -z "$last" ]; then
number=0
else
number=$(( ${last##*.} + 1 ))
fi
tag="${series}.${number}"
create_release_tag() {
local repo=$1
local head=$2
local series=$3
local number=$4
local response
local attempts=0
local candidate

while ((attempts < 1000)); do
candidate="${series}.${number}"
if response=$(gh api --silent --method POST "repos/${repo}/git/refs" \
-f "ref=refs/tags/${candidate}" -f "sha=${head}" 2>&1); then
git fetch origin "refs/tags/${candidate}:refs/tags/${candidate}" >/dev/null
printf 'release: created %s at %s\n' "$candidate" "$head" >&2
printf '%s\n' "$candidate"
return 0
fi

case "$response" in
*"Reference update failed"*"HTTP 422"* | *"Reference already exists"*"HTTP 422"*)
printf 'release: %s is unavailable; trying the next number\n' "$candidate" >&2
number=$((number + 1))
attempts=$((attempts + 1))
;;
*)
printf '%s\n' "$response" >&2
return 1
;;
esac
done

printf 'release: no available tag found after 1000 attempts\n' >&2
return 1
}

main() {
command -v gh >/dev/null 2>&1 || die "gh is required"
[ "$(git rev-parse --abbrev-ref HEAD)" = "main" ] || die "run from main"
[ -z "$(git status --porcelain)" ] || die "the worktree must be clean"

git fetch origin --tags main
local head_before
local origin_before
head_before=$(git rev-parse HEAD)
origin_before=$(git rev-parse origin/main)
[ "$head_before" = "$origin_before" ] || die "main is not up to date with origin/main"

# A second tag while the prior release is running can race the moving `latest`
# image and GitHub's latest-release selection.
local active
active=$(gh run list --workflow release.yml --limit 20 \
--json status --jq '[.[] | select(.status != "completed")] | length')
[ "$active" = "0" ] || die "another release workflow is still active"

just check
just test-race

[ -z "$(git status --porcelain)" ] || die "verification changed the worktree"
[ "$(git rev-parse HEAD)" = "$head_before" ] || die "HEAD changed during verification"
git fetch origin --tags main
[ "$(git rev-parse origin/main)" = "$origin_before" ] || die "origin/main changed during verification"

local year
local month
local series
local last
local number
local repo
local created_tag
year=$(date +%Y)
month=$(date +%m)
month=${month#0}
series="v${year}.${month}"
last=$(git tag --list "${series}.*" --sort=-v:refname | grep -E "^${series}\.[0-9]+$" | head -1 || true)
if [ -z "$last" ]; then
number=0
else
number=$((${last##*.} + 1))
fi
repo=$(gh repo view --json nameWithOwner --jq .nameWithOwner)

printf 'release: %s -> searching from %s.%s at %s\n' \
"${last:-<first this month>}" "$series" "$number" "$head_before"
created_tag=$(create_release_tag "$repo" "$head_before" "$series" "$number") ||
die "could not create a release tag"
printf 'release: %s is ready\n' "$created_tag"
}

printf 'release: %s -> %s at %s\n' "${last:-<first this month>}" "$tag" "$head_before"
git tag "$tag"
if ! git push origin "$tag"; then
git tag -d "$tag" >/dev/null
die "push failed; removed local tag"
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
main "$@"
fi
52 changes: 52 additions & 0 deletions scripts/release_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env bash
set -euo pipefail

# shellcheck source=scripts/release.sh
source "$(dirname "$0")/release.sh"

fail() {
printf 'release_test: %s\n' "$1" >&2
exit 1
}

test_reserved_tags_are_skipped() (
local created

gh() {
case "$*" in
*refs/tags/v2026.8.0* | *refs/tags/v2026.8.1*)
printf 'gh: Reference update failed (HTTP 422)\n' >&2
return 1
;;
*refs/tags/v2026.8.2*) return 0 ;;
*) return 64 ;;
esac
}

git() {
[ "$*" = "fetch origin refs/tags/v2026.8.2:refs/tags/v2026.8.2" ] ||
fail "unexpected fetch: $*"
}

created=$(create_release_tag labstack/fanout deadbeef v2026.8 0)
[ "$created" = "v2026.8.2" ] || fail "created $created, want v2026.8.2"
)

test_unrelated_errors_are_not_retried() (
gh() {
printf 'gh: HTTP 403: Resource not accessible\n' >&2
return 1
}

git() {
fail "git called after API failure: $*"
}

if create_release_tag labstack/fanout deadbeef v2026.8 0 2>/dev/null; then
fail "permission error unexpectedly succeeded"
fi
)

test_reserved_tags_are_skipped
test_unrelated_errors_are_not_retried
printf 'release_test: all tests passed\n'
Loading