From bf991184a7aa06f839d7668c6838846a934b3b62 Mon Sep 17 00:00:00 2001 From: "Daniel (dB.) Doubrovkine" Date: Mon, 23 Nov 2026 17:21:27 -0500 Subject: [PATCH] Release with ruby-gem-release Add a manually dispatched Release workflow that uses the ruby-gem-release 0.9.0 reusable workflow with RubyGems Trusted Publishing and optional dry runs. The Intridea changelog handler dates the (Next) section and prepares the next patch version. Disable the local Bundler release tasks, load the version file relative to the gemspec so release fixtures build their own version, and cover the release with ruby-gem-release RSpec helpers. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011Gr21iR4JxWxGGyXdJ3uc9 --- .github/workflows/release.yml | 30 ++++++++++++++ .gitignore | 1 + .rubocop.yml | 13 ++++++ .spellcheck.txt | 5 +++ CHANGELOG.md | 1 + Gemfile | 2 + RELEASING.md | 54 +++++++++--------------- Rakefile | 12 ++++++ mongoid-locker.gemspec | 4 +- script/release.rb | 15 +++++++ spec/release/release_spec.rb | 78 +++++++++++++++++++++++++++++++++++ 11 files changed, 177 insertions(+), 38 deletions(-) create mode 100644 .github/workflows/release.yml create mode 100644 script/release.rb create mode 100644 spec/release/release_spec.rb diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..0143731 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,30 @@ +name: Release + +on: + workflow_dispatch: + inputs: + version: + description: Optional release version overriding the checked-in version. + type: string + required: false + dry-run: + description: Check release prerequisites, such as RubyGems Trusted Publishing, without releasing. + type: boolean + default: false + +permissions: + contents: write + id-token: write + +jobs: + release: + if: github.repository == 'mongoid/mongoid-locker' && github.ref == 'refs/heads/master' + uses: dblock/ruby-gem-release/.github/workflows/release.yml@v0.9.0 + with: + ruby-version: '3.3' + version: ${{ inputs.version }} + dry-run: ${{ inputs.dry-run }} + test: | + docker run --detach --name mongodb --publish 27017:27017 mongo:8.0 + until docker exec mongodb mongosh --quiet --eval 'db.adminCommand({ping: 1})'; do sleep 1; done + bundle exec rake diff --git a/.gitignore b/.gitignore index 555c9d5..2ddda52 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ Gemfile.lock # guard tmp +/vendor/bundle diff --git a/.rubocop.yml b/.rubocop.yml index 83dbde2..e8fa011 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -38,7 +38,12 @@ Style/HashTransformValues: Enabled: true RSpec/MultipleExpectations: + inherit_mode: + merge: + - Exclude Enabled: false + Exclude: + - spec/release/release_spec.rb RSpec/LeakyConstantDeclaration: Enabled: false @@ -144,3 +149,11 @@ RSpec/BeforeAfterAll: - Exclude Exclude: - spec/support/configurations_shared_context.rb + +# One release example verifies the tag, the next development commit, and their files together. +RSpec/ExampleLength: + inherit_mode: + merge: + - Exclude + Exclude: + - spec/release/release_spec.rb diff --git a/.spellcheck.txt b/.spellcheck.txt index 279001a..ac58206 100644 --- a/.spellcheck.txt +++ b/.spellcheck.txt @@ -6,6 +6,7 @@ api backoff bbed BSON +Bundler cesarizu chainable CHANGELOG @@ -26,6 +27,7 @@ Feldman Fixnum forwardable Gemfile +gemspec github InvalidParameter isdbgrid @@ -38,6 +40,7 @@ md Mongoid mongoid MongoidHistory +MongoidLockerRelease mongos mooremo msg @@ -45,8 +48,10 @@ nchainani ObjectId param pschrammel +Rakefile randikabanura rb +README rsosag RSpec rspec diff --git a/CHANGELOG.md b/CHANGELOG.md index 10a581d..91c7e4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Your contribution here. * [#115](https://github.com/mongoid/mongoid-locker/pull/115): Lint Markdown, YAML, and spelling with rubocop-md, rubocop-prose, rubocop-spellcheck, and rubocop-yaml in a separate Lint workflow, and clean up documentation - [@dblock](https://github.com/dblock). * [#116](https://github.com/mongoid/mongoid-locker/pull/116): Run CI pushes only on the default branch, cancel superseded pull request runs, skip duplicate lint in the test matrix, and bound job times - [@dblock](https://github.com/dblock). +* [#117](https://github.com/mongoid/mongoid-locker/pull/117): Release with ruby-gem-release from a manually dispatched GitHub Actions workflow using RubyGems Trusted Publishing, with dry runs - [@dblock](https://github.com/dblock). ### 2.3.0 (2026-10-15) diff --git a/Gemfile b/Gemfile index 214d3e6..53cb3ed 100644 --- a/Gemfile +++ b/Gemfile @@ -44,6 +44,7 @@ group :development, :test do gem 'rspec', '~> 3.9' gem 'simplecov', require: false if RUBY_ENGINE == 'ruby' && Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.3') + gem 'commonmarker', '~> 2.10', require: false gem 'rubocop', '~> 1.92' gem 'rubocop-md', '~> 3.0', require: false gem 'rubocop-prose', '~> 0.2', require: false @@ -51,5 +52,6 @@ group :development, :test do gem 'rubocop-rspec', '~> 3.10', require: false gem 'rubocop-spellcheck', '~> 0.2', require: false gem 'rubocop-yaml', '~> 0.1.0', require: false + gem 'ruby-gem-release', '0.9.0', require: false end end diff --git a/RELEASING.md b/RELEASING.md index 2a18ccb..ecaa107 100644 --- a/RELEASING.md +++ b/RELEASING.md @@ -1,51 +1,35 @@ -# Releasing Mongoid::Locker +# Releasing Mongoid-Locker -## Prepare - -Use the upstream checkout on a clean `master`, verify `origin` points to `mongoid/mongoid-locker`, and run `git pull --ff-only`. Review existing CI results for supported platforms. Use patch versions for fixes, minor versions for backward-compatible features, and major versions for breaking changes. +Releases use [ruby-gem-release](https://github.com/dblock/ruby-gem-release) 0.9.0 through a manually dispatched [Release workflow](.github/workflows/release.yml). Keep the `ruby-gem-release` gem in [Gemfile](Gemfile) and the reusable workflow in `release.yml` pinned to the same version. The workflow publishes through RubyGems Trusted Publishing, so no RubyGems API key or one-time password is needed. It only runs from `master` in `mongoid/mongoid-locker`. The local Bundler `rake release`, `release:rubygem_push`, and `release:source_control_push` tasks are disabled. -Set the version in `lib/mongoid/locker/version.rb`, date its section in `CHANGELOG.md` using `YYYY-MM-DD`, and remove that section's contribution placeholder. +## One-Time Setup -```sh -bundle install -bundle exec rspec -bundle exec rubocop -git add CHANGELOG.md lib/mongoid/locker/version.rb RELEASING.md -git commit -m "Preparing for release, 2.3.0." -git push origin master -``` +Before the first automated release, a maintainer must: -Do not wait for the preparation commit's CI checks before publishing. +1. Create the GitHub environment `release` in `mongoid/mongoid-locker`, for example with `gh api --method PUT repos/mongoid/mongoid-locker/environments/release`, and restrict its deployment branches to `master`. Add required reviewers to require maintainer approval before publication. +2. At [mongoid-locker's trusted publishers](https://rubygems.org/gems/mongoid-locker/trusted_publishers), add a GitHub Actions publisher with repository owner `mongoid`, repository `mongoid-locker`, workflow filename `release.yml`, environment `release`, and the separate workflow repository owner `dblock` and repository `ruby-gem-release`. +3. Allow GitHub Actions to push release commits and `v*` tags to `master`. See the upstream [setup and permissions guidance](https://github.com/dblock/ruby-gem-release/blob/v0.9.0/RELEASING.md#one-time-setup). -## Publish RubyGems and GitHub +Verify the setup without publishing with a [dry run](https://github.com/dblock/ruby-gem-release/blob/v0.9.0/docs/actions/release.md#dry-run), which never edits files, builds, pushes, or publishes: ```sh -bundle exec rake release +gh workflow run release.yml --repo mongoid/mongoid-locker --ref master -f dry-run=true ``` -Complete RubyGems MFA when requested. This builds the gem, pushes the release tag, and publishes to RubyGems. If interrupted, inspect the existing tag and published version before retrying. Never move a published tag. If only publication is missing, run `gem push pkg/mongoid-locker-2.3.0.gem` with a fresh MFA code. +## Prepare + +Start with a clean, up-to-date `master` and verify that the latest [CI checks](https://github.com/mongoid/mongoid-locker/actions) pass. Follow semantic versioning: patch for fixes, minor for backward-compatible features, major for breaking changes. Check `VERSION` in [lib/mongoid/locker/version.rb](lib/mongoid/locker/version.rb) and the matching `(Next)` heading in [CHANGELOG.md](CHANGELOG.md), and change both in a pull request if needed, or pass a `version` override when dispatching. The workflow dates the `(Next)` heading using `YYYY/MM/DD` and removes the `* Your contribution here.` placeholder; a section without notes cannot be released. + +Release automation requires MRI Ruby 3.3 or newer; the release dependency and tasks are skipped on older Ruby versions and JRuby. Release specs in [spec/release](spec/release) run the actual release tasks against a local fixture with the [ruby-gem-release RSpec helpers](https://github.com/dblock/ruby-gem-release/blob/v0.9.0/docs/testing.md), without contacting RubyGems or GitHub. -A tag is not a GitHub release. After verifying RubyGems publication, create a matching release with the changelog notes: +## Publish ```sh -version=2.3.0 -notes="$(mktemp)" -awk -v version="$version" ' - /^### / { - if (found) exit - if ($2 == version) found = 1 - next - } - found { print } -' CHANGELOG.md > "$notes" -gh release create "v$version" --repo mongoid/mongoid-locker --verify-tag --title "$version" --notes-file "$notes" --latest -rm "$notes" -curl --fail --silent --show-error "https://rubygems.org/api/v2/rubygems/mongoid-locker/versions/$version.json" -gh release view "v$version" --repo mongoid/mongoid-locker +gh workflow run release.yml --repo mongoid/mongoid-locker --ref master ``` -Verify both the published gem and non-draft GitHub release. +Pass `-f version=x.y.z` to release a different version. Dispatching starts a real release; do not merge into `master` while it runs. The workflow prepares the release commit, starts MongoDB 8.0, runs lint and specs on Ruby 3.3, builds the gem, preserves recovery artifacts, publishes to RubyGems, pushes the release commit and `v` tag, and creates a GitHub Release with the changelog notes and the gem attached. It then increments the patch version, adds a new `(Next)` section, and pushes the next development commit. Verify the published version on [RubyGems](https://rubygems.org/gems/mongoid-locker), the tag, the [GitHub Release](https://github.com/mongoid/mongoid-locker/releases), and the next development commit. -## Prepare the Next Version +## Recover a Partial Release -Increment the development patch version in `lib/mongoid/locker/version.rb`, for example to `2.3.1`. Add `### 2.3.1 (Next)` and a contribution placeholder above the released changelog section. Commit and push these changes to `master`, leaving the release tag unchanged. +Inspect the failed run, RubyGems, remote commits and tags, and the GitHub Release before retrying. Publication may have succeeded even if a later step failed. Never move a published tag or republish a version with different contents. Use the run's retained recovery artifacts and the upstream [recovery instructions](https://github.com/dblock/ruby-gem-release/blob/v0.9.0/docs/actions/release.md#recovery-artifacts). diff --git a/Rakefile b/Rakefile index 06a5623..4df8cb4 100644 --- a/Rakefile +++ b/Rakefile @@ -3,6 +3,18 @@ require 'rubygems' require 'bundler/setup' require 'bundler/gem_tasks' + +if RUBY_ENGINE == 'ruby' && Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.3') && + Gem.loaded_specs.key?('ruby-gem-release') + require_relative 'script/release' + MongoidLockerRelease.workflow.install_tasks + + %w[release release:rubygem_push release:source_control_push].each do |name| + Rake::Task[name].clear + desc 'Disabled; releases run through GitHub Actions, see RELEASING.md' + task(name) { abort "#{name} is disabled; releases run through GitHub Actions, see RELEASING.md" } + end +end require 'rake' require 'rspec/core' diff --git a/mongoid-locker.gemspec b/mongoid-locker.gemspec index 5760801..87f32a6 100644 --- a/mongoid-locker.gemspec +++ b/mongoid-locker.gemspec @@ -1,8 +1,6 @@ # frozen_string_literal: true -$LOAD_PATH.push File.expand_path('lib', __dir__) - -require 'mongoid/locker/version' +require_relative 'lib/mongoid/locker/version' Gem::Specification.new do |s| s.name = 'mongoid-locker' diff --git a/script/release.rb b/script/release.rb new file mode 100644 index 0000000..0a54951 --- /dev/null +++ b/script/release.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require 'ruby-gem-release/changelog' +require 'ruby-gem-release/workflow/ruby_gem' + +# Builds the ruby-gem-release workflow used by the Rakefile, the Release GitHub Actions workflow, and spec/release. +module MongoidLockerRelease + def self.workflow(**options) + RubyGemRelease::Workflow::RubyGem.new( + version: RubyGemRelease::Version::RubyConstant.new(path: 'lib/mongoid/locker/version.rb', constant: 'VERSION'), + changelog: RubyGemRelease::Changelog::Intridea.new(placeholder: '* Your contribution here.'), + **options + ) + end +end diff --git a/spec/release/release_spec.rb b/spec/release/release_spec.rb new file mode 100644 index 0000000..5c0f220 --- /dev/null +++ b/spec/release/release_spec.rb @@ -0,0 +1,78 @@ +# frozen_string_literal: true + +if RUBY_ENGINE == 'ruby' && Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.3') && + Gem.loaded_specs.key?('ruby-gem-release') + require_relative '../../script/release' + require 'ruby-gem-release/rspec' + + RSpec.describe MongoidLockerRelease do + include RubyGemRelease::RSpec + + let(:root) { File.expand_path('../..', __dir__) } + let(:version_path) { 'lib/mongoid/locker/version.rb' } + let(:fixture_files) do + %w[ + mongoid-locker.gemspec script/release.rb lib/**/*.rb CHANGELOG.md + CONTRIBUTING.md README.md RELEASING.md UPGRADING.md + LICENSE.txt + ] + end + let(:placeholder) { '* Your contribution here.' } + let(:note) { '* [#1](https://github.com/mongoid/mongoid-locker/pull/1): Added a feature.' } + + def changelog(notes) + [ + '### 1.2.0 (Next)', '', notes, placeholder, '', + '### 1.1.0 (2026/01/01)', '', '* Previous release.', '' + ].join("\n") + end + + def write_version(project, version) + source = File.read(File.join(root, version_path)) + project.write(version_path, source.sub(/VERSION = (['"])[^'"]+\1/, "VERSION = '#{version}'")) + end + + def with_project(notes) + with_release_project do |project| + project.environment.merge!(ENV.to_h.slice('MONGOID_VERSION')) + project.copy_files(path: root, files: fixture_files) + project.configure_workflow(require: 'script/release', builder: 'MongoidLockerRelease.workflow') + project.write('CHANGELOG.md', changelog(notes)) + write_version(project, '1.2.0') + project.commit! + yield project + end + end + + it 'builds the standard ruby-gem-release workflow' do + expect(described_class.workflow).to be_a(RubyGemRelease::Workflow::RubyGem) + end + + it 'dates the release and prepares the next patch version' do + with_project(note) do |project| + project.release! + + released = project.remote_file('v1.2.0', 'CHANGELOG.md') + expect(released).to match(%r{\A### 1\.2\.0 \(\d{4}/\d{2}/\d{2}\)\n\n}) + expect(released).to include(note) + expect(released).not_to include('(Next)', placeholder) + expect(project.remote_file('v1.2.0', version_path)).to include("VERSION = '1.2.0'") + pending = project.remote_file('master', 'CHANGELOG.md') + expect(pending).to start_with("### 1.2.1 (Next)\n\n#{placeholder}\n\n### 1.2.0 (") + expect(project.remote_file('master', version_path)).to include("VERSION = '1.2.1'") + expect(project.git('status', '--porcelain')).to be_empty + end + end + + it 'rejects placeholder-only notes without changing files or remote refs' do + with_project('') do |project| + original = project.git('rev-parse', 'origin/master') + project.run_task('release:workflow:capture') + + expect { project.run_task('release:validate') }.to raise_error(RuntimeError, /no pending changelog notes/) + expect(project.git('status', '--porcelain')).to be_empty + expect(project.git('rev-parse', 'origin/master')).to eq(original) + end + end + end +end