diff --git a/.github/workflows/publish-release.yml b/.github/workflows/publish-release.yml new file mode 100644 index 0000000..12731a1 --- /dev/null +++ b/.github/workflows/publish-release.yml @@ -0,0 +1,128 @@ +name: Publish GitHub Release + +on: + pull_request: + paths: + - package.json + - CHANGELOG.md + - .github/workflows/publish-release.yml + push: + branches: [main] + paths: + - package.json + - CHANGELOG.md + - .github/workflows/publish-release.yml + workflow_dispatch: + +permissions: + contents: write + +concurrency: + group: publish-release + cancel-in-progress: false + +jobs: + publish: + name: publish-release + runs-on: ubuntu-24.04 + timeout-minutes: 5 + steps: + - name: Checkout full history + uses: actions/checkout@v5 + with: + fetch-depth: 0 + fetch-tags: true + + - name: Resolve release metadata + id: metadata + shell: bash + run: | + set -euo pipefail + + VERSION="$(node -p "require('./package.json').version")" + TAG="v${VERSION}" + + if ! [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + echo "::error::package.json version '${VERSION}' is not a stable semver release" + exit 1 + fi + + if ! grep -Fq "## [${VERSION}] - " CHANGELOG.md; then + echo "CHANGELOG has no finalized section for ${TAG}; publication is not ready yet." + echo "ready=false" >> "$GITHUB_OUTPUT" + exit 0 + fi + + RELEASE_SHA="$(git log --merges --fixed-strings --grep="release: ${TAG}" --format='%H' -n 1)" + + if [[ -z "$RELEASE_SHA" ]]; then + echo "::error::Could not find the merge commit for release: ${TAG}" + exit 1 + fi + + echo "version=${VERSION}" >> "$GITHUB_OUTPUT" + echo "tag=${TAG}" >> "$GITHUB_OUTPUT" + echo "release_sha=${RELEASE_SHA}" >> "$GITHUB_OUTPUT" + echo "ready=true" >> "$GITHUB_OUTPUT" + + - name: Extract CHANGELOG release notes + if: steps.metadata.outputs.ready == 'true' + env: + VERSION: ${{ steps.metadata.outputs.version }} + shell: bash + run: | + python3 - <<'PY' + import os + import re + from pathlib import Path + + version = os.environ["VERSION"] + text = Path("CHANGELOG.md").read_text(encoding="utf-8") + pattern = re.compile( + rf"^## \[{re.escape(version)}\] - \d{{4}}-\d{{2}}-\d{{2}}\n(.*?)(?=^## \[)", + re.MULTILINE | re.DOTALL, + ) + match = pattern.search(text) + if not match: + raise SystemExit(f"Could not extract CHANGELOG section for v{version}") + + notes = match.group(1).strip() + "\n" + Path("/tmp/release-notes.md").write_text(notes, encoding="utf-8") + PY + + - name: Publish tag and GitHub Release + if: steps.metadata.outputs.ready == 'true' && github.event_name != 'pull_request' + env: + GH_TOKEN: ${{ github.token }} + TAG: ${{ steps.metadata.outputs.tag }} + RELEASE_SHA: ${{ steps.metadata.outputs.release_sha }} + shell: bash + run: | + set -euo pipefail + + if gh release view "$TAG" --repo "$GITHUB_REPOSITORY" >/dev/null 2>&1; then + echo "GitHub Release $TAG already exists; nothing to do." + exit 0 + fi + + git fetch --tags --force + + if git rev-parse --verify --quiet "refs/tags/$TAG" >/dev/null; then + TAG_SHA="$(git rev-list -n 1 "$TAG")" + if [[ "$TAG_SHA" != "$RELEASE_SHA" ]]; then + echo "::error::Existing tag $TAG points to $TAG_SHA, expected $RELEASE_SHA" + exit 1 + fi + + gh release create "$TAG" \ + --repo "$GITHUB_REPOSITORY" \ + --verify-tag \ + --title "$TAG" \ + --notes-file /tmp/release-notes.md + else + gh release create "$TAG" \ + --repo "$GITHUB_REPOSITORY" \ + --target "$RELEASE_SHA" \ + --title "$TAG" \ + --notes-file /tmp/release-notes.md + fi