diff --git a/.github/workflows/publish-merged-release.yml b/.github/workflows/publish-merged-release.yml new file mode 100644 index 0000000..19a427a --- /dev/null +++ b/.github/workflows/publish-merged-release.yml @@ -0,0 +1,32 @@ +name: Publish merged release + +on: + pull_request: + branches: [main] + types: [closed] + +permissions: + contents: write + +jobs: + publish: + if: "${{ github.event.pull_request.merged && startsWith(github.event.pull_request.title, 'chore(release): ') }}" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5 + with: + ref: ${{ github.event.pull_request.merge_commit_sha }} + fetch-depth: 0 + - name: Publish tag and release + env: + GH_TOKEN: ${{ github.token }} + PR_TITLE: ${{ github.event.pull_request.title }} + run: | + set -euo pipefail + version="${PR_TITLE#chore(release): }" + version="${version%% *}" + python3 scripts/release.py validate-prepared --version "$version" + git tag "v$version" + git push origin "v$version" + python3 scripts/release.py release-notes --version "$version" > release-notes.md + gh release create "v$version" --title "v$version" --notes-file release-notes.md diff --git a/.github/workflows/publish-release.yml b/.github/workflows/publish-release.yml index b3c2b71..6cf5bce 100644 --- a/.github/workflows/publish-release.yml +++ b/.github/workflows/publish-release.yml @@ -61,13 +61,14 @@ jobs: if git rev-parse -q --verify "refs/tags/v$RELEASE_VERSION" >/dev/null; then tag_commit=$(git rev-list -n 1 "v$RELEASE_VERSION") subject=$(git show -s --format=%s "$tag_commit") - if [ "$tag_commit" != "$(git rev-parse HEAD)" ] || [ "$subject" != "chore(release): $RELEASE_VERSION" ]; then + expected="chore(release): $RELEASE_VERSION" + if [ "$tag_commit" != "$(git rev-parse HEAD)" ] || { [ "$subject" != "$expected" ] && [ "${subject%% (#*}" != "$expected" ]; }; then echo '::error::Existing tag does not match this default-branch release commit; recover manually.' exit 1 fi echo 'resuming=true' >> "$GITHUB_OUTPUT" gh release view "v$RELEASE_VERSION" --json url --jq '.url' 2>/dev/null | xargs -r -I{} echo "release-url={}" >> "$GITHUB_OUTPUT" || true - elif [ "$(git show -s --format=%s HEAD)" = "chore(release): $RELEASE_VERSION" ]; then + elif subject=$(git show -s --format=%s HEAD) && { [ "$subject" = "chore(release): $RELEASE_VERSION" ] || [ "${subject%% (#*}" = "chore(release): $RELEASE_VERSION" ]; }; then python3 .shared-workflows/scripts/release.py validate-prepared --version "$RELEASE_VERSION" echo 'resuming=true' >> "$GITHUB_OUTPUT" fi @@ -98,9 +99,14 @@ jobs: git diff --cached --quiet && { echo '::error::No release changes to commit'; exit 1; } git -c user.name='github-actions[bot]' -c user.email='41898282+github-actions[bot]@users.noreply.github.com' commit -m "chore(release): $RELEASE_VERSION" branch="release/v$RELEASE_VERSION" - git ls-remote --exit-code --heads origin "$branch" && exit 1 || true - git push origin "HEAD:refs/heads/$branch" - gh pr create --base "$DEFAULT_BRANCH" --head "$branch" --title "chore(release): $RELEASE_VERSION" --body 'Prepared release; review and merge before publishing the tag.' + remote_commit=$(git ls-remote origin "refs/heads/$branch" | cut -f1) + if [ -n "$remote_commit" ]; then + test "$remote_commit" = "$(git rev-parse HEAD)" + else + git push origin "HEAD:refs/heads/$branch" + fi + gh pr list --head "$branch" --state open --json url --jq '.[0].url' | grep -q . || \ + gh pr create --base "$DEFAULT_BRANCH" --head "$branch" --title "chore(release): $RELEASE_VERSION" --body 'Prepared release; review and merge before publishing the tag.' - name: Create GitHub Release from changelog notes if: ${{ steps.state.outputs.resuming }} id: release diff --git a/CHANGELOG.md b/CHANGELOG.md index 40f27bc..fb19318 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,3 +13,4 @@ All notable changes to these reusable GitHub Actions workflows are recorded here ### Fixed - Prepare releases through a pull request before publishing their tag and GitHub Release. +- Publish the tag and GitHub Release automatically when a release PR merges.