Skip to content

Commit fff0cda

Browse files
committed
Automate versioned native gem releases in one action
1 parent a9d2ebf commit fff0cda

6 files changed

Lines changed: 203 additions & 31 deletions

File tree

‎.github/workflows/native-gems.yml‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ jobs:
5959
path: pkg/*-${{ matrix.platform }}.gem
6060
if-no-files-found: error
6161
- name: Verify package metadata and contents
62-
run: |
63-
ruby -rrubygems/package -rzlib -rstringio -e 'Dir["pkg/*-linux-gnu.gem"].each { |path| Gem::Package::TarReader.new(File.open(path, "rb")) { |tar| tar.each { |entry| puts Zlib::GzipReader.new(StringIO.new(entry.read)).read.lines.grep(/platform:|cpu:|os:|version:/).join if entry.full_name == "metadata.gz" } } }'
64-
ruby script/verify-packages "$PLATFORM"
62+
run: ruby script/verify-packages "$PLATFORM"
6563
env:
6664
PLATFORM: ${{ matrix.platform }}
6765
test:

‎.github/workflows/release.yml‎

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,76 @@
1-
name: Publish RubyGems
1+
name: Release
22
on:
33
workflow_dispatch:
44
inputs:
5-
build_run_id:
6-
description: Successful Native gems run for this exact commit
5+
bump:
6+
description: Version bump
7+
type: choice
8+
default: patch
9+
options: [patch, minor, major]
10+
release_tag:
11+
description: Existing version tag to publish without a bump
712
type: string
8-
required: true
13+
required: false
14+
concurrency:
15+
group: release
16+
cancel-in-progress: false
917
permissions:
1018
contents: read
11-
actions: read
1219
jobs:
20+
version:
21+
runs-on: ubuntu-24.04
22+
permissions:
23+
contents: write
24+
outputs:
25+
sha: ${{ steps.version.outputs.sha }}
26+
tag: ${{ steps.version.outputs.tag }}
27+
steps:
28+
- uses: actions/checkout@v7
29+
with:
30+
ref: ${{ inputs.release_tag || github.ref }}
31+
fetch-depth: 0
32+
- uses: ruby/setup-ruby@v1
33+
with:
34+
ruby-version: '3.3'
35+
- id: version
36+
env:
37+
BUMP: ${{ inputs.bump }}
38+
RELEASE_TAG: ${{ inputs.release_tag }}
39+
run: ruby script/prepare-release
40+
packages:
41+
needs: version
42+
uses: ./.github/workflows/native-gems.yml
43+
with:
44+
ref: ${{ needs.version.outputs.sha }}
1345
publish:
46+
needs: [version, packages]
1447
runs-on: ubuntu-24.04
1548
environment: release
1649
permissions:
17-
contents: read
18-
actions: read
50+
contents: write
1951
id-token: write
2052
steps:
2153
- uses: actions/checkout@v7
54+
with:
55+
ref: ${{ needs.version.outputs.sha }}
2256
- uses: ruby/setup-ruby@v1
2357
with:
2458
ruby-version: '3.3'
25-
- name: Check tested build provenance
26-
env:
27-
GH_TOKEN: ${{ github.token }}
28-
BUILD_RUN_ID: ${{ inputs.build_run_id }}
29-
run: |
30-
[[ "$BUILD_RUN_ID" =~ ^[0-9]+$ ]]
31-
gh api "repos/$GITHUB_REPOSITORY/actions/runs/$BUILD_RUN_ID" > build-run.json
32-
ruby -rjson -e '
33-
run = JSON.parse(File.read("build-run.json"))
34-
abort "build is not successful" unless run["conclusion"] == "success" && run["status"] == "completed"
35-
abort "wrong workflow" unless run["path"] == ".github/workflows/native-gems.yml"
36-
abort "build commit differs from release" unless run["head_sha"] == ENV.fetch("GITHUB_SHA")
37-
'
38-
gh run download "$BUILD_RUN_ID" --repo "$GITHUB_REPOSITORY" --pattern 'gem-*' --dir artifacts
39-
mkdir pkg
40-
find artifacts -name '*.gem' -exec cp '{}' pkg/ \;
41-
ruby script/verify-packages
59+
- uses: actions/download-artifact@v7
60+
with:
61+
pattern: gem-*
62+
merge-multiple: true
63+
path: pkg
64+
- run: ruby script/verify-packages
4265
- uses: rubygems/configure-rubygems-credentials@dc5a8d8553e6ee01fc26761a49e99e733d17954a # v2.1.0
4366
- name: Publish tested native gems, then source fallback
4467
run: |
4568
for package in pkg/*-darwin.gem pkg/*-linux-gnu.gem; do
4669
gem push "$package"
4770
done
4871
gem push "$(ruby -e 'print "pkg/quack-ruby-#{Gem::Specification.load("quack-ruby.gemspec").version}.gem"')"
72+
- name: Create GitHub Release
73+
env:
74+
GH_TOKEN: ${{ github.token }}
75+
RELEASE_TAG: ${{ needs.version.outputs.tag }}
76+
run: gh release create "$RELEASE_TAG" pkg/*.gem --verify-tag --generate-notes

‎README.md‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,13 @@ packages all three Ruby ABIs into each platform gem, and runs the live suite
211211
on all twelve OS/architecture/Ruby combinations with build tools blocked.
212212
Release builds do not restore or publish compiler caches.
213213

214-
To release, run `Native gems`, then dispatch `Publish RubyGems` with its
215-
successful run ID at the same commit. The publishing workflow verifies the
216-
build's commit and package contents, then uploads those exact artifacts through
217-
RubyGems Trusted Publishing. Source and binary gems use the same version.
214+
To release, dispatch `Release` from main and choose a patch, minor or major
215+
bump. It updates the Ruby/Rust versions and locks, commits and tags the release,
216+
builds all five packages, and runs the twelve installed-binary test combinations.
217+
Only after every check passes does it publish those exact artifacts through
218+
RubyGems Trusted Publishing and create a GitHub Release with the gem assets.
219+
An optional `release_tag` publishes an existing tag without another bump; its
220+
source version must match the tag. Source and binary gems use the same version.
218221

219222
## License
220223

‎Rakefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Rake::ExtensionTask.new("quack_native", GEMSPEC) do |extension|
1616
spec.dependencies.reject! { |dependency| dependency.name == "rb_sys" }
1717
if ENV.fetch("RUBY_TARGET").end_with?("-linux")
1818
spec.platform = Gem::Platform.new("#{ENV.fetch('RUBY_TARGET')}-gnu")
19+
# RubyGems 3.5 retains the first assigned platform when serializing YAML.
20+
spec.original_platform = spec.platform.to_s
1921
spec.required_rubygems_version = ">= 3.3.22"
2022
end
2123
end

‎script/prepare-release‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env ruby
2+
require "rubygems"
3+
4+
gemspec = Dir["*.gemspec"].fetch(0)
5+
spec = Gem::Specification.load(gemspec)
6+
current = spec.version.to_s
7+
abort "release versions must use X.Y.Z" unless current.match?(/\A\d+\.\d+\.\d+\z/)
8+
tag = ENV.fetch("RELEASE_TAG", "")
9+
10+
def git(*args)
11+
system("git", *args, exception: true)
12+
end
13+
14+
if tag.empty?
15+
abort "create releases from main" unless ENV.fetch("GITHUB_REF") == "refs/heads/main"
16+
parts = current.split(".").map(&:to_i)
17+
index = %w[major minor patch].index(ENV.fetch("BUMP")) or abort "unknown version bump"
18+
parts[index] += 1
19+
((index + 1)...3).each { |position| parts[position] = 0 }
20+
version = parts.join(".")
21+
tag = "v#{version}"
22+
changes = {gemspec => [/spec\.version = "#{Regexp.escape(current)}"/, "spec.version = \"#{version}\""]}
23+
Dir["**/Gemfile*.lock"].each do |path|
24+
changes[path] = [/^( #{Regexp.escape(spec.name)} \()#{Regexp.escape(current)}(\))$/, "\\1#{version}\\2"]
25+
end
26+
if spec.name == "quack-ruby"
27+
changes["lib/quack.rb"] = [/VERSION = "#{Regexp.escape(current)}"/, "VERSION = \"#{version}\""]
28+
changes["ext/quack/Cargo.toml"] = [/^version = "#{Regexp.escape(current)}"/, "version = \"#{version}\""]
29+
changes["ext/quack/Cargo.lock"] = [/(name = "quack-ruby"\nversion = ")#{Regexp.escape(current)}(")/, "\\1#{version}\\2"]
30+
end
31+
changes.each do |path, (pattern, replacement)|
32+
source = File.read(path)
33+
abort "missing current version in #{path}" unless source.match?(pattern)
34+
File.write(path, source.sub(pattern, replacement))
35+
end
36+
git("diff", "--check")
37+
git("config", "user.name", "github-actions[bot]")
38+
git("config", "user.email", "github-actions[bot]@users.noreply.github.com")
39+
git("add", "--", *changes.keys)
40+
git("commit", "-m", "Release #{tag}")
41+
git("tag", "-a", tag, "-m", "Release #{tag}")
42+
git("push", "--atomic", "origin", "HEAD:main", "refs/tags/#{tag}")
43+
else
44+
abort "release tag does not match source version" unless tag == "v#{current}"
45+
tagged_sha = IO.popen(["git", "rev-parse", "refs/tags/#{tag}^{commit}"], &:read).strip
46+
head_sha = IO.popen(["git", "rev-parse", "HEAD"], &:read).strip
47+
abort "checkout does not match release tag" unless tagged_sha == head_sha
48+
version = current
49+
end
50+
51+
sha = IO.popen(["git", "rev-parse", "HEAD"], &:read).strip
52+
File.open(ENV.fetch("GITHUB_OUTPUT"), "a") do |output|
53+
output.puts "version=#{version}", "tag=#{tag}", "sha=#{sha}"
54+
end

‎test/release_test.rb‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
require "minitest/autorun"
2+
require "tmpdir"
3+
require "fileutils"
4+
require "open3"
5+
6+
class ReleaseTest < Minitest::Test
7+
ROOT = File.expand_path("..", __dir__)
8+
CURRENT = Gem::Specification.load(Dir[File.join(ROOT, "*.gemspec")].fetch(0)).version.to_s
9+
10+
def setup
11+
@directory = Dir.mktmpdir("quack-release")
12+
@checkout = File.join(@directory, "checkout")
13+
@remote = File.join(@directory, "remote.git")
14+
FileUtils.mkdir_p(@checkout)
15+
paths = Dir.chdir(ROOT) { Dir["*.gemspec", "**/Gemfile*.lock"] }
16+
paths.concat(%w[lib/quack.rb ext/quack/Cargo.toml ext/quack/Cargo.lock]) if File.file?(File.join(ROOT, "quack-ruby.gemspec"))
17+
paths.each do |path|
18+
target = File.join(@checkout, path)
19+
FileUtils.mkdir_p(File.dirname(target))
20+
FileUtils.cp(File.join(ROOT, path), target)
21+
end
22+
command("git", "init", "--bare", @remote)
23+
command("git", "init", "-b", "main")
24+
command("git", "config", "user.name", "Release test")
25+
command("git", "config", "user.email", "release@example.invalid")
26+
command("git", "add", ".")
27+
command("git", "commit", "-m", "Initial")
28+
command("git", "remote", "add", "origin", @remote)
29+
command("git", "push", "origin", "main")
30+
end
31+
32+
def teardown
33+
FileUtils.remove_entry(@directory)
34+
end
35+
36+
def command(*args)
37+
output, status = Open3.capture2e(*args, chdir: @checkout)
38+
assert status.success?, output
39+
output.strip
40+
end
41+
42+
def release(bump: "patch", tag: "")
43+
Open3.capture2e({"BUMP" => bump, "RELEASE_TAG" => tag, "GITHUB_REF" => "refs/heads/main",
44+
"GITHUB_OUTPUT" => File.join(@directory, "outputs")},
45+
RbConfig.ruby, File.join(ROOT, "script/prepare-release"), chdir: @checkout)
46+
end
47+
48+
%w[major minor patch].each_with_index do |bump, index|
49+
define_method("test_#{bump}_bumps_and_pushes_consistent_versions") do
50+
parts = CURRENT.split(".").map(&:to_i)
51+
parts[index] += 1
52+
((index + 1)...3).each { |position| parts[position] = 0 }
53+
version = parts.join(".")
54+
output, status = release(bump: bump)
55+
assert status.success?, output
56+
spec = Gem::Specification.load(Dir[File.join(@checkout, "*.gemspec")].fetch(0))
57+
assert_equal version, spec.version.to_s
58+
Dir[File.join(@checkout, "**/Gemfile*.lock")].each do |lock|
59+
assert_includes File.read(lock), " #{spec.name} (#{version})"
60+
end
61+
if spec.name == "quack-ruby"
62+
assert_includes File.read(File.join(@checkout, "lib/quack.rb")), "VERSION = \"#{version}\""
63+
assert_includes File.read(File.join(@checkout, "ext/quack/Cargo.toml")), "version = \"#{version}\""
64+
assert_includes File.read(File.join(@checkout, "ext/quack/Cargo.lock")), "name = \"quack-ruby\"\nversion = \"#{version}\""
65+
end
66+
assert_equal command("git", "rev-parse", "HEAD"), command("git", "--git-dir", @remote, "rev-parse", "refs/tags/v#{version}^{commit}")
67+
assert_empty command("git", "status", "--porcelain")
68+
end
69+
end
70+
71+
def test_existing_tag_keeps_version_and_commit
72+
command("git", "tag", "v#{CURRENT}")
73+
before = command("git", "rev-parse", "HEAD")
74+
output, status = release(tag: "v#{CURRENT}")
75+
assert status.success?, output
76+
assert_equal before, command("git", "rev-parse", "HEAD")
77+
assert_includes File.read(File.join(@directory, "outputs")), "tag=v#{CURRENT}"
78+
end
79+
80+
def test_mismatched_tag_is_rejected
81+
command("git", "tag", "v9999.0.0")
82+
output, status = release(tag: "v9999.0.0")
83+
refute status.success?
84+
assert_includes output, "release tag does not match source version"
85+
assert_empty command("git", "status", "--porcelain")
86+
end
87+
end

0 commit comments

Comments
 (0)