devbox/scripts/release.sh
shoemoney 72e2a617d4
Some checks are pending
CI / vet-build-test (push) Waiting to run
fix: remove the moving distribution tag before Forgejo draft promotion
2026-11-27 19:48:52 -06:00

83 lines
5.2 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
# Build and publish a complete release to the public distribution repository.
# ./scripts/release.sh v2.1.0
# DEVBOX_SKIP_BUILD=1 ./scripts/release.sh latest # promote the same verified build
# Versioned releases are immutable. Uploads remain draft until every asset exists.
cd "$(dirname "$0")/.."
TAG="${1:-latest}"
API="${FORGEJO_API:-https://git.shoemoney.ai/api/v1}"
REPO="${REPO:-shoemoney/devbox-dist}"
DIST_BRANCH="${DIST_BRANCH:-main}"
TOKEN="${FORGEJO_TOKEN:-$(cat ~/.config/forgejo/token 2>/dev/null || true)}"
[ -n "$TOKEN" ] || { echo 'No Forgejo token configured' >&2; exit 1; }
[[ "$TAG" =~ ^(latest|v[0-9]+\.[0-9]+\.[0-9]+)$ ]] || { echo 'Expected latest or vMAJOR.MINOR.PATCH' >&2; exit 1; }
hashes() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$@"; else shasum -a 256 "$@"; fi; }
auth=(-H "Authorization: token $TOKEN")
api() { curl --retry 2 --retry-delay 1 -fsSL "${auth[@]}" "$@"; }
COMMIT=$(git rev-parse HEAD)
# Dist assets must have been built from exactly this source revision.
if [ "${DEVBOX_SKIP_BUILD:-0}" != 1 ]; then
if [ "$TAG" = latest ]; then ./scripts/build-release.sh; else VERSION="$TAG" ./scripts/build-release.sh; fi
fi
[ -f dist/BUILD_INFO ] || { echo 'Missing dist/BUILD_INFO; build first' >&2; exit 1; }
[ "$(sed -n '1p' dist/BUILD_INFO)" = "$COMMIT" ] || { echo 'Build source revision does not match HEAD' >&2; exit 1; }
BUILD_VERSION=$(sed -n '2p' dist/BUILD_INFO)
[ "$TAG" = latest ] || [ "$BUILD_VERSION" = "$TAG" ] || { echo 'Build version does not match release tag' >&2; exit 1; }
(cd dist && hashes -c SHA256SUMS)
# Prepare the exact names downloaded by install.sh and checksum the installers too.
STAGE=dist/release-assets
rm -rf "$STAGE"
mkdir -p "$STAGE"
for bin in devbox devbox-hub; do
for target in linux/amd64 linux/arm64 linux/arm darwin/arm64 darwin/amd64 windows/amd64; do
os="${target%/*}"; arch="${target#*/}"; ext=""
[ "$os" != windows ] || ext=.exe
cp "dist/${bin}_${BUILD_VERSION}_${os}_${arch}${ext}" "$STAGE/${bin}_${os}_${arch}${ext}"
done
done
cp install.sh install.ps1 "$STAGE/"
(cd "$STAGE" && hashes devbox_* devbox-hub_* install.sh install.ps1 > SHA256SUMS)
# Read errors fail the release; a 404 alone means the tag has no release.
old_json=$(mktemp)
trap 'rm -f "$old_json"' EXIT
code=$(curl -sSL "${auth[@]}" -o "$old_json" -w '%{http_code}' "$API/repos/$REPO/releases/tags/$TAG")
old_id=""
case "$code" in
200) old_id=$(python3 -c 'import json,sys;print(json.load(open(sys.argv[1]))["id"])' "$old_json");;
404) ;;
*) echo "Release lookup failed: HTTP $code" >&2; exit 1;;
esac
if [ -n "$old_id" ] && [ "$TAG" != latest ]; then
echo "Release $TAG already exists; choose a new version" >&2; exit 1
fi
upload_tag="$TAG"
[ "$TAG" != latest ] || upload_tag="latest-staging-${COMMIT:0:12}-$(date +%s)"
body=$(printf 'devbox %s — built from source `%s`, binary version `%s`.\n\nBuilt-in Tailscale in the hub: `devbox-hub serve --data ./hub-data --tailscale`. No separate Tailscale installation is required on the hub. Authenticate using the logged login URL or TS_AUTHKEY; identity persists under <data>/tailscale. Clients use the regular Tailscale app. Existing HTTP/HTTPS deployments remain supported.\n\nInstall: `curl -fsSL https://git.shoemoney.ai/%s/releases/download/%s/install.sh -o install.sh && sh install.sh --release-url https://git.shoemoney.ai/%s/releases/download/%s`\n\nAssets include 12 binaries, both installers, and SHA256SUMS. Building from source requires Go 1.27.4+.' "$TAG" "$COMMIT" "$BUILD_VERSION" "$REPO" "$TAG" "$REPO" "$TAG")
rel_id=$(api -X POST "$API/repos/$REPO/releases" -H 'Content-Type: application/json' \
-d "$(python3 -c 'import json,sys;print(json.dumps(dict(tag_name=sys.argv[1],target_commitish=sys.argv[2],name=sys.argv[3],body=sys.argv[4],draft=True,prerelease=False)))' "$upload_tag" "$DIST_BRANCH" "$TAG" "$body")" \
| python3 -c 'import json,sys;print(json.load(sys.stdin)["id"])')
echo "Uploading draft release $TAG (id $rel_id)"
for f in "$STAGE"/*; do
name=$(basename "$f")
echo " upload $name"
# Do not retry non-idempotent uploads: preserve a failed draft for inspection.
curl -fsSL "${auth[@]}" -X POST "$API/repos/$REPO/releases/$rel_id/assets?name=$name" -F "attachment=@$f" >/dev/null
done
count=$(api "$API/repos/$REPO/releases/$rel_id" | python3 -c 'import json,sys;print(len(json.load(sys.stdin)["assets"]))')
[ "$count" = 15 ] || { echo "Expected 15 assets, got $count; leaving draft unpublished" >&2; exit 1; }
# Keep the existing moving release until the replacement is fully uploaded.
if [ -n "$old_id" ]; then api -X DELETE "$API/repos/$REPO/releases/$old_id" >/dev/null; fi
# Forgejo cannot publish a renamed draft onto an existing tag (it returns 500).
# Remove only the moving distribution tag after uploads; version tags stay fixed.
if [ "$TAG" = latest ]; then
code=$(curl -sSL "${auth[@]}" -X DELETE -o "$old_json" -w '%{http_code}' "$API/repos/$REPO/tags/latest")
case "$code" in 200|204|404) ;; *) echo "Moving tag removal failed: HTTP $code" >&2; exit 1;; esac
fi
api -X PATCH "$API/repos/$REPO/releases/$rel_id" -H 'Content-Type: application/json' \
-d "$(python3 -c 'import json,sys;print(json.dumps(dict(tag_name=sys.argv[1],draft=False)))' "$TAG")" >/dev/null
echo "https://git.shoemoney.ai/$REPO/releases/tag/$TAG"