Release your Ruby gem from GitHub Actions without maintaining custom release scripts or storing a RubyGems API key.
Implements Releases as Code for Ruby gems. You start a GitHub Actions workflow, and it:
- runs your tests,
- publishes the gem to RubyGems,
- updates the changelog (optional),
- creates a Git tag and a GitHub Release,
- bumps the version for the next development iteration.
See also npm-package-release and rust-crate-release.
You need Ruby 3.3+, GitHub Actions, and RubyGems Trusted Publishing.
Add ruby-gem-release and commonmarker ~> 2.10 (needed only for Markdown changelogs) to your development dependencies. Then install the release tasks in your Rakefile, naming your version file and changelog format:
require 'ruby-gem-release/workflow/ruby_gem'
require 'ruby-gem-release/changelog'
RubyGemRelease::Workflow::RubyGem.new(
version: RubyGemRelease::Version::RubyConstant.new(path: 'lib/example/version.rb', constant: 'VERSION'),
changelog: RubyGemRelease::Changelog::KeepAChangelog.new
).install_tasksWith these defaults, a release:
- uses the only gemspec in the repository root,
- fails if the changelog has no pending notes,
- moves the pending notes under a new release heading,
- publishes to RubyGems and GitHub Releases,
- bumps the patch version afterwards (pass
next_version: :minoror:majorto bump another part), - dates the release in UTC.
If your Rakefile also installs the Bundler gem tasks, disable the local Bundler release tasks so nobody can publish around the workflow. See Bundler Release Tasks.
Every release starts by checking that the checkout is clean, and Ruby setup installs gems inside it. Add what it writes to .gitignore, leaving out the lockfile if you commit it:
/vendor/bundle/
/.bundle/
/Gemfile.lockWith a release Gemfile, ignore its lockfile instead, such as /gemfiles/release.gemfile.lock. A dry run fails and lists any lines that are missing.
Copy the sample workflow to .github/workflows/release.yml and set your Ruby version:
name: Release
on:
workflow_dispatch:
inputs:
version:
description: Optional release version overriding the checked-in version.
type: string
required: false
rubygems-owner:
description: RubyGems account ID for first publication; omit for an existing gem.
type: string
required: false
permissions:
contents: write
id-token: write
jobs:
release:
uses: dblock/ruby-gem-release/.github/workflows/release.yml@v0.9.0
with:
ruby-version: '3.3'
version: ${{ inputs.version }}
rubygems-owner: ${{ inputs.rubygems-owner }}Trusted Publishing lets GitHub Actions publish to RubyGems without a stored API key.
- In your GitHub repository, open Settings → Environments and create an environment named
release. Or rungh api --method PUT repos/{owner}/{repo}/environments/releasefrom your repository directory. - On RubyGems, open the pending trusted publishers page for a new gem, or your gem's Trusted publishers page (for example
https://rubygems.org/gems/example/trusted_publishers) for an existing gem. - Create a trusted publisher with your gem's repository, workflow filename
release.yml, and environmentrelease. - Set the separate workflow repository fields to
dblockandruby-gem-release. This tells RubyGems to trust this project's reusable workflow.
To check the setup without releasing, run a dry run.
Anyone with write access can now dispatch a release, without admin access or RubyGems credentials. The release environment alone restricts nothing. To require approval from a maintainer, configure required reviewers and deployment branches.
Your branch rules must allow the workflow to push release commits and tags (branch permissions); each release checks this before publishing. Run your tests, review the changes, then dispatch from your default branch. Dispatching starts a real release; with required reviewers, it waits for approval.
Do not merge into the default branch while a release runs. If that happens, see recovery.
First release: pass your numeric RubyGems account ID, shown on the Pending Trusted Publishers page, as rubygems-owner. This account will own the new gem:
gh workflow run release.yml -f rubygems-owner=YOUR_ACCOUNT_IDLater releases: omit rubygems-owner. The trusted publisher is still required:
gh workflow run release.ymlTo release a different version, enter it in the dispatch form or pass -f version=1.2.3. The workflow writes it to your version file, and the next development version is bumped from it.
For a real-world example of adding this to an existing gem, see ruby-link-checker #26.
Replace only what you need. See the standard workflow and Detailed Usage for examples.
- Project conventions: choose version objects and changelog formats, or write your own.
- Extra steps: add lifecycle hooks for checks, file edits, publication observers, and notifications without replacing the pipeline.
- Custom callbacks: replace validation, preparation, publication, and notifications. Callbacks receive an execution context with release data, step history, and the workflow.
- Semantic releases: opt into Conventional Commits, versions from release history, and generated notes.
- Packages and registries: override how the gem is built or where it is published.
- Release hosting: replace or turn off GitHub Release creation without affecting gem publication or Git.
- CI platforms: choose the platform, registry, and hosting independently. Supply an execution provider for environment variables, metadata, temporary storage, and authentication. GitHub Actions is the only built-in platform; others need their own adapter and pipeline.
- Actions options: override commands, inputs, and timezone through the Actions Reference, or use the Composite Action as a step in your own job.
- Testing: use optional RSpec helpers and local publishing stand-ins to test your release customizations and the files they change.
For complete examples, see releasing ruby-gem-release itself and GitLab and semantic releases.
The defaults include clean-checkout checks, duplicate-version checks, and preserved recovery artifacts. Custom publication callbacks must keep the documented order and artifact checks.
See UPGRADING.md for breaking changes and migration steps. Upgrade the gem and pinned workflow or action together.
See CONTRIBUTING.md, DESIGN.md, CHANGELOG.md, and RELEASING.md.
Distributed under the MIT License.
