Skip to content

Commit c02509d

Browse files
dblockclaude
andcommitted
Check RubyGems Trusted Publishing before every release
Add RubyGemsCredentials#check!, which performs the real OIDC token request and exchange, verifies push_rubygem scope, and reports the matched repository, workflow, and environment claims without exporting the short-lived credential. Rejected exchanges now name those claims. A Workflow::Preflight runner and release:workflow:preflight task run the check, skipping it for non-RubyGems destinations. Both Actions interfaces run it after context capture, before validation, edits, or publication, so a missing publisher or id-token permission fails before anything is pushed or published. The reusable workflow's dry-run input runs only the preflight in the release environment; the self-release caller passes it through. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011Gr21hZDQo5axDMuFgzL6s
1 parent 41f8630 commit c02509d

16 files changed

Lines changed: 290 additions & 26 deletions

File tree

‎.github/workflows/publish.yml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ on:
77
description: Optional release version overriding the checked-in version.
88
type: string
99
required: false
10+
dry-run:
11+
description: Check release prerequisites without editing files, pushing, or publishing.
12+
type: boolean
13+
default: false
1014

1115
permissions:
1216
contents: write
@@ -20,3 +24,4 @@ jobs:
2024
version: ${{ inputs.version }}
2125
timezone: America/New_York
2226
rubygems-owner: ${{ vars.RUBYGEMS_OWNER_ID }}
27+
dry-run: ${{ inputs.dry-run }}

‎.github/workflows/release.yml‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,40 @@ on:
6464
description: Command to run after a release job failure.
6565
type: string
6666
default: bundle exec rake release:failure
67+
dry-run:
68+
description: Check release prerequisites, such as RubyGems Trusted Publishing, without editing files, pushing, or publishing.
69+
type: boolean
70+
default: false
6771

6872
concurrency:
6973
group: ruby-gem-release-${{ github.repository }}
7074
cancel-in-progress: false
7175

7276
jobs:
77+
preflight:
78+
if: ${{ inputs.dry-run }}
79+
runs-on: ubuntu-latest
80+
environment: release
81+
env:
82+
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
83+
RUBYGEMS_OWNER_ID: ${{ inputs.rubygems-owner }}
84+
steps:
85+
- uses: actions/checkout@v7
86+
id: checkout
87+
with:
88+
persist-credentials: false
89+
- uses: ruby/setup-ruby@v1
90+
id: setup_ruby
91+
with:
92+
ruby-version: ${{ inputs.ruby-version }}
93+
bundler-cache: true
94+
- name: Check Release Prerequisites
95+
id: check_release_prerequisites
96+
env:
97+
RELEASE_STEP_ID: check_release_prerequisites
98+
run: bundle exec rake release:workflow:preflight
7399
release:
100+
if: ${{ !inputs.dry-run }}
74101
runs-on: ubuntu-latest
75102
environment: release
76103
env:
@@ -105,6 +132,11 @@ jobs:
105132
env:
106133
RELEASE_STEP_ID: capture_release_context
107134
run: bundle exec rake release:workflow:capture
135+
- name: Check Release Prerequisites
136+
id: check_release_prerequisites
137+
env:
138+
RELEASE_STEP_ID: check_release_prerequisites
139+
run: bundle exec rake release:workflow:preflight
108140
- name: Validate Release
109141
id: validate_release
110142
env:

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- [#79](https://github.com/dblock/ruby-gem-release/pull/79): Select the next development version part with `next_version: :minor` or `:major` on `Workflow::RubyGem` and `Workflow::SemanticRubyGem` - [@dblock](https://github.com/dblock).
66
- [#80](https://github.com/dblock/ruby-gem-release/pull/80): Skip posting empty Danger report comments - [@dblock](https://github.com/dblock).
7+
- [#85](https://github.com/dblock/ruby-gem-release/pull/85): Verify RubyGems Trusted Publishing before every release with a `release:workflow:preflight` task, add a `dry-run` workflow input that runs only that check, and name the token claims in rejected exchanges - [@dblock](https://github.com/dblock).
78
- Your contribution here.
89

910
## 0.7.0 (2026/11/20)

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ gh api --method PUT repos/{owner}/{repo}/environments/release
7070

7171
For a new gem, open the [pending trusted publishers page](https://rubygems.org/profile/oidc/pending_trusted_publishers). For an existing gem, open your existing gem's **Trusted publishers** page (e.g. `https://rubygems.org/gems/example/trusted_publishers`). Create a [trusted publisher](https://guides.rubygems.org/trusted-publishing/#using-reusable-workflows). Set the repository fields to your gem's repository, the workflow filename to `release.yml`, and the environment to `release`. Set the separate workflow repository fields to `dblock` and `ruby-gem-release`; these identify the reusable workflow RubyGems verifies in the OIDC token.
7272

73+
To verify this setup without releasing, dispatch a [dry run](docs/actions/release.md#dry-run).
74+
7375
Collaborators with write access can dispatch releases after this setup, without repository admin access or their own RubyGems credentials. Creating the environment alone does not restrict publication. Configure [required maintainer approval and deployment branches](RELEASING.md#maintainer-approved-publication) to control who approves publication.
7476

7577
### 4. Make a Release

‎RELEASING.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ bundle exec rake build
4040

4141
The manual **Release** caller is [`.github/workflows/publish.yml`](.github/workflows/publish.yml). Merges and schedules do not publish. It uses Ruby 4.0, the root Rakefile, and `America/New_York` for release dates and Git timestamp offsets.
4242

43-
Verify the existing RubyGems trusted publisher before dispatch. The caller passes the non-secret `RUBYGEMS_OWNER_ID` repository variable when set; this identifies an account, not an API key. Pending publishers need it for first publication.
43+
Verify the existing RubyGems trusted publisher before dispatch with a [dry run](docs/actions/release.md#dry-run), which exchanges credentials without releasing: `gh workflow run publish.yml --ref master -f dry-run=true`. The caller passes the non-secret `RUBYGEMS_OWNER_ID` repository variable when set; this identifies an account, not an API key. Pending publishers need it for first publication.
4444

4545
Only after explicit authorization, successful validation, and confirmed setup:
4646

‎action.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ runs:
9696
env:
9797
RELEASE_STEP_ID: capture_release_context
9898
run: bundle exec rake release:workflow:capture
99+
- name: Check Release Prerequisites
100+
id: check_release_prerequisites
101+
shell: bash
102+
env:
103+
RELEASE_STEP_ID: check_release_prerequisites
104+
run: bundle exec rake release:workflow:preflight
99105
- name: Validate Release
100106
id: validate_release
101107
shell: bash

‎docs/actions/action.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ Register a RubyGems trusted publisher for the calling repository, its own workfl
6868

6969
For maintainer-only publication, protect the caller job's `release` environment using [Maintainer-Approved Publication](../../RELEASING.md#maintainer-approved-publication). Other repository writers can dispatch, but publication waits for a designated maintainer to approve. Environments do not restrict who can dispatch, and an environment without protection rules does not restrict release access.
7070

71+
The action checks [release prerequisites](release.md#release-prerequisites), including the trusted publisher, after context capture and before validation, edits, or publication. To check without releasing, run `bundle exec rake release:workflow:preflight` in a job of the same workflow file, in the `release` environment, with `id-token: write`. RubyGems matches the workflow file and environment, so a check from another workflow or environment does not prove the release job's configuration. The composite action has no dry-run input.
72+
7173
For first publication, register a pending trusted publisher and supply its numeric account ID through `rubygems-owner`. Later publications can omit that input. The default `release:publish` task authenticates through the configured destination before invoking its publication callback; no separate YAML authentication step is needed. The RubyGems destination uses the [Ruby credentials helper](../workflow/ruby_gems_credentials.md) without a stored API key. It exports environment variables and does not create or modify `~/.gem/credentials`. Command overrides that bypass the task must own authentication as well as publication.
7274

7375
## Release and Recovery

‎docs/actions/release.md‎

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,57 @@ For a step-based interface with caller-owned job settings, see the [Composite Ac
88

99
Set these under the calling job's `with:` mapping. Only `ruby-version` is required.
1010

11-
| Input | Type | Default | Behavior |
12-
| --- | --- | --- | --- |
13-
| `ruby-version` | String | Required | Ruby version used for validation and building. |
14-
| `version` | String | Empty | Selected numeric `MAJOR.MINOR.PATCH` release version, without leading zeros. Overrides the checked-in version in standard preparation. |
15-
| `gemspec` | String | Empty | Specification path, defaulting to the only root gemspec. Missing or ambiguous specifications fail. |
16-
| `rubygems-owner` | String | Empty | Numeric RubyGems account ID required for a new gem's pending trusted publisher to create it. |
17-
| `timezone` | String | `UTC` | Installed timezone identifier for the release calendar date and Git timestamp offsets. |
18-
| `prepare-artifacts` | String | Empty | Trusted command preparing explicitly selected input archives after setup, before capture. |
19-
| `artifacts` | String | Empty | Additional newline-separated artifact paths preserved before publication, alongside default paths. |
20-
| `validate` | String | `bundle exec rake release:validate` | Check release preconditions before edits; exit nonzero on failure. |
21-
| `prepare` | String | `bundle exec rake release:prepare` | Prepare release files before version resolution and testing. |
22-
| `test` | String | `bundle exec rake` | Test prepared release files before publication. |
23-
| `publish` | String | `bundle exec rake release:publish` | Commit, build, tag, publish to RubyGems, push atomically, and create a GitHub Release through the configured callback. |
24-
| `bump` | String | `bundle exec rake release:bump` | Prepare, commit, and push the next development iteration through the configured callback. |
25-
| `success` | String | `bundle exec rake release:success` | Run after publication and the next development iteration succeed. |
26-
| `failure` | String | `bundle exec rake release:failure` | Run after a release job failure, after Ruby setup. |
27-
| `git-user-name` | String | `github-actions[bot]` | Checkout-local author name for release and next development iteration commits. |
28-
| `git-user-email` | String | `github-actions[bot]@users.noreply.github.com` | Checkout-local author email for release and next development iteration commits. |
11+
| Input | Type | Default | Behavior |
12+
| --- | --- | --- | --- |
13+
| `ruby-version` | String | Required | Ruby version used for validation and building. |
14+
| `version` | String | Empty | Selected numeric `MAJOR.MINOR.PATCH` release version, without leading zeros. Overrides the checked-in version in standard preparation. |
15+
| `gemspec` | String | Empty | Specification path, defaulting to the only root gemspec. Missing or ambiguous specifications fail. |
16+
| `rubygems-owner` | String | Empty | Numeric RubyGems account ID required for a new gem's pending trusted publisher to create it. |
17+
| `timezone` | String | `UTC` | Installed timezone identifier for the release calendar date and Git timestamp offsets. |
18+
| `prepare-artifacts` | String | Empty | Trusted command preparing explicitly selected input archives after setup, before capture. |
19+
| `artifacts` | String | Empty | Additional newline-separated artifact paths preserved before publication, alongside default paths. |
20+
| `validate` | String | `bundle exec rake release:validate` | Check release preconditions before edits; exit nonzero on failure. |
21+
| `prepare` | String | `bundle exec rake release:prepare` | Prepare release files before version resolution and testing. |
22+
| `test` | String | `bundle exec rake` | Test prepared release files before publication. |
23+
| `publish` | String | `bundle exec rake release:publish` | Commit, build, tag, publish to RubyGems, push atomically, and create a GitHub Release through the configured callback. |
24+
| `bump` | String | `bundle exec rake release:bump` | Prepare, commit, and push the next development iteration through the configured callback. |
25+
| `success` | String | `bundle exec rake release:success` | Run after publication and the next development iteration succeed. |
26+
| `failure` | String | `bundle exec rake release:failure` | Run after a release job failure, after Ruby setup. |
27+
| `git-user-name` | String | `github-actions[bot]` | Checkout-local author name for release and next development iteration commits. |
28+
| `git-user-email` | String | `github-actions[bot]@users.noreply.github.com` | Checkout-local author email for release and next development iteration commits. |
29+
| `dry-run` | Boolean | `false` | Check release prerequisites, such as RubyGems Trusted Publishing, without editing files, pushing, or publishing. See [Dry Run](#dry-run). |
2930

3031
Identity values must be nonempty and contain no line breaks. They change commit attribution, not authentication or push permissions.
3132

33+
### Release Prerequisites
34+
35+
Every release runs `bundle exec rake release:workflow:preflight` after context capture, before validation, edits, or publication, so missing permissions fail before anything is pushed or published. The preflight requests the job's OIDC token and exchanges it at RubyGems exactly as publication does, passing `rubygems-owner` when set. Success proves that the caller granted `id-token: write` and that a trusted publisher matches the repository, workflow, and environment in the token. The job reports those claims and the credential's scopes; on failure, the error names the claims RubyGems rejected. The exchanged credential is masked, expires after 15 minutes, and is never exported or used; publication performs its own exchange. RubyGems does not report which gems a publisher is attached to, so the preflight cannot confirm that the publisher belongs to this gem, or that RubyGems would accept the package.
36+
37+
### Dry Run
38+
39+
Set `dry-run: true` to run only the release prerequisites. A separate `preflight` job runs instead of the `release` job, in the same `release` environment, so required reviewers still approve it. It checks out the caller without persisted credentials, installs the bundle, and runs the same [prerequisite checks](#release-prerequisites). It never edits files, commits, tags, pushes, builds, or publishes.
40+
41+
To dispatch dry runs, add a boolean input to the caller and pass it through:
42+
43+
```yaml
44+
on:
45+
workflow_dispatch:
46+
inputs:
47+
dry-run:
48+
description: Check release prerequisites without releasing.
49+
type: boolean
50+
default: false
51+
52+
jobs:
53+
release:
54+
uses: dblock/ruby-gem-release/.github/workflows/release.yml@v0.7.0
55+
with:
56+
ruby-version: '3.3'
57+
dry-run: ${{ inputs.dry-run }}
58+
```
59+
60+
Then run `gh workflow run release.yml -f dry-run=true`.
61+
3262
### Release Validation
3363

3464
Validation runs after context capture, before preparation edits. Empty pending notes fail unless the selected release already has a changelog heading, allowing repetition. Malformed content also fails. With no changelog, validation passes. Custom changelog objects implement `pending?` and `released?(version)` or supply a `validate:` callback.

‎docs/tasks.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,6 @@ The [Detailed Usage example](usage.md#custom-callbacks) commits, builds, tags, p
6262

6363
## Workflow Credentials
6464

65+
`release:workflow:preflight` checks release prerequisites without editing files, pushing, or publishing. With a RubyGems destination it verifies Trusted Publishing through [`Workflow::RubyGemsCredentials#check!`](workflow/ruby_gems_credentials.md#check) with optional `RUBYGEMS_OWNER_ID`; other destinations skip that check. Both Actions interfaces run it in every release after context capture, and the reusable workflow runs only it for [dry runs](actions/release.md#dry-run).
66+
6567
`release:workflow:credentials` directly invokes [`Workflow::RubyGemsCredentials`](workflow/ruby_gems_credentials.md) with optional `RUBYGEMS_OWNER_ID`. It remains available for custom RubyGems commands, but neither Actions interface calls a separate authentication task. Normal authentication belongs to `release:publish` through the configured destination.

0 commit comments

Comments
 (0)