#!/usr/bin/env bash # Create (or replace) a Gitea release and upload assets, using the Gitea API. # # Required env: # GITEA_API e.g., https://git.scient.ing/api/v1/repos// # GITEA_TOKEN a token with contents:write # TAG the release tag # ASSETS space-separated list of files to attach # Optional env: # RELEASE_NAME (default: $TAG) # BODY (default: empty) # PRERELEASE true|false (default: false) # REPLACE true|false (default: false) delete an existing release+tag first # TARGET_COMMITISH (default: empty; Gitea uses the default branch) # JQ jq command (default: jq); set to e.g. "pixi exec --spec jq -- jq" # DRY_RUN set to anything to print actions instead of calling the API set -euo pipefail : "${GITEA_API:?}"; : "${GITEA_TOKEN:?}"; : "${TAG:?}"; : "${ASSETS:?}" : "${JQ:=jq}" name="${RELEASE_NAME:-$TAG}" body="${BODY:-}" prerelease="${PRERELEASE:-false}" replace="${REPLACE:-false}" target="${TARGET_COMMITISH:-}" auth="Authorization: token ${GITEA_TOKEN}" dry="${DRY_RUN:-}" say() { echo "[gitea-release] $*"; } if [ "$replace" = "true" ]; then say "replacing any existing '$TAG' release and tag" if [ -n "$dry" ]; then say "DRY_RUN GET/DELETE ${GITEA_API}/releases/tags/${TAG} and DELETE tag ${TAG}" else existing=$(curl -fsSL -H "$auth" "${GITEA_API}/releases/tags/${TAG}" 2>/dev/null || true) if [ -n "$existing" ]; then id=$(printf '%s' "$existing" | $JQ -r '.id // empty') [ -n "$id" ] && curl -fsSL -X DELETE -H "$auth" "${GITEA_API}/releases/${id}" fi curl -fsSL -X DELETE -H "$auth" "${GITEA_API}/tags/${TAG}" 2>/dev/null || true fi fi payload=$($JQ -n \ --arg tag "$TAG" --arg name "$name" --arg body "$body" \ --argjson pre "$prerelease" --arg target "$target" \ '{tag_name:$tag, name:$name, body:$body, draft:false, prerelease:$pre} + (if $target == "" then {} else {target_commitish:$target} end)') say "creating release '$TAG' (prerelease=$prerelease)" if [ -n "$dry" ]; then say "DRY_RUN POST ${GITEA_API}/releases" printf '%s\n' "$payload" rid="" else rid=$(curl -fsSL -X POST -H "$auth" -H "Content-Type: application/json" \ "${GITEA_API}/releases" -d "$payload" | $JQ -r '.id') fi for f in $ASSETS; do n=$(basename "$f") say "uploading $n" if [ -n "$dry" ]; then say "DRY_RUN POST ${GITEA_API}/releases/${rid}/assets?name=${n} (@${f})" else curl -fsSL -X POST -H "$auth" -F "attachment=@${f}" \ "${GITEA_API}/releases/${rid}/assets?name=${n}" fi done say "done"