fix: gitea-release.sh tolerate 404 on tag lookup; report HTTP errors; mark v0.22.0 published
This commit is contained in:
+32
-12
@@ -8,38 +8,58 @@
|
||||
# The git tag (vX.Y.Z, derived from the version) must already exist and be pushed
|
||||
# (`git tag v0.22.0 && git push gitea v0.22.0`). Re-running is idempotent: it
|
||||
# reuses an existing release for the tag and replaces a same-named asset.
|
||||
# Set GITEA_INSECURE=1 to skip TLS verification (self-signed cert on a LAN box).
|
||||
set -euo pipefail
|
||||
|
||||
VERSION="${1:-}"; S9PK="${2:-}"
|
||||
[ -n "$VERSION" ] && [ -n "$S9PK" ] || {
|
||||
echo "usage: GITEA_URL=.. GITEA_TOKEN=.. $0 <version e.g. 0.22.0:0> <s9pk path>" >&2; exit 2; }
|
||||
: "${GITEA_URL:?set GITEA_URL to your Gitea base URL, e.g. https://gitea.lan:3000}"
|
||||
: "${GITEA_TOKEN:?set GITEA_TOKEN to a token with repository write access}"
|
||||
: "${GITEA_TOKEN:?set GITEA_TOKEN to a token with repository read+write access}"
|
||||
[ -f "$S9PK" ] || { echo "s9pk not found: $S9PK" >&2; exit 1; }
|
||||
|
||||
TAG="v${VERSION%%:*}" # 0.22.0:0 -> v0.22.0
|
||||
ASSET="$(basename "$S9PK")"
|
||||
SLUG="$(git remote get-url gitea | sed -E 's#.*[:/]([^/:]+/[^/]+)\.git$#\1#')" # grant/spark-control
|
||||
API="${GITEA_URL%/}/api/v1/repos/${SLUG}"
|
||||
AUTH=(-H "Authorization: token ${GITEA_TOKEN}")
|
||||
CURL=(curl -sS) # no -f: we inspect HTTP codes ourselves
|
||||
[ "${GITEA_INSECURE:-}" = "1" ] && CURL+=(-k)
|
||||
|
||||
echo "repo ${SLUG} | tag ${TAG} | asset ${ASSET} | ${GITEA_URL}"
|
||||
|
||||
# api METHOD URL [extra curl args...] -> sets globals HTTP_CODE and BODY
|
||||
api() {
|
||||
local method="$1" url="$2"; shift 2
|
||||
local out
|
||||
out="$("${CURL[@]}" -X "$method" -H "Authorization: token ${GITEA_TOKEN}" "$@" \
|
||||
-w $'\n%{http_code}' "$url")"
|
||||
HTTP_CODE="${out##*$'\n'}"
|
||||
BODY="${out%$'\n'*}"
|
||||
}
|
||||
|
||||
# Reuse an existing release for this tag, otherwise create one.
|
||||
id="$(curl -fsS "${AUTH[@]}" "$API/releases/tags/$TAG" 2>/dev/null | jq -r '.id // empty')"
|
||||
if [ -z "$id" ]; then
|
||||
id="$(curl -fsS -X POST "${AUTH[@]}" -H 'Content-Type: application/json' \
|
||||
api GET "$API/releases/tags/$TAG"
|
||||
if [ "$HTTP_CODE" = 200 ]; then
|
||||
id="$(printf '%s' "$BODY" | jq -r '.id')"
|
||||
elif [ "$HTTP_CODE" = 404 ]; then
|
||||
api POST "$API/releases" -H 'Content-Type: application/json' \
|
||||
--data "$(jq -n --arg t "$TAG" --arg n "$VERSION" \
|
||||
'{tag_name:$t, name:$n, body:("Spark Control "+$n+". See AGENTS.md / release notes.")}')" \
|
||||
"$API/releases" | jq -r '.id')"
|
||||
'{tag_name:$t, name:$n, body:("Spark Control "+$n+". See AGENTS.md / release notes.")}')"
|
||||
[ "$HTTP_CODE" = 201 ] || { echo "create release failed (HTTP $HTTP_CODE): $BODY" >&2; exit 1; }
|
||||
id="$(printf '%s' "$BODY" | jq -r '.id')"
|
||||
else
|
||||
echo "release lookup failed (HTTP $HTTP_CODE) — check GITEA_URL and the token's scope: $BODY" >&2
|
||||
exit 1
|
||||
fi
|
||||
[ -n "$id" ] && [ "$id" != null ] || { echo "could not obtain release id (check URL/token/tag)" >&2; exit 1; }
|
||||
[ -n "$id" ] && [ "$id" != null ] || { echo "could not parse release id: $BODY" >&2; exit 1; }
|
||||
|
||||
# Replace a same-named asset so re-runs don't 409.
|
||||
old="$(curl -fsS "${AUTH[@]}" "$API/releases/$id/assets" | jq -r --arg n "$ASSET" '.[] | select(.name==$n) | .id')"
|
||||
[ -n "$old" ] && curl -fsS -X DELETE "${AUTH[@]}" "$API/releases/$id/assets/$old" >/dev/null || true
|
||||
api GET "$API/releases/$id/assets"
|
||||
old="$(printf '%s' "$BODY" | jq -r --arg n "$ASSET" '.[]? | select(.name==$n) | .id')"
|
||||
[ -n "$old" ] && { api DELETE "$API/releases/$id/assets/$old"; }
|
||||
|
||||
curl -fsS -X POST "${AUTH[@]}" -F "attachment=@${S9PK};type=application/octet-stream" \
|
||||
"$API/releases/$id/assets?name=$ASSET" >/dev/null
|
||||
api POST "$API/releases/$id/assets?name=$ASSET" \
|
||||
-F "attachment=@${S9PK};type=application/octet-stream"
|
||||
[ "$HTTP_CODE" = 201 ] || { echo "asset upload failed (HTTP $HTTP_CODE): $BODY" >&2; exit 1; }
|
||||
|
||||
echo "published: ${GITEA_URL%/}/${SLUG}/releases/tag/${TAG}"
|
||||
|
||||
Reference in New Issue
Block a user