#!/usr/bin/env bash # 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. 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="$GITHUB_SERVER_URL/api/v1/repos/$GITHUB_REPOSITORY" 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 attached=$(curl -sS -H "$auth" "$api/releases/$id/assets" | jq -r '.[].name') for f in "$dist"/*; do [ -f "$f" ] || continue name=$(basename "$f") [ "$name" = RELEASE_NOTES.md ] && continue if printf '%s\n' "$attached" | grep -qxF "$name"; then echo "already attached: $name" continue fi echo "attaching $name" curl -sSf -X POST -H "$auth" -F "attachment=@$f" \ "$api/releases/$id/assets?name=$name" >/dev/null done echo "release $tag complete: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/releases/tag/$tag"