Abbaye

at 44279e3 Raw

#!/usr/bin/env bash
#
# Bump, tag, build, and push a new release.
#
# Usage:  ./scripts/release.sh [-y] <version>
#
# Prerequisites:
#   - On `main` branch, working tree clean
#   - Tools: rust/cargo, git-cliff, cargo-readme, prek
#
# What it does:
#   1. Pre-flight: branch check, clean tree, user confirmation
#   2. Regenerate README.md + abbaye.schema.json
#   3. Bump version in Cargo.toml, commit
#   4. Tag (so git-cliff can find it)
#   5. Generate changelog, amend release commit
#   6. Pre-commit hooks (may amend again if hooks modify files)
#   7. Release build
#   8. Force-push branch + tags
#   9. Build abbaye website with itself
#  10. Deploy docs to server

set -euo pipefail

# Parse flags
YES=0
while [[ "${1:-}" == -* ]]; do
    case "$1" in
        -y|--yes) YES=1; shift ;;
        *) echo "Unknown option: $1"; exit 1 ;;
    esac
done

VERSION="${1:-}"
if [[ -z "$VERSION" ]]; then
    echo "Usage: $0 [-y] <version>"
    exit 1
fi

# ── Pre-flight ─────────────────────────────────────────────────────────────

BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [[ "$BRANCH" != "main" ]]; then
    echo "Error: must be on main branch (currently on $BRANCH)"
    exit 1
fi

if ! git diff --quiet; then
    echo "Error: working tree has uncommitted changes"
    exit 1
fi

# Strip optional "v" prefix so the rest of the script can safely prepend it.
VERSION="${VERSION#v}"

# Ensure native aarch64-musl cross-compiler linker is available.
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER="aarch64-linux-musl-gcc"

# Confirm unless -y was passed or stdin is not a terminal.
if [[ "$YES" == "0" ]] && [[ -t 0 ]]; then
    read -rp "Release v${VERSION}? [y/N] " CONFIRM
    if [[ "$CONFIRM" != "y" && "$CONFIRM" != "Y" ]]; then
        echo "Aborted."
        exit 1
    fi
fi

# ── Pre-flight regeneration ────────────────────────────────────────────────

echo "=== Regenerating README ==="
cargo readme --no-indent-headings --no-title > README.md
git add README.md

echo "=== Regenerating schema ==="
cargo run --bin abbaye dump-schema > abbaye.schema.json
git add abbaye.schema.json

# ── Bump version ───────────────────────────────────────────────────────────

echo "=== Bumping version to $VERSION ==="
cargo set-version "$VERSION"
git add Cargo.toml Cargo.lock

if ! git diff --cached --quiet; then
    scripts/commit.sh --tag "v${VERSION}" -m "chore: release v${VERSION}" -n
else
    echo "Warning: No changes detected in Cargo.toml or Cargo.lock. Skipping version commit."
fi

# ── Mise plugin test ────────────────────────────────────────────────────────

echo "=== Testing mise plugin ==="
# Use the latest *already—published* version (the one being released
# is not yet on the download server).
LATEST=$(bash .mise/plugins/abbaye/bin/list-all | sort -V | grep -v "^$VERSION$" | tail -1)
echo "  Testing against latest published version: $LATEST"
if bash scripts/test-mise-plugin.sh "$LATEST" alpine:latest; then
    echo "=== Mise plugin OK ==="
else
    echo "=== WARNING: mise plugin test failed - older binary may not be on server ==="
fi

# ── Pre-commit hooks ───────────────────────────────────────────────────────

echo "=== Running pre-commit hooks ==="
# The changelog check hook is expected to fail here: the tag points at the
# pre-amend commit, so git-cliff doesn't yet see this version as released.
prek run --all-files || echo "=== Note: some pre-commit hooks may fail (expected) ==="

# If hooks modified anything (trailing whitespace, EOF fixes, fmt), fold them
# into the release commit.
if ! git diff --quiet; then
    git add -A
    git commit --amend --no-edit -n
fi

# ── Build ──────────────────────────────────────────────────────────────────

echo "=== Building release binary ==="
cargo build --release

# ── Push (move tag to the final amended commit) ────────────────────────────

echo "=== Pushing ==="
git tag --force "v${VERSION}"
git push --force

# ── Post-release ───────────────────────────────────────────────────────────

echo "=== Building abbaye website ==="
cargo run --release --bin abbaye -- build

echo "=== Deploying docs ==="
rsync --progress -avz --links --perms --update public/ ololduck@vit.am:public_html/abbaye/