name: vociferate/publish description: > Extract release notes from the changelog and create or update a Gitea/GitHub release. The repository must be checked out at the release tag before this action runs. inputs: token: description: > Token used to authenticate release API calls. Defaults to the workflow token. required: false default: '' version: description: > Semantic version to publish (with or without leading v). When omitted, derived from the current git tag ref. required: false default: '' changelog: description: Path to changelog file relative to repository root. required: false default: changelog.md outputs: release-id: description: Numeric ID of the created or updated release. value: ${{ steps.create-release.outputs.id }} tag: description: The tag used for the release (e.g. v1.2.3). value: ${{ steps.resolve-version.outputs.tag }} version: description: The bare version string without leading v (e.g. 1.2.3). value: ${{ steps.resolve-version.outputs.version }} runs: using: composite steps: - name: Resolve release version id: resolve-version shell: bash env: INPUT_VERSION: ${{ inputs.version }} GITHUB_REF_VALUE: ${{ github.ref }} run: | set -euo pipefail provided="$(printf '%s' "${INPUT_VERSION:-}" | sed 's/^[[:space:]]\+//; s/[[:space:]]\+$//')" if [[ -n "$provided" ]]; then normalized="${provided#v}" tag="v${normalized}" elif [[ "${GITHUB_REF_VALUE}" == refs/tags/* ]]; then tag="${GITHUB_REF_VALUE#refs/tags/}" normalized="${tag#v}" elif head_tag="$(git describe --exact-match --tags HEAD 2>/dev/null)" && [[ -n "$head_tag" ]]; then tag="$head_tag" normalized="${tag#v}" else echo "A version input is required when the workflow is not running from a tag push" >&2 exit 1 fi echo "tag=$tag" >> "$GITHUB_OUTPUT" echo "version=$normalized" >> "$GITHUB_OUTPUT" - name: Extract release notes from changelog id: extract-notes shell: bash env: CHANGELOG: ${{ inputs.changelog != '' && inputs.changelog || 'changelog.md' }} RELEASE_VERSION: ${{ steps.resolve-version.outputs.version }} RUNNER_TEMP: ${{ runner.temp }} run: | set -euo pipefail release_notes="$(awk -v version="$RELEASE_VERSION" ' $0 ~ "^## \\[" version "\\]" {capture=1} capture { if ($0 ~ "^## \\[" && $0 !~ "^## \\[" version "\\]") exit print } ' "$CHANGELOG")" if [[ -z "${release_notes//[[:space:]]/}" ]]; then echo "Release notes section for ${RELEASE_VERSION} was not found in ${CHANGELOG}" >&2 exit 1 fi notes_file="${RUNNER_TEMP}/release-notes.md" printf '%s\n' "$release_notes" > "$notes_file" echo "notes_file=$notes_file" >> "$GITHUB_OUTPUT" - name: Create or update release id: create-release shell: bash env: TOKEN: ${{ inputs.token != '' && inputs.token || github.token }} TAG_NAME: ${{ steps.resolve-version.outputs.tag }} RELEASE_NOTES_FILE: ${{ steps.extract-notes.outputs.notes_file }} GITHUB_API_URL: ${{ github.api_url }} GITHUB_SERVER_URL: ${{ github.server_url }} GITHUB_SHA: ${{ github.sha }} GITHUB_REPOSITORY: ${{ github.repository }} run: | set -euo pipefail release_notes="$(cat "$RELEASE_NOTES_FILE")" escaped_release_notes="$(printf '%s' "$release_notes" | sed 's/\\/\\\\/g; s/"/\\"/g; :a;N;$!ba;s/\n/\\n/g')" release_api="${GITHUB_API_URL:-${GITHUB_SERVER_URL%/}/api/v1}/repos/${GITHUB_REPOSITORY}/releases" release_by_tag_api="${release_api}/tags/${TAG_NAME}" status_code="$(curl -sS -o release-existing.json -w '%{http_code}' \ -H "Authorization: token ${TOKEN}" \ -H "Content-Type: application/json" \ "${release_by_tag_api}")" if [[ "$status_code" == "200" ]]; then existing_release_id="$(sed -n 's/.*"id"[[:space:]]*:[[:space:]]*\([0-9][0-9]*\).*/\1/p' release-existing.json | head -n 1)" if [[ -z "$existing_release_id" ]]; then echo "Failed to parse existing release id for ${TAG_NAME}" >&2 cat release-existing.json >&2 exit 1 fi curl --fail-with-body \ -X PATCH \ -H "Authorization: token ${TOKEN}" \ -H "Content-Type: application/json" \ "${release_api}/${existing_release_id}" \ --data "{\"tag_name\":\"${TAG_NAME}\",\"target\":\"${GITHUB_SHA}\",\"name\":\"${TAG_NAME}\",\"body\":\"${escaped_release_notes}\",\"draft\":false,\"prerelease\":false}" \ --output release.json echo "id=$existing_release_id" >> "$GITHUB_OUTPUT" elif [[ "$status_code" != "404" ]]; then echo "Unexpected response while checking release ${TAG_NAME}: HTTP ${status_code}" >&2 cat release-existing.json >&2 exit 1 else curl --fail-with-body \ -X POST \ -H "Authorization: token ${TOKEN}" \ -H "Content-Type: application/json" \ "${release_api}" \ --data "{\"tag_name\":\"${TAG_NAME}\",\"target\":\"${GITHUB_SHA}\",\"name\":\"${TAG_NAME}\",\"body\":\"${escaped_release_notes}\",\"draft\":false,\"prerelease\":false}" \ --output release.json release_id="$(sed -n 's/.*"id"[[:space:]]*:[[:space:]]*\([0-9][0-9]*\).*/\1/p' release.json | head -n 1)" if [[ -z "$release_id" ]]; then echo "Failed to parse release id from API response" >&2 cat release.json >&2 exit 1 fi echo "id=$release_id" >> "$GITHUB_OUTPUT" fi