Files
werewulf/scripts/C10-publish-release.sh
T
ilkerandClaude Opus 5 c2f85016a9 scripts: publish a tagged release, with its binaries, to Gitea
`make release` produces the artefacts; this puts them where someone
can download them. The tag's own message becomes the release notes,
so the two cannot drift.

It checks three things before uploading anything, because each one
fails in a way that is otherwise hard to read: the checksums have to
match the files, the tag has to be on the server and not only here,
and the token has to have repository write. Gitea answers a
packages-only token with a bare 403, which looks exactly like a wrong
token; the script says which it was.

Re-running replaces attachments rather than adding a second file
under the same name, which Gitea would otherwise allow.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-22 00:00:09 +02:00

86 lines
3.9 KiB
Bash
Executable File

#!/bin/bash
# Publishes a tagged release to Gitea: the release itself, then the binaries
# from hpcod/dist as attachments.
#
# cd hpcod && make release
# GITEA_TOKEN=... ./scripts/C10-publish-release.sh v0.1.0
#
# The token needs write access to the repository. A packages-only token can
# push images but cannot create a release, and Gitea answers that with a 403
# rather than an explanation - the check below says which it was.
#
# Re-running is safe: an existing release is reused, and an attachment already
# there is deleted before the new one goes up, because Gitea will otherwise
# happily hold two files with the same name.
set -euo pipefail
TAG=${1:?usage: GITEA_TOKEN=... $0 vX.Y.Z}
API=${GITEA_API:-https://gitea.mynodes.xyz/api/v1}
REPO=${GITEA_REPO:-ilker/werewulf}
DIST=${DIST:-hpcod/dist}
: "${GITEA_TOKEN:?set GITEA_TOKEN to a Gitea token with write access to $REPO}"
auth=(-H "Authorization: token ${GITEA_TOKEN}")
api() { curl -sS "${auth[@]}" "$@"; }
# --- 1) The artefacts have to exist, and be the ones this tag names ----------
[ -d "$DIST" ] || { echo "no $DIST: run 'make release' in hpcod/ first"; exit 1; }
[ -f "$DIST/SHA256SUMS" ] || { echo "no $DIST/SHA256SUMS"; exit 1; }
files=("$DIST"/*"$TAG"*)
[ -e "${files[0]}" ] || { echo "$DIST holds nothing for $TAG"; exit 1; }
( cd "$DIST" && shasum -a 256 -c SHA256SUMS >/dev/null ) \
|| { echo "checksums do not match the files in $DIST"; exit 1; }
echo "artefacts for $TAG: ${#files[@]} + SHA256SUMS, checksums verified"
# --- 2) The token has to be able to write here -------------------------------
perm=$(api "$API/repos/$REPO" | python3 -c \
'import json,sys; print(json.load(sys.stdin).get("permissions",{}).get("push"))' 2>/dev/null || echo error)
[ "$perm" = "True" ] || {
echo "this token cannot write to $REPO (push permission: $perm)."
echo "a packages-only token is not enough; it needs repository write."
exit 1
}
# --- 3) The tag has to be on the server, not only here -----------------------
api "$API/repos/$REPO/tags/$TAG" | grep -q '"name"' \
|| { echo "$TAG is not on the server: git push origin $TAG"; exit 1; }
# --- 4) The release ----------------------------------------------------------
id=$(api "$API/repos/$REPO/releases/tags/$TAG" | python3 -c \
'import json,sys; print(json.load(sys.stdin).get("id",""))' 2>/dev/null || true)
if [ -z "$id" ]; then
# The tag's own message is the release notes: it was written once, when
# the tag was made, and retyping it here is how the two drift apart.
body=$(mktemp)
trap 'rm -f "$body"' EXIT
git tag -l --format='%(contents)' "$TAG" | python3 -c \
'import json,sys; n=sys.argv[1]; print(json.dumps({"tag_name":n,"name":n,"body":sys.stdin.read()}))' \
"$TAG" > "$body"
id=$(api -X POST -H 'Content-Type: application/json' --data-binary @"$body" \
"$API/repos/$REPO/releases" | python3 -c \
'import json,sys; print(json.load(sys.stdin)["id"])')
echo "created release $TAG (id $id)"
else
echo "reusing release $TAG (id $id)"
fi
# --- 5) The attachments ------------------------------------------------------
existing=$(api "$API/repos/$REPO/releases/$id/assets" | python3 -c \
'import json,sys; print("\n".join(f"{a[\"id\"]} {a[\"name\"]}" for a in json.load(sys.stdin)))')
for f in "${files[@]}" "$DIST/SHA256SUMS"; do
name=$(basename "$f")
old=$(printf '%s\n' "$existing" | awk -v n="$name" '$2==n {print $1}')
[ -n "$old" ] && api -X DELETE "$API/repos/$REPO/releases/$id/assets/$old" >/dev/null
api -X POST -F "attachment=@$f" \
"$API/repos/$REPO/releases/$id/assets?name=$name" >/dev/null
echo " uploaded $name"
done
# --- 6) Read back what the server actually holds -----------------------------
echo
echo "the server now holds, for $TAG:"
api "$API/repos/$REPO/releases/$id/assets" | python3 -c '
import json,sys
for a in json.load(sys.stdin):
print(f" {a[\"name\"]:<34} {a[\"size\"]:>10,} bytes {a[\"browser_download_url\"]}")'