Skip to content

Commit e435dd4

Browse files
Publish merged releases from default branch pushes.
Signed-off-by: Samuel Williams <samuel.williams@shopify.com>
1 parent 824b3fc commit e435dd4

14 files changed

Lines changed: 270 additions & 47 deletions

File tree

‎.github/workflows/release-publish.yaml‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,28 @@
11
name: Publish release
22

3-
# Only merged source is executed. Validation of unmerged PRs lives in its own
4-
# read-only pull_request workflow. This trigger also supports reviewed fork PRs.
3+
# Inspect the exact pushed commit and publish only a validated, merged release PR.
54
on:
6-
pull_request_target:
7-
types: [closed]
5+
push:
86
branches: ["main"]
97

108
permissions:
119
contents: read
1210
pull-requests: read
1311

14-
concurrency:
15-
group: release-publish
16-
cancel-in-progress: false
17-
1812
env:
1913
BUNDLE_WITH: maintenance
20-
RELEASE_PR: ${{ github.event.pull_request.number }}
2114

2215
jobs:
2316
inspect:
24-
if: github.event.pull_request.merged == true
2517
runs-on: ubuntu-latest
2618
outputs:
2719
release: ${{ steps.inspect.outputs.release }}
2820
commit: ${{ steps.inspect.outputs.commit }}
21+
pull_request: ${{ steps.inspect.outputs.pull_request }}
2922
steps:
3023
- uses: actions/checkout@v7
3124
with:
25+
ref: ${{ github.sha }}
3226
fetch-depth: 0
3327
persist-credentials: false
3428
- uses: ruby/setup-ruby@v1
@@ -38,13 +32,20 @@ jobs:
3832
- id: inspect
3933
env:
4034
GH_TOKEN: ${{ github.token }}
35+
RELEASE_COMMIT: ${{ github.sha }}
4136
run: bundle exec bake gem:github:release:resolve
4237

4338
publish:
4439
needs: inspect
4540
if: needs.inspect.outputs.release == 'true'
4641
runs-on: ubuntu-latest
4742
environment: rubygems
43+
concurrency:
44+
group: release-publish
45+
cancel-in-progress: false
46+
queue: max
47+
env:
48+
RELEASE_PR: ${{ needs.inspect.outputs.pull_request }}
4849
permissions:
4950
contents: write
5051
pull-requests: read
@@ -55,8 +56,6 @@ jobs:
5556
- uses: actions/checkout@v7
5657
with:
5758
# Inspection verified this merged commit belongs to the default branch.
58-
# Checkout v7 also blocks merged fork PRs without this explicit opt-in.
59-
allow-unsafe-pr-checkout: true
6059
ref: ${{ needs.inspect.outputs.commit }}
6160
fetch-depth: 0
6261
persist-credentials: false

‎bake/gem/github/release.rb‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,25 @@ def major(refresh: false)
2626
Bake::Gem::GitHub::Project.new(context.root).prepare(context, "major", refresh: refresh)
2727
end
2828

29-
# Resolve and validate a merged PR, emitting a commit output for the publishing job.
30-
# @parameter number [String] The merged PR number; defaults to `RELEASE_PR`.
29+
# Resolve and validate a pushed commit, emitting its merged PR and commit for publishing.
30+
# @parameter number [String | Nil] A merged PR number for older workflows; defaults to `RELEASE_PR`.
31+
# @parameter commit [String | Nil] The pushed commit SHA; defaults to `RELEASE_COMMIT` and takes precedence over `number`.
3132
# @returns [Hash | Nil] Release metadata, or nil for an ordinary PR.
32-
def resolve(number: ENV.fetch("RELEASE_PR"))
33-
result = Bake::Gem::GitHub::Project.new(context.root).inspect_release(number)
33+
def resolve(number: ENV["RELEASE_PR"], commit: ENV["RELEASE_COMMIT"])
34+
project = Bake::Gem::GitHub::Project.new(context.root)
35+
result = commit ? project.inspect_commit(commit) : project.inspect_release(number)
3436

3537
if path = ENV["GITHUB_OUTPUT"]
3638
File.open(path, "a") do |file|
3739
file.puts "release=#{!result.nil?}"
38-
file.puts "commit=#{result.fetch(:commit)}" if result
40+
if result
41+
file.puts "commit=#{result.fetch(:commit)}"
42+
file.puts "pull_request=#{result.fetch(:pull_request)}"
43+
end
3944
end
4045
end
4146

42-
return result
47+
result
4348
end
4449

4550
# Build or restore the exact artifact for a merged release PR.

‎context/getting-started.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This guide explains how to configure reviewed Ruby gem releases and prepare the
44

55
## How releases work
66

7-
Maintainers prepare a release PR containing the version bump and generated release notes. CI regenerates those changes from the current base to verify the content. Native GitHub rules control approval and merging; after merge, GitHub Actions builds the exact merged commit and publishes its verified artifact to RubyGems.
7+
Maintainers prepare a release PR containing the version bump and generated release notes. CI regenerates those changes from the current base to verify the content. Native GitHub rules control approval and merging. A push to the default branch starts release inspection; GitHub Actions builds the exact pushed commit only when it is a validated, merged release PR, then publishes its verified artifact to RubyGems.
88

99
`bake-gem` provides version updates, release hooks, and clean builds. `bake-gem-github` adds PR preparation, GitHub policy, and remote publishing. The supported process uses one gemspec, stable three-part versions, merge or squash merging, and RubyGems.org.
1010

@@ -111,6 +111,8 @@ git diff
111111

112112
This updates managed files in the working tree and returns their changed paths. Review the diff and selectively retain repository customizations before committing. The task does not stage, commit, or change remote settings. Repeated updates produce no further changes unless customizations differ from the templates. Apply changed rulesets after the corresponding workflows are running.
113113

114+
Regenerate existing workflows to adopt publishing on `push` instead of `pull_request_target`. The workflow filename and `rubygems` environment remain the same, so the RubyGems Trusted Publisher configuration does not change. No exception to GitHub's `pull_request_target` execution policy is needed. The resolve task continues to accept PR numbers from older workflows while you migrate.
115+
114116
The release workflows follow `bake modernize` action versions and use moving major tags where available. The RubyGems credentials action uses its [documented `@main` reference](https://github.com/rubygems/configure-rubygems-credentials#trusted-publisher-recommended). Repositories that require fixed revisions can customize these references.
115117

116118
## Current scope

‎context/preparing-releases.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ Replace `patch` with `minor` or `major`. The wrapper fetches the default branch
2121

2222
All release changes belong in the PR. Core preparation commits additions and deletions from release hooks but never pushes, tags or publishes. Validation independently generates the expected tree from the current base. A changed base SHA alone is fine; changed generated notes are not. Ordinary PRs with no version change pass release validation and still build unsigned.
2323

24+
## Publish the merged release
25+
26+
Merging into the configured default branch triggers `release-publish.yaml` through its `push` event. Inspection uses the exact pushed SHA and resolves its associated PR through GitHub. Publication requires one matching merged PR in this repository, targeting the configured branch, with that exact merge commit. Ordinary changes do not publish; release changes without a matching merged PR fail inspection. Both merge commits and squash merges are supported, including merged fork PRs.
27+
28+
Each release must land as the tip of its own push, as it does when merging a PR through GitHub. Later pushes do not change a pending release's source: inspection and publishing remain pinned to the original commit, and reruns use the same event. Do not combine a release and later changes into one direct push. If you automate merging, use a GitHub App or personal access token; pushes made using a workflow's `GITHUB_TOKEN` do not trigger another workflow.
29+
30+
Only validated releases enter the publishing queue and request approval from the `rubygems` environment when required. Ordinary pushes cannot replace them in that queue. The publishing job retains the existing signing, attestation, and artifact recovery checks.
31+
2432
## Resume interrupted preparation
2533

2634
If preparation stops after creating or pushing the release branch, return to the current default branch and repeat the same command. The existing branch is validated and reused, so retries do not create a second version bump or PR. Resolve any uncommitted changes before switching branches.

‎context/recovering-releases.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ A rerun can recover an interrupted individual asset upload once `release.tar` is
2424

2525
## Understand workflow reruns
2626

27-
GitHub concurrency does not guarantee a durable FIFO queue: rerun any publishing run displaced while pending. Resume reruns all jobs, including integrity checks; it does not repeat or second-guess the native review policy or a permitted administrator bypass. Older publishing runs execute their original code; adding this recovery support to the default branch does not change an already-triggered workflow.
27+
Only publishing jobs share a concurrency group. They use `queue: max`, allowing up to 100 pending publishing jobs without replacing earlier ones; ordinary pushes only run inspection and do not enter this queue. GitHub cancels additional jobs if that limit is reached. Rerun a canceled publishing workflow after capacity becomes available.
28+
29+
Resume reruns all jobs against the original pushed commit, including integrity checks; it does not repeat or second-guess the native review policy or a permitted administrator bypass. Older publishing runs execute their original code and concurrency policy; updating the default branch does not change an already-triggered workflow.

‎guides/getting-started/readme.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This guide explains how to configure reviewed Ruby gem releases and prepare the
44

55
## How releases work
66

7-
Maintainers prepare a release PR containing the version bump and generated release notes. CI regenerates those changes from the current base to verify the content. Native GitHub rules control approval and merging; after merge, GitHub Actions builds the exact merged commit and publishes its verified artifact to RubyGems.
7+
Maintainers prepare a release PR containing the version bump and generated release notes. CI regenerates those changes from the current base to verify the content. Native GitHub rules control approval and merging. A push to the default branch starts release inspection; GitHub Actions builds the exact pushed commit only when it is a validated, merged release PR, then publishes its verified artifact to RubyGems.
88

99
`bake-gem` provides version updates, release hooks, and clean builds. `bake-gem-github` adds PR preparation, GitHub policy, and remote publishing. The supported process uses one gemspec, stable three-part versions, merge or squash merging, and RubyGems.org.
1010

@@ -111,6 +111,8 @@ git diff
111111

112112
This updates managed files in the working tree and returns their changed paths. Review the diff and selectively retain repository customizations before committing. The task does not stage, commit, or change remote settings. Repeated updates produce no further changes unless customizations differ from the templates. Apply changed rulesets after the corresponding workflows are running.
113113

114+
Regenerate existing workflows to adopt publishing on `push` instead of `pull_request_target`. The workflow filename and `rubygems` environment remain the same, so the RubyGems Trusted Publisher configuration does not change. No exception to GitHub's `pull_request_target` execution policy is needed. The resolve task continues to accept PR numbers from older workflows while you migrate.
115+
114116
The release workflows follow `bake modernize` action versions and use moving major tags where available. The RubyGems credentials action uses its [documented `@main` reference](https://github.com/rubygems/configure-rubygems-credentials#trusted-publisher-recommended). Repositories that require fixed revisions can customize these references.
115117

116118
## Current scope

‎guides/preparing-releases/readme.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ Replace `patch` with `minor` or `major`. The wrapper fetches the default branch
2121

2222
All release changes belong in the PR. Core preparation commits additions and deletions from release hooks but never pushes, tags or publishes. Validation independently generates the expected tree from the current base. A changed base SHA alone is fine; changed generated notes are not. Ordinary PRs with no version change pass release validation and still build unsigned.
2323

24+
## Publish the merged release
25+
26+
Merging into the configured default branch triggers `release-publish.yaml` through its `push` event. Inspection uses the exact pushed SHA and resolves its associated PR through GitHub. Publication requires one matching merged PR in this repository, targeting the configured branch, with that exact merge commit. Ordinary changes do not publish; release changes without a matching merged PR fail inspection. Both merge commits and squash merges are supported, including merged fork PRs.
27+
28+
Each release must land as the tip of its own push, as it does when merging a PR through GitHub. Later pushes do not change a pending release's source: inspection and publishing remain pinned to the original commit, and reruns use the same event. Do not combine a release and later changes into one direct push. If you automate merging, use a GitHub App or personal access token; pushes made using a workflow's `GITHUB_TOKEN` do not trigger another workflow.
29+
30+
Only validated releases enter the publishing queue and request approval from the `rubygems` environment when required. Ordinary pushes cannot replace them in that queue. The publishing job retains the existing signing, attestation, and artifact recovery checks.
31+
2432
## Resume interrupted preparation
2533

2634
If preparation stops after creating or pushing the release branch, return to the current default branch and repeat the same command. The existing branch is validated and reused, so retries do not create a second version bump or PR. Resolve any uncommitted changes before switching branches.

‎guides/recovering-releases/readme.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ A rerun can recover an interrupted individual asset upload once `release.tar` is
2424

2525
## Understand workflow reruns
2626

27-
GitHub concurrency does not guarantee a durable FIFO queue: rerun any publishing run displaced while pending. Resume reruns all jobs, including integrity checks; it does not repeat or second-guess the native review policy or a permitted administrator bypass. Older publishing runs execute their original code; adding this recovery support to the default branch does not change an already-triggered workflow.
27+
Only publishing jobs share a concurrency group. They use `queue: max`, allowing up to 100 pending publishing jobs without replacing earlier ones; ordinary pushes only run inspection and do not enter this queue. GitHub cancels additional jobs if that limit is reached. Rerun a canceled publishing workflow after capacity becomes available.
28+
29+
Resume reruns all jobs against the original pushed commit, including integrity checks; it does not repeat or second-guess the native review policy or a permitted administrator bypass. Older publishing runs execute their original code and concurrency policy; updating the default branch does not change an already-triggered workflow.

‎lib/bake/gem/github/project.rb‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,12 @@ def merged(number)
111111

112112
# Resolve release identity; ordinary merged PRs do not publish.
113113
# @parameter number [String | Integer] The merged PR number.
114+
# @parameter commit [String | Nil] The expected merge commit, when resolving a push event.
114115
# @returns [Hash | Nil] Release metadata with symbol keys, or nil for an ordinary PR. Includes `name`, `version`, `commit`, `base`, `bump`, `repository`, `pull_request`, `merged_by`, and `pull_request_url`.
115116
# @raises [RuntimeError] If the merged source does not match the independently generated release.
116-
def inspect_release(number)
117+
def inspect_release(number, commit: nil)
117118
pr = merged(number)
119+
raise "PR merge commit does not match the pushed commit." if commit && pr.fetch("merge_commit_sha") != commit
118120

119121
commit = pr.fetch("merge_commit_sha")
120122
metadata = @release.validate(base: "#{commit}^1", candidate: commit, optional: true)
@@ -128,6 +130,31 @@ def inspect_release(number)
128130
end
129131
end
130132

133+
# Resolve a pushed release commit to the PR which merged it into the configured branch.
134+
# @parameter commit [String] The full commit SHA from the push event.
135+
# @returns [Hash | Nil] Release metadata from {inspect_release}, or nil for an ordinary change.
136+
# @raises [RuntimeError] If the commit is invalid or a release has no unique matching merged PR.
137+
# @raises [Bake::Gem::CommandExecutionError] If Git or GitHub cannot verify the release.
138+
def inspect_commit(commit)
139+
raise "Expected a full pushed commit SHA." unless commit.match?(/\A(?:[0-9a-f]{40}|[0-9a-f]{64})\z/)
140+
responses = readlines(
141+
"gh", "api", "repos/#{@repository}/commits/#{commit}/pulls?per_page=100",
142+
"--paginate", "--slurp", chdir: @root,
143+
)
144+
pulls = JSON.parse(responses.join).flatten(1).select do |pr|
145+
pr["merged_at"] && pr["merge_commit_sha"] == commit &&
146+
pr.dig("base", "ref") == @config.fetch("branch") && pr.dig("base", "repo", "full_name") == @repository
147+
end
148+
149+
if pulls.empty?
150+
return nil unless @release.validate(base: "#{commit}^1", candidate: commit, optional: true)
151+
raise "Release commit has no matching merged PR."
152+
end
153+
raise "Multiple merged PRs match the pushed commit." if pulls.size > 1
154+
155+
return inspect_release(pulls.first.fetch("number"), commit: commit)
156+
end
157+
131158
# Return a read-only comparison of managed settings and current repository settings.
132159
# @returns [Hash] Desired rules, existing rules, environments, optional environment changes, and expected Trusted Publisher settings. This does not verify RubyGems ownership or publisher configuration.
133160
def doctor

‎releases.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Publish validated release PRs from pushes to the default branch, retaining the exact merged commit and environment approval without requiring `pull_request_target`.
6+
- Queue publishing jobs without replacing pending releases when later changes land.
7+
38
## v0.3.0
49

510
- Configure publishing environment reviewers through release setup while preserving existing environment protections.

0 commit comments

Comments
 (0)