#!/usr/bin/env bash # Creates the Forgejo release associated with the tag version-x.y.z. Description: the message of the tag if it is # annotated, otherwise (lightweight tag created by tag.sh) the file $RELEASE_NOTES_DIR/version-x.y.z.md of the # working directory, which must be the checkout of the tag. Meant for the CI; idempotent: an already existing release is # reused, and an attached file that is already present is not sent again. # # Variables (provided by the workflow): # FORGETOKEN Forgejo API token (write access to releases) # TAG tag to publish (e.g. version-0.1.4) # SERVER Forgejo server URL (e.g. https://forge.example.org) # REPO owner/repository (e.g. org/crate) # ASSET optional: file to attach to the release (e.g. dist/crate-kit-0.1.4.tar.gz) set -euo pipefail . "$(dirname "$0")/common.sh" : "${FORGETOKEN:?FORGETOKEN required}" "${TAG:?TAG required}" "${SERVER:?SERVER required}" "${REPO:?REPO required}" command -v jq >/dev/null || { err "✗ jq is required"; exit 1; } api="$SERVER/api/v1/repos/$REPO" auth="Authorization: token $FORGETOKEN" if release_json=$(curl -sf -H "$auth" "$api/releases/tags/$TAG"); then warn " The release $TAG already exists: reused" else # Message of the annotated tag (object of type "tag"); empty for a lightweight tag. ref_json=$(forge_call "reading tag $TAG" -H "$auth" "$api/git/refs/tags/$TAG") || exit 1 obj_sha=$(echo "$ref_json" | jq -r '.[0]?.object.sha // empty') obj_type=$(echo "$ref_json" | jq -r '.[0]?.object.type // empty') body="" if [ "$obj_type" = "tag" ]; then tag_json=$(forge_call "reading the message of tag $TAG" -H "$auth" "$api/git/tags/$obj_sha") || exit 1 body=$(echo "$tag_json" | jq -r '.message // empty') fi # Lightweight tag (tag.sh, jj only): the notes are the file committed in the tagged revision, read in the # working directory (the workflow runs in the checkout of the tag). if [ -z "$body" ] && [ -f "$RELEASE_NOTES_DIR/$TAG.md" ]; then body=$(cat "$RELEASE_NOTES_DIR/$TAG.md") fi release_json=$(forge_call "creating release $TAG" -X POST "$api/releases" -H "$auth" \ -H "Content-Type: application/json" \ -d "$(jq -n --arg tag "$TAG" --arg body "$body" '{tag_name: $tag, name: $tag, body: $body}')") || exit 1 ok "✓ Release $TAG created" fi if [ -n "${ASSET:-}" ]; then [ -f "$ASSET" ] || { err "✗ file to attach not found: $ASSET"; exit 1; } release_id=$(echo "$release_json" | jq -r '.id') name=$(basename "$ASSET") if echo "$release_json" | jq -e --arg n "$name" '.assets[]? | select(.name == $n)' >/dev/null; then warn " $name is already attached to the release: nothing to do" else forge_call "uploading $name to release $TAG" -X POST "$api/releases/$release_id/assets?name=$name" \ -H "$auth" -F "attachment=@$ASSET" >/dev/null || exit 1 ok "✓ $name attached to release $TAG" fi fi