Skip to content

Commit 76f8768

Browse files
dblockCopilotclaude
committed
Automate gem releases with ruby-gem-release
Preserve Grape's two contribution sections, dashed changelog dates, and stable/development README updates while delegating publication to the pinned reusable workflow. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011Gr21j6KD4oksnhmZ5u3pr
1 parent b9e341a commit 76f8768

8 files changed

Lines changed: 161 additions & 78 deletions

File tree

‎.github/workflows/release.yml‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Release
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
version:
7+
description: Optional release version overriding the checked-in version.
8+
type: string
9+
required: false
10+
dry-run:
11+
description: Only check the release prerequisites, without editing, pushing, or publishing.
12+
type: boolean
13+
default: false
14+
15+
permissions:
16+
contents: read
17+
18+
jobs:
19+
release:
20+
# Unreleased master with the gemfile input; pin the next release's commit.
21+
uses: dblock/ruby-gem-release/.github/workflows/release.yml@57890abebd3c236e4d0543fe2b23643e29ab28ab # master
22+
permissions:
23+
contents: write
24+
id-token: write
25+
with:
26+
ruby-version: '3.4'
27+
gemfile: gemfiles/release.gemfile
28+
version: ${{ inputs.version }}
29+
dry-run: ${{ inputs.dry-run }}

‎.github/workflows/test.yml‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
- gemfiles/rack_3_2.gemfile
3434
- gemfiles/rails_8_0.gemfile
3535
- gemfiles/rails_8_1.gemfile
36-
specs: ['spec --exclude-pattern=spec/integration/{grape_entity,grape_swagger,hashie,dry_validation,multi_*}/*_spec.rb']
36+
specs: ['spec --exclude-pattern=spec/integration/{grape_entity,grape_swagger,hashie,dry_validation,multi_*,ruby_gem_release}/*_spec.rb']
3737
include:
3838
- ruby: '4.0'
3939
gemfile: gemfiles/grape_entity.gemfile
@@ -59,6 +59,9 @@ jobs:
5959
- ruby: '4.0'
6060
gemfile: gemfiles/multi_xml_0_8.gemfile
6161
specs: 'spec/integration/multi_xml'
62+
- ruby: '3.4'
63+
gemfile: gemfiles/release.gemfile
64+
specs: 'spec/integration/ruby_gem_release'
6265
runs-on: ubuntu-latest
6366
env:
6467
BUNDLE_GEMFILE: ${{ github.workspace }}/${{ matrix.gemfile }}

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* [#3056](https://github.com/ruby-grape/grape/pull/3056): Drop support for Rails 7.2 and require ActiveSupport 8.0 - [@ericproulx](https://github.com/ericproulx).
1010
* [#3062](https://github.com/ruby-grape/grape/pull/3062): Use the reusable Danger workflow with a separate Gemfile - [@dblock](https://github.com/dblock).
1111
* [#3063](https://github.com/ruby-grape/grape/pull/3063): Drop support for Rack 2.2 and 3.0 and require Rack 3.1 - [@ericproulx](https://github.com/ericproulx).
12+
* [#3078](https://github.com/ruby-grape/grape/pull/3078): Automate gem releases with ruby-gem-release - [@dblock](https://github.com/dblock).
1213
* [#3088](https://github.com/ruby-grape/grape/pull/3088): Pin workflow actions to commits and grant their tokens only what they need - [@ericproulx](https://github.com/ericproulx).
1314
* [#3090](https://github.com/ruby-grape/grape/pull/3090): Call Danger's workflows from danger/danger-pr-comment, where they moved - [@ericproulx](https://github.com/ericproulx).
1415
* Your contribution here.

‎RELEASING.md‎

Lines changed: 38 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,73 @@
11
Releasing Grape
22
===============
33

4-
There're no particular rules about when to release Grape. Release bug fixes frequently, features not so frequently and breaking API changes rarely.
4+
There are no particular rules about when to release Grape. Release bug fixes frequently, features not so frequently and breaking API changes rarely.
55

6-
### Release
7-
8-
Run tests, check that all tests succeed locally.
9-
10-
```
11-
bundle install
12-
rake
13-
```
14-
15-
Double-check that the [last build succeeded](https://github.com/ruby-grape/grape/actions) for all supported platforms.
16-
17-
Those with r/w permissions to the [master Grape repository](https://github.com/ruby-grape/grape) generally have large Grape-based projects. Point one to Grape HEAD and run all your API tests to catch any obvious regressions.
18-
19-
```
20-
gem grape, github: 'ruby-grape/grape'
21-
```
22-
23-
Modify the "Stable Release" section in [README.md](README.md). Change the text to reflect that this is going to be the documentation for a stable release. Remove references to the previous release of Grape. Keep the file open, you'll have to undo this change after the release.
24-
25-
```
26-
## Stable Release
27-
28-
You're reading the documentation for the stable release of Grape, 0.6.0.
29-
```
30-
31-
Change "Next Release" in [CHANGELOG.md](CHANGELOG.md) to the new version.
32-
33-
```
34-
#### 0.6.0 (2013/9/16)
35-
```
6+
### One-Time Setup
367

37-
Remove the line with "Your contribution here.", since there will be no more contributions to this release.
8+
Releases use [ruby-gem-release](https://github.com/dblock/ruby-gem-release) and RubyGems Trusted Publishing without a stored RubyGems API key. The release tooling lives in its own bundle, [gemfiles/release.gemfile](gemfiles/release.gemfile), which evaluates the main `Gemfile` and adds `ruby-gem-release` and `commonmarker`, so neither is installed for contributors or other CI jobs. The gem is pinned there and the reusable workflow is pinned by commit SHA in [release.yml](.github/workflows/release.yml). Both currently point at an unreleased `master` commit with the workflow's `gemfile` input; move both to the next release together. The trusted publisher names the workflow repository but not a ref, so verify that a pinned SHA is the commit of the matching `vVERSION` tag.
389

39-
Commit your changes.
10+
Create a GitHub environment named `release` in **Settings > Environments**. Configure required release-maintainer reviewers, prevent self-review where appropriate, and restrict deployments to `master`. Creating an unprotected environment alone does not restrict publication: repository writers can dispatch the workflow, and required reviewers gate publication. The release job installs the release bundle, which includes the development and test bundle, without a lockfile and runs RuboCop and the full suite while holding `id-token: write` and `contents: write`, so any gem in that bundle could request a publishing token. Approval controls when the job runs, not what runs in it; review dependency changes on `master` before approving.
4011

41-
```
42-
git add README.md CHANGELOG.md
43-
git commit -m "Preparing for release, 0.6.0."
44-
git push origin master
45-
```
12+
In the [grape trusted publishers settings](https://rubygems.org/gems/grape/trusted_publishers), configure these values:
4613

47-
Release.
14+
| Field | Value |
15+
| --- | --- |
16+
| Repository owner | `ruby-grape` |
17+
| Repository name | `grape` |
18+
| Workflow filename | `release.yml` |
19+
| Environment | `release` |
20+
| Workflow repository owner | `dblock` |
21+
| Workflow repository name | `ruby-gem-release` |
4822

49-
```
50-
$ rake release
23+
The workflow repository identifies the reusable workflow, not Grape's caller workflow. Ensure branch and tag rules permit `GITHUB_TOKEN` to push release commits to `master` and create `vVERSION` tags. If rules require PRs or checks that the release token cannot bypass, a repository administrator must establish an approved release policy before dispatching; the workflow does not change repository protections.
5124

52-
grape 0.6.0 built to pkg/grape-0.6.0.gem.
53-
Tagged v0.6.0.
54-
Pushed git commits and tags.
55-
Pushed grape 0.6.0 to rubygems.org.
56-
```
57-
58-
### Prepare for the Next Version
25+
### Release
5926

60-
Modify the "Stable Release" section in [README.md](README.md). Change the text to reflect that this is going to be the next release.
27+
Run tests and check that all tests succeed locally:
6128

29+
```sh
30+
bundle install
31+
bundle exec rake
32+
bundle exec rake build
33+
BUNDLE_GEMFILE=gemfiles/release.gemfile bundle install
34+
BUNDLE_GEMFILE=gemfiles/release.gemfile bundle exec rspec spec/integration/ruby_gem_release
6235
```
63-
## Stable Release
6436

65-
You're reading the documentation for the next release of Grape, which should be 0.6.1.
66-
The current stable release is [0.6.0](https://github.com/ruby-grape/grape/blob/v0.6.0/README.md).
67-
```
37+
The release specs run only in the release bundle, as their own CI job. They use `ruby-gem-release/rspec` to exercise the production workflow and README hooks in a disposable Git repository, replacing registry and hosted publication with the library's local adapters. They do not publish to RubyGems or GitHub.
6838

69-
Add the next release to [CHANGELOG.md](CHANGELOG.md).
39+
Double-check that the [last build succeeded](https://github.com/ruby-grape/grape/actions) for all supported platforms. Point a Grape-based project at Grape HEAD and run its API tests to catch regressions:
7040

41+
```ruby
42+
gem 'grape', github: 'ruby-grape/grape'
7143
```
72-
### 0.6.1 (Next)
73-
74-
#### Features
7544

76-
* Your contribution here.
45+
Review the next section of [CHANGELOG.md](CHANGELOG.md) and the version in [lib/grape/version.rb](lib/grape/version.rb). Keep one `* Your contribution here.` placeholder in each of Features and Fixes; ruby-gem-release preserves this two-section format. Actual release notes are required.
7746

78-
#### Fixes
47+
Dispatch **Release** from `master` in GitHub Actions, or run:
7948

80-
* Your contribution here.
49+
```sh
50+
gh workflow run release.yml --repo ruby-grape/grape --ref master
8151
```
8252

83-
Bump the minor version in lib/grape/version.rb.
53+
To select a different release version without a separate version-bump commit, pass `-f version=4.2.0`. Dispatching authorizes publication once the `release` environment's approval requirements are satisfied; do not dispatch from PR CI. To check the `release` environment, trusted publisher, and push permissions without editing, pushing, or publishing anything, pass `-f dry-run=true`; required reviewers still approve the dry run.
8454

85-
```ruby
86-
module Grape
87-
VERSION = '0.6.1'.freeze
88-
end
89-
```
55+
The workflow validates release preconditions, dates the changelog and removes both contribution placeholders, updates the README to describe the stable release, and runs `bundle exec rake`. It builds and preserves the gem and recovery artifacts, publishes that exact gem to RubyGems, pushes the release commit and tag, and creates a GitHub Release. It then increments the minor version, as Grape does after every release, and pushes the next development iteration with both changelog sections and the README's stable-release link restored. No manual release edits are needed; `rake release` stops and points here instead of publishing.
9056

91-
Commit your changes.
57+
### Recovery
9258

93-
```
94-
git add CHANGELOG.md README.md lib/grape/version.rb
95-
git commit -m "Preparing for next development iteration, 0.6.1."
96-
git push origin master
97-
```
59+
Inspect the failed run and its `release-gem-*` artifact before retrying. It can contain the built gem, checksum manifest, and Git recovery bundle. RubyGems publication cannot be undone by resetting Git: if a version was uploaded, never publish different bytes under that version or move its tag. Follow the [ruby-gem-release recovery guide](https://github.com/dblock/ruby-gem-release/blob/master/RELEASING.md#verify-and-recover) to determine which steps completed and recover deliberately.
9860

9961
### Make an Announcement
10062

101-
Make an announcement on the [ruby-grape@googlegroups.com](mailto:ruby-grape@googlegroups.com) mailing list. The general format is as follows.
63+
Make an announcement on the [ruby-grape@googlegroups.com](mailto:ruby-grape@googlegroups.com) mailing list:
10264

103-
```
104-
Grape 0.6.0 has been released.
65+
```text
66+
Grape 4.2.0 has been released.
10567
106-
There were 8 contributors to this release, not counting documentation.
68+
There were N contributors to this release, not counting documentation.
10769
10870
Please note the breaking API change in ...
10971
11072
[copy/paste CHANGELOG here]
111-
11273
```

‎Rakefile‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ Bundler.setup(:default, :test, :development)
66

77
Bundler::GemHelper.install_tasks
88

9+
# Releases run through the Release workflow, not Bundler's tasks; see RELEASING.md.
10+
%w[release release:guard_clean release:source_control_push release:rubygem_push].each { |name| Rake::Task[name].clear }
11+
desc('Release through the Release GitHub Actions workflow, see RELEASING.md')
12+
task(:release) { abort('Release through the Release GitHub Actions workflow; see RELEASING.md.') }
13+
914
require('rspec/core/rake_task')
1015
RSpec::Core::RakeTask.new(:spec) do |spec|
1116
spec.pattern = 'spec/**/*_spec.rb'
@@ -25,3 +30,12 @@ require('rubocop/rake_task')
2530
RuboCop::RakeTask.new
2631

2732
task(default: %i[rubocop spec])
33+
34+
# The release workflow's tasks, in its own bundle (gemfiles/release.gemfile).
35+
begin
36+
require_relative('tasks/release')
37+
GrapeRelease.workflow.install_tasks
38+
rescue LoadError => e
39+
# (Any other bundle, which hasn't got ruby-gem-release.)
40+
raise unless e.path.to_s.start_with?('ruby-gem-release')
41+
end

‎gemfiles/release.gemfile‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
# The release workflow's bundle: the main Gemfile, so the release job still runs
4+
# the full suite, plus the release tooling only it needs.
5+
eval_gemfile '../Gemfile'
6+
7+
gem 'commonmarker', '~> 2.10', require: false
8+
# Unreleased master with the reusable workflow's gemfile input; pin the next release.
9+
gem 'ruby-gem-release', git: 'https://github.com/dblock/ruby-gem-release.git', ref: '57890abebd3c236e4d0543fe2b23643e29ab28ab', require: false
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
require 'ruby-gem-release/rspec'
4+
require_relative '../../../tasks/release'
5+
6+
describe GrapeRelease do
7+
include RubyGemRelease::RSpec
8+
9+
it 'updates the README and CHANGELOG in the release and next development commits' do
10+
with_release_project do |project|
11+
project.copy_files(path: File.expand_path('../../..', __dir__), files: %w[tasks/release.rb grape.gemspec lib/grape/version.rb README.md CHANGELOG.md])
12+
project.configure_workflow(require: 'tasks/release', builder: 'GrapeRelease.workflow')
13+
project.commit!
14+
15+
project.release!(version: '4.2.9')
16+
17+
readme = File.read(File.expand_path('../../../README.md', __dir__))
18+
section = /^## Stable Release\n.*?(?=^## |\z)/m
19+
stable = "You're reading the documentation for the stable release of Grape, 4.2.9."
20+
development = "You're reading the documentation for the next release of Grape, which should be 4.3.0.\n" \
21+
'The current stable release is [4.2.9](https://github.com/ruby-grape/grape/blob/v4.2.9/README.md).'
22+
expect(project.remote_file('v4.2.9', 'README.md')).to eq(readme.sub(section, "## Stable Release\n\n#{stable}\n\n").strip)
23+
expect(project.remote_file('master', 'README.md')).to eq(readme.sub(section, "## Stable Release\n\n#{development}\n\n").strip)
24+
25+
changelog = File.read(File.expand_path('../../../CHANGELOG.md', __dir__))
26+
released = changelog.sub('### 4.2.0 (Next)', "### 4.2.9 (#{project.environment.fetch('RELEASE_DATE')})").gsub("* Your contribution here.\n", '')
27+
development = "### 4.3.0 (Next)\n\n#### Features\n\n* Your contribution here.\n\n#### Fixes\n\n* Your contribution here.\n\n#{released}"
28+
expect(project.environment.fetch('RELEASE_DATE')).to match(/\A\d{4}-\d{2}-\d{2}\z/)
29+
expect(project.remote_file('v4.2.9', 'CHANGELOG.md')).to eq(released.strip)
30+
expect(project.remote_file('master', 'CHANGELOG.md')).to eq(development.strip)
31+
expect(project.remote_file('v4.2.9', 'lib/grape/version.rb')).to include("VERSION = '4.2.9'")
32+
expect(project.remote_file('master', 'lib/grape/version.rb')).to include("VERSION = '4.3.0'")
33+
expect(project.git('status', '--porcelain')).to eq('')
34+
end
35+
end
36+
end

‎tasks/release.rb‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
require 'ruby-gem-release/workflow/ruby_gem'
4+
require 'ruby-gem-release/changelog'
5+
require 'ruby-gem-release/markdown/section'
6+
7+
module GrapeRelease
8+
# Grape starts the next minor version after every release, patch releases included,
9+
# and drops an empty Features or Fixes section from a release.
10+
def self.workflow(**)
11+
workflow = RubyGemRelease::Workflow::RubyGem.new(
12+
version: RubyGemRelease::Version::RubyConstant.new(path: 'lib/grape/version.rb', constant: 'VERSION'),
13+
changelog: RubyGemRelease::Changelog::Intridea.new(placeholder: '* Your contribution here.', date_format: '%Y-%m-%d', sections: %w[Features Fixes]),
14+
next_version: :minor,
15+
**
16+
)
17+
18+
readme = RubyGemRelease::Markdown::Section.new(path: 'README.md', heading: '## Stable Release')
19+
workflow.after(:prepare_release) do |ctx|
20+
readme.replace!("You're reading the documentation for the stable release of Grape, #{ctx.release.version}.")
21+
end
22+
workflow.after(:prepare_next) do |ctx|
23+
readme.replace!(
24+
"You're reading the documentation for the next release of Grape, which should be #{ctx.release.next_version}.\n" \
25+
"The current stable release is [#{ctx.release.version}](https://github.com/ruby-grape/grape/blob/v#{ctx.release.version}/README.md)."
26+
)
27+
end
28+
workflow
29+
end
30+
end

0 commit comments

Comments
 (0)