2026-09-10 13:10:56 -04:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
# Create-or-update a Forgejo release and (re)upload its assets.
|
|
|
|
|
#
|
|
|
|
|
# Usage: publish-release.sh <tag> <release-name> <body-file> <asset> [<asset>...]
|
|
|
|
|
# Env: FORGEJO_TOKEN user PAT with repo write (required)
|
|
|
|
|
# REPO_API default https://forgejo.siriusdevops.com/api/v1/repos/sirius/onionwire
|
2026-09-10 13:58:46 -04:00
|
|
|
# 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.
|
2026-09-10 13:10:56 -04:00
|
|
|
#
|
|
|
|
|
# Idempotent: re-running for the same tag reuses the release and replaces
|
|
|
|
|
# same-named assets instead of failing with 409.
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
2026-09-12 02:51:07 -04:00
|
|
|
# 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.
|
2026-09-10 14:08:39 -04:00
|
|
|
api_curl() {
|
2026-09-12 02:51:07 -04:00
|
|
|
curl -sS --http1.1 --retry 5 --retry-connrefused --retry-delay 3 \
|
2026-09-10 14:08:39 -04:00
|
|
|
--connect-timeout 20 --max-time 300 "$@"
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-12 02:51:07 -04:00
|
|
|
auth_json() {
|
2026-09-13 21:23:27 -04:00
|
|
|
<<<<<<< Updated upstream
|
2026-09-13 15:27:57 -04:00
|
|
|
api_curl -u "sirius:${FORGEJO_TOKEN}" -H "Content-Type: application/json" "$@"
|
2026-09-13 21:23:27 -04:00
|
|
|
=======
|
|
|
|
|
api_curl -u "sirius:${FORGEJO_TOKEN}" -H "Content-Type: application/json" "$@" | sed 's/\r$//'
|
|
|
|
|
>>>>>>> Stashed changes
|
2026-09-12 02:51:07 -04:00
|
|
|
}
|
|
|
|
|
|
2026-09-10 13:10:56 -04:00
|
|
|
tag="${1:?usage: publish-release.sh <tag> <name> <body-file> <asset>...}"
|
|
|
|
|
name="${2:?missing release name}"
|
|
|
|
|
body_file="${3:?missing body file}"
|
|
|
|
|
shift 3
|
|
|
|
|
|
|
|
|
|
: "${FORGEJO_TOKEN:?FORGEJO_TOKEN is not set}"
|
2026-09-12 02:51:07 -04:00
|
|
|
FORGEJO_TOKEN=$(printf '%s' "$FORGEJO_TOKEN" | tr -d '\r\n')
|
2026-09-10 13:10:56 -04:00
|
|
|
api="${REPO_API:-https://forgejo.siriusdevops.com/api/v1/repos/sirius/onionwire}"
|
2026-09-10 13:58:46 -04:00
|
|
|
target="${TARGET_COMMITISH:-}"
|
2026-09-10 13:10:56 -04:00
|
|
|
|
|
|
|
|
jqp() { python3 -c "import json,sys; d=json.load(sys.stdin); print($1)"; }
|
|
|
|
|
|
2026-09-12 02:51:07 -04:00
|
|
|
die_http() {
|
|
|
|
|
local what="$1" code="$2" file="$3"
|
|
|
|
|
echo "publish: $what HTTP $code" >&2
|
|
|
|
|
cat "$file" >&2 || true
|
|
|
|
|
echo >&2
|
|
|
|
|
exit 1
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-10 13:10:56 -04:00
|
|
|
# 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])
|
2026-09-10 13:58:46 -04:00
|
|
|
payload = {
|
2026-09-10 13:10:56 -04:00
|
|
|
"tag_name": sys.argv[2],
|
|
|
|
|
"name": sys.argv[4],
|
|
|
|
|
"body": body,
|
|
|
|
|
"draft": False,
|
|
|
|
|
"prerelease": False,
|
2026-09-10 13:58:46 -04:00
|
|
|
}
|
|
|
|
|
# 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))
|
2026-09-10 13:10:56 -04:00
|
|
|
PY
|
|
|
|
|
|
2026-09-12 02:51:07 -04:00
|
|
|
code=$(auth_json -o /tmp/release-rel.json -w '%{http_code}' \
|
|
|
|
|
"$api/releases/tags/$tag")
|
2026-09-10 13:10:56 -04:00
|
|
|
if [ "$code" = "404" ]; then
|
|
|
|
|
echo "publish: creating release $tag"
|
2026-09-12 02:51:07 -04:00
|
|
|
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
|
2026-09-10 13:10:56 -04:00
|
|
|
elif [ "$code" = "200" ]; then
|
|
|
|
|
echo "publish: release $tag exists, updating"
|
|
|
|
|
rid=$(jqp "d['id']" < /tmp/release-rel.json)
|
2026-09-12 02:51:07 -04:00
|
|
|
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
|
2026-09-10 13:10:56 -04:00
|
|
|
else
|
2026-09-12 02:51:07 -04:00
|
|
|
die_http "GET releases/tags/$tag" "$code" /tmp/release-rel.json
|
2026-09-10 13:10:56 -04:00
|
|
|
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; }
|
2026-09-10 14:20:45 -04:00
|
|
|
# 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
|
2026-09-10 13:10:56 -04:00
|
|
|
asset=$(basename "$f")
|
2026-09-10 14:20:45 -04:00
|
|
|
echo "publish: asset $asset is $bytes bytes on disk"
|
2026-09-13 15:27:57 -04:00
|
|
|
api_curl -u "sirius:${FORGEJO_TOKEN}" "$api/releases/$rid/assets" \
|
2026-09-10 13:10:56 -04:00
|
|
|
| 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)"
|
2026-09-12 02:51:07 -04:00
|
|
|
del=$(api_curl -o /tmp/release-adel.json -w '%{http_code}' \
|
2026-09-13 15:27:57 -04:00
|
|
|
-X DELETE -u "sirius:${FORGEJO_TOKEN}" \
|
2026-09-12 02:51:07 -04:00
|
|
|
"$api/releases/$rid/assets/$aid")
|
|
|
|
|
if [ "$del" != "204" ] && [ "$del" != "404" ]; then
|
|
|
|
|
die_http "DELETE asset $aid" "$del" /tmp/release-adel.json
|
|
|
|
|
fi
|
2026-09-10 13:10:56 -04:00
|
|
|
fi
|
|
|
|
|
done
|
2026-09-12 02:51:07 -04:00
|
|
|
up=$(api_curl -o /tmp/release-asset.json -w '%{http_code}' \
|
2026-09-13 21:23:27 -04:00
|
|
|
<<<<<<< Updated upstream
|
2026-09-12 02:51:07 -04:00
|
|
|
-X POST "$api/releases/$rid/assets?name=$asset" \
|
2026-09-13 15:27:57 -04:00
|
|
|
-u "sirius:${FORGEJO_TOKEN}" \
|
2026-09-13 21:23:27 -04:00
|
|
|
=======
|
|
|
|
|
-X POST -u "sirius:${FORGEJO_TOKEN}" "$api/releases/$rid/assets?name=$asset" \
|
|
|
|
|
>>>>>>> Stashed changes
|
2026-09-12 02:51:07 -04:00
|
|
|
-F "attachment=@$f")
|
|
|
|
|
if [ "$up" != "201" ]; then
|
|
|
|
|
die_http "upload $asset" "$up" /tmp/release-asset.json
|
|
|
|
|
fi
|
2026-09-10 13:10:56 -04:00
|
|
|
python3 -c "
|
2026-09-12 00:52:46 -04:00
|
|
|
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,))
|
|
|
|
|
"
|
2026-09-10 13:10:56 -04:00
|
|
|
done
|
|
|
|
|
|
2026-09-13 15:27:57 -04:00
|
|
|
api_curl -u "sirius:${FORGEJO_TOKEN}" "$api/releases/$rid/assets" \
|
2026-09-10 13:10:56 -04:00
|
|
|
| python3 -c "
|
|
|
|
|
import json,sys
|
|
|
|
|
for a in json.load(sys.stdin):
|
|
|
|
|
print('publish: asset ', a['name'], a['size'])"
|