From da41fcf574565b816b50543b45dcfd5e167946b7 Mon Sep 17 00:00:00 2001 From: samuido <176191237+rebuildup@users.noreply.github.com> Date: Wed, 16 Sep 2026 23:05:59 +0900 Subject: [PATCH 1/2] ci(release): publish finalized GitHub releases --- .github/workflows/publish-release.yml | 123 ++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 .github/workflows/publish-release.yml diff --git a/.github/workflows/publish-release.yml b/.github/workflows/publish-release.yml new file mode 100644 index 0000000..a39cf48 --- /dev/null +++ b/.github/workflows/publish-release.yml @@ -0,0 +1,123 @@ +name: Publish GitHub Release + +on: + 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 -Eq "^## \\\[${VERSION}\\\] - [0-9]{4}-[0-9]{2}-[0-9]{2}$" 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' + 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 From e6b3c3a7f33aa3bff5b5fab8d58d0776bb44a2fd Mon Sep 17 00:00:00 2001 From: samuido <176191237+rebuildup@users.noreply.github.com> Date: Wed, 16 Sep 2026 23:07:22 +0900 Subject: [PATCH 2/2] ci(release): validate publication workflow on PR --- .github/workflows/publish-release.yml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/.github/workflows/publish-release.yml b/.github/workflows/publish-release.yml index a39cf48..12731a1 100644 --- a/.github/workflows/publish-release.yml +++ b/.github/workflows/publish-release.yml @@ -1,6 +1,11 @@ name: Publish GitHub Release on: + pull_request: + paths: + - package.json + - CHANGELOG.md + - .github/workflows/publish-release.yml push: branches: [main] paths: @@ -42,7 +47,7 @@ jobs: exit 1 fi - if ! grep -Eq "^## \\\[${VERSION}\\\] - [0-9]{4}-[0-9]{2}-[0-9]{2}$" CHANGELOG.md; then + 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 @@ -86,7 +91,7 @@ jobs: PY - name: Publish tag and GitHub Release - if: steps.metadata.outputs.ready == 'true' + if: steps.metadata.outputs.ready == 'true' && github.event_name != 'pull_request' env: GH_TOKEN: ${{ github.token }} TAG: ${{ steps.metadata.outputs.tag }}