#!/usr/bin/env bash # Create-or-update a Forgejo release and (re)upload its assets. # # Usage: publish-release.sh [...] # Env: FORGEJO_TOKEN user PAT with repo write (required) # REPO_API default https://forgejo.siriusdevops.com/api/v1/repos/sirius/onionwire # TARGET_COMMITISH optional; set it only when the release may have to # create the tag (e.g. a push event's commit sha). # Leave empty to never move an existing tag. # # Idempotent: re-running for the same tag reuses the release and replaces # same-named assets instead of failing with 409. set -euo pipefail # HTTP/1.1: Cloudflare resets HTTP/2 mid-POST (curl 92). Do NOT use # --retry-all-errors: that retries 422 Unprocessable Entity five times # and hides the response body (run 116). Connection failures still retry. api_curl() { curl -sS --http1.1 --retry 5 --retry-connrefused --retry-delay 3 \ --connect-timeout 20 --max-time 300 "$@" } auth_json() { <<<<<<< Updated upstream api_curl -u "sirius:${FORGEJO_TOKEN}" -H "Content-Type: application/json" "$@" ======= api_curl -u "sirius:${FORGEJO_TOKEN}" -H "Content-Type: application/json" "$@" | sed 's/\r$//' >>>>>>> Stashed changes } tag="${1:?usage: publish-release.sh ...}" name="${2:?missing release name}" body_file="${3:?missing body file}" shift 3 : "${FORGEJO_TOKEN:?FORGEJO_TOKEN is not set}" FORGEJO_TOKEN=$(printf '%s' "$FORGEJO_TOKEN" | tr -d '\r\n') api="${REPO_API:-https://forgejo.siriusdevops.com/api/v1/repos/sirius/onionwire}" target="${TARGET_COMMITISH:-}" jqp() { python3 -c "import json,sys; d=json.load(sys.stdin); print($1)"; } die_http() { local what="$1" code="$2" file="$3" echo "publish: $what HTTP $code" >&2 cat "$file" >&2 || true echo >&2 exit 1 } # Body with @TAG@ substituted, JSON-encoded by python (handles newlines/quotes). python3 - "$body_file" "$tag" "$target" "$name" > /tmp/release-body.json <<'PY' import json, sys body = open(sys.argv[1]).read().replace("@TAG@", sys.argv[2]) payload = { "tag_name": sys.argv[2], "name": sys.argv[4], "body": body, "draft": False, "prerelease": False, } # Only send target_commitish when asked: on an existing tag it is a request to # move the tag, which is never what a re-publish wants. if sys.argv[3]: payload["target_commitish"] = sys.argv[3] print(json.dumps(payload)) PY code=$(auth_json -o /tmp/release-rel.json -w '%{http_code}' \ "$api/releases/tags/$tag") if [ "$code" = "404" ]; then echo "publish: creating release $tag" create=$(auth_json -o /tmp/release-rel.json -w '%{http_code}' \ -X POST --data-binary @/tmp/release-body.json "$api/releases") if [ "$create" = "201" ]; then : elif [ "$create" = "409" ] || [ "$create" = "422" ]; then echo "publish: POST releases HTTP $create — joining if the release exists" cat /tmp/release-rel.json >&2 || true echo >&2 code=$(auth_json -o /tmp/release-rel.json -w '%{http_code}' \ "$api/releases/tags/$tag") if [ "$code" != "200" ]; then die_http "create release $tag (then GET)" "$code" /tmp/release-rel.json fi echo "publish: release $tag exists, joining" else die_http "create release $tag" "$create" /tmp/release-rel.json fi elif [ "$code" = "200" ]; then echo "publish: release $tag exists, updating" rid=$(jqp "d['id']" < /tmp/release-rel.json) patch=$(auth_json -o /tmp/release-rel.json -w '%{http_code}' \ -X PATCH --data-binary @/tmp/release-body.json "$api/releases/$rid") if [ "$patch" != "200" ]; then die_http "PATCH release $rid" "$patch" /tmp/release-rel.json fi else die_http "GET releases/tags/$tag" "$code" /tmp/release-rel.json fi rid=$(jqp "d['id']" < /tmp/release-rel.json) echo "publish: release id $rid ($tag)" # Replace same-named assets so re-runs are clean. for f in "$@"; do [ -f "$f" ] || { echo "publish: missing asset $f" >&2; exit 1; } # Never publish an empty asset: a 0-byte binary/checksum looks like a # successful release and silently breaks everyone who downloads it. bytes=$(wc -c < "$f") if [ "$bytes" -eq 0 ]; then echo "publish: refusing to upload empty asset $f (0 bytes, cwd=$PWD)" >&2 exit 1 fi asset=$(basename "$f") echo "publish: asset $asset is $bytes bytes on disk" api_curl -u "sirius:${FORGEJO_TOKEN}" "$api/releases/$rid/assets" \ | python3 -c " import json,sys for a in json.load(sys.stdin): print(a['id'], a.get('name','')) " | while read -r aid aname; do if [ "$aname" = "$asset" ]; then echo "publish: deleting stale asset $aname ($aid)" del=$(api_curl -o /tmp/release-adel.json -w '%{http_code}' \ -X DELETE -u "sirius:${FORGEJO_TOKEN}" \ "$api/releases/$rid/assets/$aid") if [ "$del" != "204" ] && [ "$del" != "404" ]; then die_http "DELETE asset $aid" "$del" /tmp/release-adel.json fi fi done up=$(api_curl -o /tmp/release-asset.json -w '%{http_code}' \ <<<<<<< Updated upstream -X POST "$api/releases/$rid/assets?name=$asset" \ -u "sirius:${FORGEJO_TOKEN}" \ ======= -X POST -u "sirius:${FORGEJO_TOKEN}" "$api/releases/$rid/assets?name=$asset" \ >>>>>>> Stashed changes -F "attachment=@$f") if [ "$up" != "201" ]; then die_http "upload $asset" "$up" /tmp/release-asset.json fi python3 -c " import json,sys d=json.load(open('/tmp/release-asset.json')) name, size = d.get('name'), d.get('size') print('publish: uploaded', name, size, 'bytes') if not size: sys.exit('publish: server stored 0-byte asset %s — refusing to continue' % (name,)) " done api_curl -u "sirius:${FORGEJO_TOKEN}" "$api/releases/$rid/assets" \ | python3 -c " import json,sys for a in json.load(sys.stdin): print('publish: asset ', a['name'], a['size'])"