#!/usr/bin/env bash # Author: Guoliang Chen, a Senior Digital Design Engineer # Create (or reuse) the Gitea release for a tag and attach every artifact. # # Usage: tools/publish_release.sh # # Written for the release workflow: expects the CI-provided environment # (GITHUB_SERVER_URL, GITHUB_REPOSITORY, GITHUB_TOKEN) and a dist directory # holding the artifacts plus RELEASE_NOTES.md (the release body; not # attached). A tag containing "-" (v0.8.0-rc1) is marked prerelease. # Re-running is safe: an existing release for the tag is reused, and an # asset name that is already attached is skipped. # # Two things learned from the 0.29.0 / 0.30.0 releases (2026-09-02), whose # uploads died with `curl: (56) ... error: 524` -- Cloudflare's origin-timeout # page, because $GITHUB_SERVER_URL is the public host and every request goes # through Cloudflare, which gives an origin 100 seconds to answer: # * LUCISV_API_URL, when set (the workflow passes the repository's Actions # variable of that name), is used for the API instead of # $GITHUB_SERVER_URL -- a direct http://host:port the runner can reach # without the proxy. The release link printed at the end still uses the # public URL. # * Every upload is retried (UPLOAD_ATTEMPTS, default 5, backing off), and # before each retry the server is asked again what is attached: a 524 is # Cloudflare giving up on the *response*, and the origin may well have # finished storing the file. An asset present with the local file's # size is treated as done; one present with a different size is deleted # and sent again, so a truncated upload can never be published. set -eu [ $# -eq 2 ] || { echo "usage: $0 " >&2; exit 2; } tag=$1 dist=$2 for var in GITHUB_SERVER_URL GITHUB_REPOSITORY GITHUB_TOKEN; do eval "[ -n \"\${$var:-}\" ]" || { echo "$var is not set" >&2; exit 2; } done notes="$dist/RELEASE_NOTES.md" [ -s "$notes" ] || { echo "no release notes at $notes" >&2; exit 2; } api="${LUCISV_API_URL:-$GITHUB_SERVER_URL}/api/v1/repos/$GITHUB_REPOSITORY" [ -n "${LUCISV_API_URL:-}" ] && echo "using the direct API URL $LUCISV_API_URL" attempts=${UPLOAD_ATTEMPTS:-5} auth="Authorization: token $GITHUB_TOKEN" prerelease=false case $tag in *-*) prerelease=true ;; esac # Reuse the release if a re-run already created it, else create it. id=$(curl -sS -H "$auth" "$api/releases/tags/$tag" | jq -r '.id // empty') if [ -n "$id" ]; then echo "release for $tag exists (id $id), reusing" else payload=$(jq -n --arg tag "$tag" --rawfile body "$notes" --argjson pre "$prerelease" \ '{tag_name: $tag, name: $tag, body: $body, draft: false, prerelease: $pre}') id=$(curl -sSf -X POST -H "$auth" -H 'Content-Type: application/json' \ -d "$payload" "$api/releases" | jq -r .id) echo "created release $tag (id $id)" fi # What the server holds for this release, as "name size" lines. attached_assets() { curl -sS -H "$auth" "$api/releases/$id/assets" \ | jq -r '.[] | "\(.name) \(.size) \(.id)"' } # Attach one file, or confirm the server already has it whole. Returns 0 # once an asset with the file's name and size is on the server. attach() { f=$1 name=$(basename "$f") size=$(wc -c < "$f" | tr -d ' ') n=0 while :; do have=$(attached_assets | awk -v n="$name" '$1 == n { print $2, $3 }') if [ -n "$have" ]; then have_size=${have%% *} have_id=${have##* } if [ "$have_size" = "$size" ]; then [ $n -eq 0 ] && echo "already attached: $name" || echo "attached after retry: $name" return 0 fi echo "server holds $name at $have_size bytes, local is $size: deleting the partial upload" >&2 curl -sSf -X DELETE -H "$auth" "$api/releases/$id/assets/$have_id" >/dev/null || true fi n=$((n + 1)) [ $n -le "$attempts" ] || { echo "giving up on $name after $attempts attempts" >&2; return 1; } [ $n -eq 1 ] && echo "attaching $name" || echo "attaching $name (attempt $n of $attempts)" if curl -sSf -X POST -H "$auth" -F "attachment=@$f" \ "$api/releases/$id/assets?name=$name" >/dev/null; then return 0 fi # Back off before asking the server again; a proxy timeout says nothing # about whether the origin finished. sleep $((10 * n)) done } for f in "$dist"/*; do [ -f "$f" ] || continue [ "$(basename "$f")" = RELEASE_NOTES.md ] && continue attach "$f" done echo "release $tag complete: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/releases/tag/$tag" # Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com