search_hub

at 2ab9aa7 Raw

#!/usr/bin/env bash
set -euo pipefail

if [ $# -ne 1 ]; then
    echo "Usage: $0 <version>"
    echo "  e.g. $0 0.2.0"
    exit 1
fi

VERSION="$1"

# Validate semver
if ! echo "$VERSION" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+$'; then
    echo "error: version must be semver (e.g. 0.2.0)"
    exit 1
fi

# Must be on main
BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ "$BRANCH" != "main" ]; then
    echo "error: releases must be made from main (currently on $BRANCH)"
    exit 1
fi

# Working tree must be clean
if ! git diff --stat --exit-code; then
    echo "error: working tree has uncommitted changes"
    exit 1
fi
if ! git diff --cached --stat --exit-code; then
    echo "error: staged but uncommitted changes"
    exit 1
fi

# Tag must not already exist
if git tag -l "v$VERSION" | grep -q .; then
    echo "error: tag v$VERSION already exists"
    exit 1
fi

# CHANGELOG must exist and have an [Unreleased] section with content
if [ ! -f CHANGELOG.md ]; then
    echo "error: CHANGELOG.md not found"
    exit 1
fi
UNRELEASED=$(awk '/^## \[Unreleased\]/{found=1;next} /^## \[/{if(found) exit} found{print}' CHANGELOG.md | sed '/^$/d' | wc -l)
if [ "$UNRELEASED" -eq 0 ]; then
    echo "error: CHANGELOG.md has an [Unreleased] section but it is empty"
    echo "  Add entries under ## [Unreleased] before releasing."
    exit 1
fi

echo "==> Bumping version to $VERSION in Cargo.toml"
sed -i "s/^version = \".*\"/version = \"$VERSION\"/" Cargo.toml

echo "==> Running cargo test"
cargo test 2>&1

echo "==> Running cargo build --release"
cargo build --release 2>&1

echo "==> Updating CHANGELOG.md for v$VERSION"
RELEASE_DATE=$(date +%Y-%m-%d)
awk -v ver="$VERSION" -v date="$RELEASE_DATE" '
    /^## \[Unreleased\]/ {
        print "## [Unreleased]"
        print ""
        print "## [" ver "] - " date
        in_unreleased = 1
        next
    }
    /^## \[/ && in_unreleased {
        in_unreleased = 0
        print ""
        print $0
        next
    }
    in_unreleased { print; next }
    { print }
' CHANGELOG.md > CHANGELOG.md.tmp && mv CHANGELOG.md.tmp CHANGELOG.md

echo "==> Committing release: v$VERSION"
git add Cargo.toml Cargo.lock CHANGELOG.md
git commit -m "release: v$VERSION"

echo "==> Tagging v$VERSION"
git tag -a "v$VERSION" -m "release: v$VERSION"

echo "==> Building release site with abbaye"
if command -v abbaye &>/dev/null; then
    rm -rf public/
    abbaye build 2>&1 || echo "warning: abbaye build failed"
else
    echo "warning: abbaye not found, skipping site build"
fi

echo "==> Building container image"
if command -v buildah &>/dev/null; then
    buildah build -t "oci.vit.am/search-hub:v$VERSION" -t "oci.vit.am/search-hub:latest" -f Containerfile . 2>&1
    read -r -p "Push container image to oci.vit.am? [y/N] " REPLY
    if [[ "$REPLY" =~ ^[Yy]$ ]]; then
        buildah push "oci.vit.am/search-hub:v$VERSION" 2>&1
        buildah push "oci.vit.am/search-hub:latest" 2>&1
    fi
else
    echo "warning: buildah not found, skipping container image build"
fi

echo "==> Uploading to server"
read -r -p "Upload public/ to ololduck@vit.am:public_html/search_hub/? [y/N] " REPLY
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
    rsync -avz --progress public/ ololduck@vit.am:public_html/search_hub/ 2>&1
fi
echo ""
echo "Release v$VERSION ready."
echo "To publish:     git push --tags origin main"
echo "To upload:      rsync -avz --progress public/ ololduck@vit.am:public_html/search_hub/"
echo "To push image:  buildah push oci.vit.am/search-hub:v$VERSION && buildah push oci.vit.am/search-hub:latest"
echo "To undo:        git tag -d v$VERSION && git reset --soft HEAD~1"