#!/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 -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 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}") echo "repo ${SLUG} | tag ${TAG} | asset ${ASSET} | ${GITEA_URL}" # 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' \ --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')" fi [ -n "$id" ] && [ "$id" != null ] || { echo "could not obtain release id (check URL/token/tag)" >&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 curl -fsS -X POST "${AUTH[@]}" -F "attachment=@${S9PK};type=application/octet-stream" \ "$API/releases/$id/assets?name=$ASSET" >/dev/null echo "published: ${GITEA_URL%/}/${SLUG}/releases/tag/${TAG}"