Skip to content

Automate gem releases with ruby-gem-release - #3078

Open
dblock wants to merge 1 commit into
ruby-grape:masterfrom
dblock:dblock/automate-gem-release
Open

dblock wants to merge 1 commit into
ruby-grape:masterfrom
dblock:dblock/automate-gem-release

Conversation

@dblock

@dblock dblock commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

Summary

Automate Grape releases with ruby-gem-release and RubyGems Trusted Publishing. A manually dispatched workflow owns validation, artifact preservation, publication, release commits and tags, GitHub Releases, and the next development iteration, with an optional version override and a dry-run input that only checks the release prerequisites.

  • Release bundle: ruby-gem-release and commonmarker live in gemfiles/release.gemfile, which evaluates the main Gemfile, so contributors and other CI jobs don't install them. The reusable workflow's gemfile: input runs every release step in that bundle. The Rakefile installs the release tasks only when ruby-gem-release is available, and rake release stops and points to RELEASING.md in every bundle.
  • Pins: the gem is pinned to ruby-gem-release 0.9.0 and the reusable workflow to its v0.9.0 commit (fc9b267), which pins its own actions by commit. The release job alone gets contents: write and id-token: write.
  • Changelog and version: the Intridea changelog keeps Grape's Features and Fixes sections and contribution placeholders, writes dashed dates (date_format: '%Y-%m-%d'), and drops an empty section from a release (sections: %w[Features Fixes]). The next development version bumps minor (next_version: :minor). README lifecycle hooks update the Stable Release section.
  • Spec: spec/integration/ruby_gem_release/release_spec.rb runs the production workflow builder in separate Rake processes with local publication adapters and asserts the full README, CHANGELOG, and version file in both the release tag and master. It runs in its own test.yml job against the release bundle and is excluded from the main jobs.

Required Before the First Release

  • Configure the GitHub release environment with maintainer approval and deployment restricted to master.
  • Remove the pull-request requirement on master (it requires 0 approvals, so writers can already merge their own PRs), keeping force-push and deletion blocked, so the release token can push the release commit and tag.
  • Configure the grape RubyGems trusted publisher with repository ruby-grape/grape, workflow release.yml, environment release, and workflow repository dblock/ruby-gem-release.
  • Dispatch with -f dry-run=true to verify the environment, trusted publisher, and push permissions.

These settings are documented in RELEASING.md; this PR doesn't change repository protections or RubyGems configuration.

Validation

  • Default bundle: bundle exec rake passes (RuboCop, 3,320 examples), bundle exec rake build passes, and neither commonmarker nor ruby-gem-release is installed.
  • Release bundle (BUNDLE_GEMFILE=gemfiles/release.gemfile): bundle exec rspec spec/integration/ruby_gem_release passes, including the 4.2.9 → 4.3.0 bump.
  • actionlint passes for release.yml and test.yml.

No workflow was dispatched, and nothing was published to RubyGems or GitHub.

This description was written by an AI (Claude) acting on behalf of @dblock.

@dblock
dblock force-pushed the dblock/automate-gem-release branch 6 times, most recently from 28eb7b2 to dc1808b Compare November 11, 2026 19:26
@dblock
dblock marked this pull request as ready for review November 11, 2026 19:29
@dblock
dblock requested a review from ericproulx November 11, 2026 19:29
@dblock

dblock commented Nov 11, 2026 •

Copy link
Copy Markdown
Member Author

@ericproulx this automates Grape releases, would need some 1-time setup in GitHub and Rubygems for trusted publisher (copilot can do it for you), see above

@ericproulx

Copy link
Copy Markdown
Contributor

@dblock Thanks for this. Before we do the one-time setup, three things would make the first run go wrong, plus a security ask.

1. Master's protection would reject the release push, after gem push. Master requires a pull request (0 approvals, admins exempt, which is how the "Preparing for release" commits go in today). GITHUB_TOKEN can't bypass that, and publish pushes the gem before the atomic push of master and the tag. The first dispatch would publish 4.2.0 and then fail into recovery. We need to change the protection first, or give the workflow a token that can bypass it. On the library side, could it check that it can push before it calls gem push?

2. Changelog dates. Intridea writes (%Y/%m/%d), but Grape's last 20 release headings use (2026-10-31). The date_format: option in 0.7.0 (dblock/ruby-gem-release#69) fixes it. Since 0.7.0 also makes Commonmarker optional (dblock/ruby-gem-release#62), that means bumping both pins and adding gem 'commonmarker'.

3. The next version should bump minor, not patch. stage_next! calls increment! with its part: :patch default. Grape has always bumped minor after a release, patch releases included (3.0.1 → 3.1.0, 3.1.1 → 3.2.0, 4.1.0 → 4.2.0). With this PR, master would read 4.2.1 after releasing 4.2.0, and every feature release would need -f version=. This works on 0.6.0 and 0.7.0:

class MinorVersion < RubyGemRelease::Version::RubyConstant
  def increment!(from: read) = super(part: :minor, from:)
end

The spec pins the patch bump today (4.2.9 → 4.2.10). A CHANGELOG assertion next to the README ones would have caught 2 and 3.

4. Security. Everything runs in one job holding id-token: write and contents: write. That job installs the dev/test bundle without a lockfile and runs RuboCop and the full suite, so any gem in that bundle can mint the publish token. grape.gemspec sets rubygems_mfa_required, and trusted publishing necessarily does without the OTP. That leaves the release environment's approval as the only gate, and it controls when the job runs, not what runs in it. Could we:

  • pin the workflow by SHA instead of the tag? The trusted publisher names your workflow repo but not a ref.
    uses: dblock/ruby-gem-release/.github/workflows/release.yml@f3d8d74968070fa7dcfdfa6893451032e01d7f4e # v0.6.0
  • split the reusable workflow into a read-only build/test job and a publish job that only downloads the built gem and pushes it?

Nits

  • The Rakefile builds the workflow on every rake. Locally, with no RELEASE_DATA_DIR, Package::RubyGem and SourceControl::Git each leave a Dir.mktmpdir behind.
  • Bundler's rake release is still installed. Worth removing so nobody bypasses the workflow out of habit.
  • release_notes_spec.rb tests the README hooks, so release_spec.rb?
  • A fixes-only release keeps an empty #### Features heading. 4.0.1 dropped it by hand.

The fixture's environment isolation (unsetenv_others) looks right, so the spec is safe inside the real release job.

@dblock
dblock force-pushed the dblock/automate-gem-release branch 2 times, most recently from ad1c0f5 to 48baed1 Compare November 20, 2026 21:11
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Danger Report

No issues found.

View run

@dblock

dblock commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

Thanks @ericproulx, this was a thorough review!

  1. Master protection. You're right, the push would fail after gem push. Before the first release I'll remove the PR requirement on master. Currently the policy lets those with write permissions merge their own PRs, so this is the same security risk. Force-push and deletion stay blocked.

  2. Checking before gem push. A dry-run check can't detect protection: GitHub only enforces branch rules on a real push, and the Actions token can't read them. I'd also rather not change the publication order. Registry and Git pushes can't be atomic, so any order can leave a partial release, and gem push is the step most likely to fail. Reruns pick up where the release stopped, and the artifacts are kept for recovery.

  3. Changelog dates. Fixed. The PR now uses 0.7.0 with date_format: '%Y-%m-%d' and commonmarker, and the spec asserts the full CHANGELOG in both the tag and master.

  4. Minor bump. Fixed with your MinorVersion subclass, and the spec now expects 4.2.9 → 4.3.0. Select the next development version part dblock/ruby-gem-release#79 added next_version: :minor, so I'll replace the subclass once 0.8.0 is released.

  5. Security. The workflow is now pinned by SHA (d36d233 # v0.7.0), and RELEASING.md says to update both pins together. I don't think splitting build and publish is worth it. Today a maintainer installs the same unlocked dependencies to run rake release, and a split wouldn't stop a compromised build dependency from altering the gem it builds. Compared with today's manual release, the workflow is safer:

    • Each run mints a short-lived token, so there's no long-lived API key in ~/.gem/credentials to steal and reuse.
    • The token can only push grape, while a maintainer's key usually covers every gem they own.
    • The fresh runner holds only that token, not a laptop's SSH keys, GitHub tokens or other credentials.
    • The gem is built reproducibly from a clean checkout of the exact master commit, never from a local working copy.
    • A designated maintainer must approve each release, optionally someone other than whoever dispatched it. Who did what is logged, and the logs, gem and recovery bundle are kept.

    What we give up is the MFA code on each push, which environment approval replaces. That seems like a fair trade.

Nits. Bundler's rake release now stops and points to RELEASING.md, and the spec is renamed to release_spec.rb. In the library, dblock/ruby-gem-release#82 creates the temp dirs only when needed, and dblock/ruby-gem-release#83 adds sections: to drop empty subsections such as #### Features from a release. I'll use it once released.

Written by an AI (Claude) on behalf of @dblock.

@ericproulx

Copy link
Copy Markdown
Contributor

@dblock Thanks for working through all of that so quickly. One more: could ruby-gem-release and commonmarker go in a Gemfile of their own, as Danger did in #3062 and Duel does in #3077? Today every bundle installs them, including each CI gemfile variant and every contributor's bundle install, and commonmarker is a native Rust extension.

That needs a gemfile: input on the reusable release workflow, which neither v0.7.0 nor master has yet. Overriding the command inputs isn't enough, because the capture, resolve, checkpoint and failure-phase steps run bundle exec rake release:workflow:* in the default bundle, which ruby/setup-ruby installs. Danger's and Duel's reusable workflows both take a gemfile: input that sets BUNDLE_GEMFILE before Ruby is set up, so the same would work here.

With that input, the Grape side would be:

  • gemfiles/release.gemfile, which evaluates the main Gemfile and adds the two gems, so the release job's bundle exec rake still runs the full suite:
    eval_gemfile '../Gemfile'
    
    gem 'commonmarker', '~> 2.10', require: false
    gem 'ruby-gem-release', '0.7.0', require: false
  • gemfile: gemfiles/release.gemfile in release.yml.
  • A rescue LoadError around the tasks/release hook in the Rakefile, as Add Duel performance regression guards #3077 does for Duel. The rake release override can stay unconditional, since it needs neither gem.
  • The release spec in spec/integration/ruby_gem_release/, which rake spec already skips. It would run in a test.yml job against gemfiles/release.gemfile, like the grape_entity and hashie ones, and be added to the main jobs' exclude list. That way a pull request that changes tasks/release.rb or the README's Stable Release section is still checked.
  • Both gems dropped from the Gemfile.

@ericproulx

Copy link
Copy Markdown
Contributor

@dblock Pinning the reusable workflow by SHA freezes its file, but not the actions it uses. At d36d233, the release job runs actions/checkout@v7, ruby/setup-ruby@v1 and actions/upload-artifact@v7 by tag, and ruby/setup-ruby@v1 is a branch. That's the job that can mint a RubyGems token. Could ruby-gem-release pin them by commit, with the version as a comment, as Duel does in its reusable workflows?

To be fair, Grape's own workflows weren't pinned either. #3088 fixes that.

@dblock

dblock commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

A follow-up, @ericproulx: these are now merged into ruby-gem-release and will be in 0.8.0, which I'll switch this PR to once it's released.

Written by an AI (Claude) on behalf of @dblock.

@dblock

dblock commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

Good catch, thanks @ericproulx! dblock/ruby-gem-release#88 pins every action in the reusable workflow, the composite action and its own CI to a commit with the version as a comment, adds Dependabot for updates, and adds a spec that fails on unpinned references. Once it's released, I'll repin Grape's release.yml to that release's commit. I also rebased this PR on #3088 and moved the write permissions onto the release job to match.

Written by an AI (Claude) on behalf of @dblock.

@ericproulx

Copy link
Copy Markdown
Contributor

@dblock Thanks for pinning the actions in dblock/ruby-gem-release#88. That's unreleased, though, and this PR still calls v0.7.0, where checkout@v7, setup-ruby@v1 and upload-artifact@v7 run by tag. Could you release 0.8.0 and move this PR to it, the Gemfile pin and the workflow's commit together? It would also let this PR:

The gemfile: input from my earlier comment isn't in 0.8.0 yet. It can follow in a later release if that's simpler.

@dblock

dblock commented Nov 20, 2026 •

Copy link
Copy Markdown
Member Author

Thanks @ericproulx! This PR now uses ruby-gem-release master at 57890ab, which is 0.8.0 plus the gemfile: input (dblock/ruby-gem-release#89). The Gemfile pin and the workflow's commit move together:

  • The actions the reusable workflow uses are pinned by commit (Pin actions to commit SHAs dblock/ruby-gem-release#88).
  • ruby-gem-release and commonmarker moved to gemfiles/release.gemfile, which evaluates the main Gemfile, and release.yml passes gemfile: gemfiles/release.gemfile. The Rakefile rescues LoadError around the release tasks, but only when ruby-gem-release is missing; rake release stays unconditional. The release spec moved to spec/integration/ruby_gem_release/ and runs in its own test.yml job against that bundle.
  • next_version: :minor replaces the MinorVersion subclass, and sections: %w[Features Fixes] drops an empty section from a release.
  • Every release runs the Trusted Publishing and push-rule preflight. release.yml exposes dry-run, which I'll use to verify the setup before the first real release.

I'll move both pins to the next release, which will include the gemfile: input.

Anything else is needed for this?

Written by an AI (Claude) on behalf of @dblock.

@ericproulx

ericproulx commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

@dblock I guess you'll make a release before merging this PR. The SHA pin are on master instead of the 0.8.0 tag.

@dblock
dblock force-pushed the dblock/automate-gem-release branch from 76f8768 to 01594dc Compare November 20, 2026 23:47
@dblock

dblock commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

@dblock I guess you'll make a release before merging this PR. The SHA pin are on master instead of the 0.8.0 tag.

Done.

@dblock

dblock commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

What needs to happen after this is merged, before the first automated release (also in RELEASING.md):

  1. Release environment (repository admin): under Settings > Environments, create release, add the maintainers who may approve releases as required reviewers (optionally preventing self-review), and restrict deployments to master.
  2. Branch protection (repository admin): remove the pull-request requirement on master, keeping force-push and deletion blocked, so the release token can push the release commit, the vVERSION tag, and the next development commit.
  3. Trusted publisher (a grape gem owner on RubyGems): on the grape trusted publishers page, add a GitHub Actions publisher with repository ruby-grape/grape, workflow release.yml, environment release, and workflow repository dblock/ruby-gem-release.
  4. Dry run: gh workflow run release.yml --repo ruby-grape/grape --ref master -f dry-run=true, then approve it in the release environment. It checks the trusted publisher exchange and push permissions without editing, pushing, or publishing anything.
  5. First release: review the next section of CHANGELOG.md, then gh workflow run release.yml --repo ruby-grape/grape --ref master and approve it. Check that 4.2.0 is on RubyGems, that the v4.2.0 tag and GitHub Release exist, and that master moved to 4.3.0 with a fresh CHANGELOG section and README.

The mailing list announcement stays manual. Once releases go through the workflow, maintainers no longer need a RubyGems API key to publish Grape.

Written by an AI (Claude) on behalf of @dblock.

@dblock
dblock force-pushed the dblock/automate-gem-release branch 4 times, most recently from b870ec9 to c09c443 Compare November 21, 2026 14:48
@ericproulx

Copy link
Copy Markdown
Contributor

@dblock The .rubocop_todo.yml isn't needed anymore. Both offenses it covers are in lib/grape/endpoint.rb on this PR's base: Metrics/ClassLength on Endpoint and Metrics/AbcSize on compile!. #3093 fixed them on master. I rebased this branch onto master and dropped the file. The rebase is clean, and bundle exec rubocop reports no offenses.

To do the same, with upstream pointing at ruby-grape/grape:

git checkout dblock/automate-gem-release
git fetch upstream master
git rebase upstream/master
git rm .rubocop_todo.yml
git checkout upstream/master -- .rubocop.yml
bundle exec rubocop
git commit --amend --no-edit
git push --force-with-lease

The git checkout upstream/master -- .rubocop.yml line restores master's file. That drops the inherit_from: .rubocop_todo.yml line, which is the PR's only change to it.

Preserve Grape's two contribution sections, dashed changelog dates, minor
version bumps, and stable/development README updates while delegating
publication to the pinned reusable workflow, run in a separate release
bundle.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dblock
dblock force-pushed the dblock/automate-gem-release branch from c09c443 to 7f6443f Compare November 21, 2026 18:07
@dblock

dblock commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Thanks @ericproulx, done. I rebased onto master (with #3093), removed .rubocop_todo.yml, and restored master's .rubocop.yml, so this PR no longer changes either file. bundle exec rubocop reports no offenses, and the specs and all CI checks pass.

This comment was written by an AI (Claude) acting on behalf of @dblock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants