#!/usr/bin/env bash # Publish a built Spark Control s9pk to Gitea Releases, so adopters can pull the # latest package with a read-only token instead of being hand-sent the file. # # GITEA_URL=https://gitea.example:3000 GITEA_TOKEN= \ # scripts/gitea-release.sh 0.22.0:0 package/spark-control_x86_64.s9pk # # 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 " >&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 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}" 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. 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.")}')" [ "$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 parse release id: $BODY" >&2; exit 1; } # Replace a same-named asset so re-runs don't 409. 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"; } 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}"