Skip to content

Commit e1f6c05

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 00c2a4e commit e1f6c05

7 files changed

Lines changed: 135 additions & 77 deletions

File tree

‎.github/workflows/release.yml‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
11+
permissions:
12+
contents: write
13+
id-token: write
14+
15+
jobs:
16+
release:
17+
uses: dblock/ruby-gem-release/.github/workflows/release.yml@d36d23398924dcc9cbd65e108ad33b5a9ee1c13a # v0.7.0
18+
with:
19+
ruby-version: '3.4'
20+
version: ${{ inputs.version }}

‎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
* Your contribution here.
1314

1415
#### Fixes

‎Gemfile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ end
1717
group :development do
1818
gem 'benchmark-ips'
1919
gem 'benchmark-memory'
20+
gem 'commonmarker', '~> 2.10', require: false
2021
gem 'irb'
22+
gem 'ruby-gem-release', '0.7.0', require: false
2123
end
2224

2325
group :test do

‎RELEASING.md‎

Lines changed: 36 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,71 @@
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 0.7.0](https://github.com/dblock/ruby-gem-release/tree/v0.7.0), pinned in the Gemfile and by commit SHA in the reusable workflow, and RubyGems Trusted Publishing without a stored RubyGems API key. The trusted publisher names the workflow repository but not a ref, so update both pins together and verify that the workflow 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 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
6233
```
63-
## Stable Release
6434

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-
```
35+
The release specs 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.
6836

69-
Add the next release to [CHANGELOG.md](CHANGELOG.md).
37+
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:
7038

39+
```ruby
40+
gem 'grape', github: 'ruby-grape/grape'
7141
```
72-
### 0.6.1 (Next)
73-
74-
#### Features
7542

76-
* Your contribution here.
43+
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.
7744

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

80-
* Your contribution here.
47+
```sh
48+
gh workflow run release.yml --repo ruby-grape/grape --ref master
8149
```
8250

83-
Bump the minor version in lib/grape/version.rb.
51+
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 or use it as a dry run.
8452

85-
```ruby
86-
module Grape
87-
VERSION = '0.6.1'.freeze
88-
end
89-
```
53+
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.
9054

91-
Commit your changes.
55+
### Recovery
9256

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-
```
57+
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/v0.7.0/RELEASING.md#verify-and-recover) to determine which steps completed and recover deliberately.
9858

9959
### Make an Announcement
10060

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

103-
```
104-
Grape 0.6.0 has been released.
63+
```text
64+
Grape 4.2.0 has been released.
10565
106-
There were 8 contributors to this release, not counting documentation.
66+
There were N contributors to this release, not counting documentation.
10767
10868
Please note the breaking API change in ...
10969
11070
[copy/paste CHANGELOG here]
111-
11271
```

‎Rakefile‎

Lines changed: 8 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,6 @@ require('rubocop/rake_task')
2530
RuboCop::RakeTask.new
2631

2732
task(default: %i[rubocop spec])
33+
34+
require_relative 'tasks/release'
35+
GrapeRelease.workflow.install_tasks

‎spec/tasks/release_spec.rb‎

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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
class MinorVersion < RubyGemRelease::Version::RubyConstant
10+
def increment!(from: read) = super(part: :minor, from:)
11+
end
12+
13+
def self.workflow(**)
14+
workflow = RubyGemRelease::Workflow::RubyGem.new(
15+
version: MinorVersion.new(path: 'lib/grape/version.rb', constant: 'VERSION'),
16+
changelog: RubyGemRelease::Changelog::Intridea.new(placeholder: '* Your contribution here.', date_format: '%Y-%m-%d'),
17+
**
18+
)
19+
20+
readme = RubyGemRelease::Markdown::Section.new(path: 'README.md', heading: '## Stable Release')
21+
workflow.after(:prepare_release) do |ctx|
22+
readme.replace!("You're reading the documentation for the stable release of Grape, #{ctx.release.version}.")
23+
end
24+
workflow.after(:prepare_next) do |ctx|
25+
readme.replace!(
26+
"You're reading the documentation for the next release of Grape, which should be #{ctx.release.next_version}.\n" \
27+
"The current stable release is [#{ctx.release.version}](https://github.com/ruby-grape/grape/blob/v#{ctx.release.version}/README.md)."
28+
)
29+
end
30+
workflow
31+
end
32+
end

0 commit comments

Comments
 (0)