Files
werewulf/scripts/C10-publish-release.sh
T
ilkerandClaude Opus 5.5 9010831bff scripts: C10 lists and reads back release attachments on any Python 3
The two one-liners that list a release's attachments escaped their quotes
inside a single-quoted shell string, so Python received a backslash before
each and stopped with a SyntaxError - after the release was created and
before anything was uploaded. Rewritten without f-strings. Found
publishing v0.2.0, which it then published.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 18:25:51 +01: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("%s %s" % (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(" %-34s %12s bytes %s" % (a["name"], format(a["size"], ","), a["browser_download_url"]))'