Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 83 additions & 28 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name: Release
on:
push:
tags: ["v*"]
workflow_dispatch:

permissions: {}

Expand All @@ -16,13 +17,15 @@ jobs:
permissions:
contents: read

release:
build:
needs: test
runs-on: ubuntu-latest
environment: release
permissions:
contents: write
id-token: write
contents: read
outputs:
version: ${{ steps.metadata.outputs.version }}
tag: ${{ steps.metadata.outputs.tag }}
prerelease: ${{ steps.metadata.outputs.prerelease }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
Expand All @@ -34,40 +37,92 @@ jobs:
- run: bundle install

- name: Verify tagged commit is on main
if: github.event_name == 'push'
run: |
if ! git merge-base --is-ancestor "${GITHUB_SHA}" origin/main; then
echo "tag ${GITHUB_REF_NAME} (${GITHUB_SHA}) is not on main" >&2
exit 1
fi

- name: Verify tag matches Drip::VERSION
run: |
version="$(bundle exec ruby -Ilib -e 'require "drip/version"; print Drip::VERSION')"
if [ "${GITHUB_REF_NAME}" != "v${version}" ]; then
echo "tag ${GITHUB_REF_NAME} does not match Drip::VERSION v${version}" >&2
exit 1
fi
- name: Read release metadata
id: metadata
env:
TAG: ${{ github.event_name == 'push' && github.ref_name || '' }}
run: bundle exec rake "release:metadata[${TAG}]" >> "${GITHUB_OUTPUT}"

- name: Extract release notes from CHANGELOG.md
- name: Build gem and release notes
env:
VERSION: ${{ steps.metadata.outputs.version }}
run: |
version="${GITHUB_REF_NAME#v}"
awk -v v="$version" '
/^## \[/ { in_section = index($0, "## [" v "]") == 1; next }
in_section { print }
' CHANGELOG.md > "${RUNNER_TEMP}/release-notes.md"
if ! grep -q '[^[:space:]]' "${RUNNER_TEMP}/release-notes.md"; then
echo "no CHANGELOG.md section found for ${version}" >&2
exit 1
fi
mkdir dist
gem build drip-ruby.gemspec --output "dist/drip-ruby-${VERSION}.gem"
bundle exec rake "release:notes[${VERSION}]" > dist/release-notes.md
cat dist/release-notes.md >> "${GITHUB_STEP_SUMMARY}"

- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
with:
name: release
path: dist/
if-no-files-found: error

- uses: rubygems/release-gem@7f9650160c1a4e7989fdc9855807bdbd421d8b6b # v1.4.1
release:
if: github.event_name == 'push'
needs: build
runs-on: ubuntu-latest
environment: release
permissions:
contents: write
id-token: write
steps:
- uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
with:
name: release
path: dist
- uses: ruby/setup-ruby@14594264cd68ce8a2345dd349bc3d138a4ef85c8 # v1.327.0
with:
ruby-version: "3.4"
- uses: rubygems/configure-rubygems-credentials@bc6dd217f8a4f919d6835fcfefd470ef821f5c44 # v1.0.0

- name: Create GitHub release
- name: Create or update the draft GitHub release
env:
GH_TOKEN: ${{ github.token }}
TAG: ${{ needs.build.outputs.tag }}
PRERELEASE: ${{ needs.build.outputs.prerelease }}
run: |
draft="$(gh release view "${TAG}" --repo "${GITHUB_REPOSITORY}" --json isDraft --jq .isDraft 2>/dev/null || echo missing)"
case "${draft}" in
missing)
flags=(--draft --verify-tag --title "${TAG}" --notes-file dist/release-notes.md)
if [ "${PRERELEASE}" = "true" ]; then flags+=(--prerelease); fi
gh release create "${TAG}" dist/*.gem --repo "${GITHUB_REPOSITORY}" "${flags[@]}"
;;
true)
gh release upload "${TAG}" dist/*.gem --repo "${GITHUB_REPOSITORY}" --clobber
;;
*)
echo "release ${TAG} is already published"
;;
esac

- name: Push gem to RubyGems
env:
VERSION: ${{ needs.build.outputs.version }}
run: |
gh release create "${GITHUB_REF_NAME}" pkg/*.gem \
--repo "${GITHUB_REPOSITORY}" \
--title "${GITHUB_REF_NAME}" \
--notes-file "${RUNNER_TEMP}/release-notes.md" \
--verify-tag
if curl -fsS https://rubygems.org/api/v1/versions/drip-ruby.json |
jq -e --arg v "${VERSION}" 'any(.[]; .number == $v)' >/dev/null; then
echo "drip-ruby ${VERSION} is already on RubyGems"
else
gem push "dist/drip-ruby-${VERSION}.gem"
fi

publish:
needs: [build, release]
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Publish the GitHub release
env:
GH_TOKEN: ${{ github.token }}
TAG: ${{ needs.build.outputs.tag }}
run: gh release edit "${TAG}" --repo "${GITHUB_REPOSITORY}" --draft=false
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ Metrics/ClassLength:
Metrics/BlockLength:
Exclude:
- 'test/**/*'
- 'rakelib/**/*.rake'
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

[main]: https://github.com/DripEmail/drip-ruby/compare/v3.5.0...HEAD
[Unreleased]: https://github.com/DripEmail/drip-ruby/compare/v3.5.0...HEAD

### Changed
- Raised `required_ruby_version` to `>= 3.2`; Ruby 3.1 is no longer supported.
- CI runs the test suite on Ruby 3.2, 3.3, 3.4, and 4.0.
- Releases are published from GitHub Actions when a `v*` tag on `main` is
pushed, after the CI suite passes.
pushed, after the CI suite passes. `rake release:prepare` and
`rake release:tag` drive the release; `rake release` (from Bundler) is
removed. Prerelease versions (`X.Y.Z.rcN`) are supported.
- The gem package contains only `lib/`, `LICENSE.txt`, `README.md`, and
`CHANGELOG.md`, and building it no longer requires a git checkout.

Expand Down
33 changes: 25 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,28 @@ subscriber.email

## Releasing

1. Set `Drip::VERSION` in `lib/drip/version.rb`.
2. Move the `[Unreleased]` entries in `CHANGELOG.md` under a new `## [x.y.z] - YYYY-MM-DD` heading.
3. Merge to `main`.
4. From an up-to-date `main`, tag and push: `git tag -a vX.Y.Z -m vX.Y.Z && git push origin vX.Y.Z`.

Pushing the tag triggers `.github/workflows/release.yml`, which checks that the
tag matches `Drip::VERSION`, publishes the gem to RubyGems, and creates the
GitHub release with the matching `CHANGELOG.md` section as its notes.
Releases are cut with Rake tasks and published by
`.github/workflows/release.yml` when a `v*` tag is pushed. Versions are
`X.Y.Z`, or `X.Y.Z.alphaN` / `X.Y.Z.betaN` / `X.Y.Z.rcN` for prereleases.

1. On a branch off an up-to-date `main`, run
`bundle exec rake "release:prepare[X.Y.Z]"`. It sets `Drip::VERSION`, and
for a final release moves the `[Unreleased]` entries in `CHANGELOG.md`
under a dated `## [X.Y.Z]` section. A prerelease changes only the version;
its notes come from `[Unreleased]`. The task then runs rubocop and the
test suite.
2. Commit, open a pull request, and merge it after review.
3. On an up-to-date `main`, run `bundle exec rake release:tag`. It checks the
branch, working tree, version, existing tags, RubyGems, `CHANGELOG.md`, and
the CI result for `HEAD` (via `gh`), prints the release notes, and asks for
confirmation before creating and pushing the tag.

The workflow runs the CI suite, verifies the tag is on `main` and matches
`Drip::VERSION`, builds the gem, creates a draft GitHub release with the gem
attached, pushes the gem to RubyGems, and publishes the GitHub release. Each
step skips work that is already done, so a failed run can be re-run.

`bundle exec rake release:notes` previews the notes for the current version.
Running the Release workflow manually (`workflow_dispatch`) is a dry run: it
runs the tests, builds the gem and notes, and uploads them as a workflow
artifact without publishing anything.
10 changes: 9 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "bundler/gem_helper"
require "rake/testtask"

gem_helper = Bundler::GemHelper.new(__dir__)

desc "Build the gem into pkg/"
task(:build) { gem_helper.build_gem }

desc "Build and install the gem into system gems"
task(:install) { gem_helper.install_gem }

require "rubocop/rake_task"
RuboCop::RakeTask.new

Expand Down
137 changes: 137 additions & 0 deletions rakelib/release.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# frozen_string_literal: true

require "json"
require "net/http"
require "open3"
require_relative "release_tools"

module ReleaseTasks
VERSION_FILE = "lib/drip/version.rb"
CHANGELOG = "CHANGELOG.md"
RUBYGEMS_VERSIONS = URI("https://rubygems.org/api/v1/versions/drip-ruby.json")

module_function

def capture(*command)
out, err, status = Open3.capture3(*command)
raise ReleaseTools::Error, "#{command.join(' ')} failed: #{err.strip}" unless status.success?

out.strip
rescue SystemCallError => e
raise ReleaseTools::Error, "#{command.first}: #{e.message}"
end

def git(*) = capture("git", *)
def fetch = git("fetch", "--quiet", "--tags", "origin")
def branch = git("rev-parse", "--abbrev-ref", "HEAD")
def clean? = git("status", "--porcelain").empty?
def tags = git("tag", "--list", "v*").lines(chomp: true)

def published_versions
response = Net::HTTP.get_response(RUBYGEMS_VERSIONS)
return [] if response.is_a?(Net::HTTPNotFound)
raise ReleaseTools::Error, "RubyGems lookup failed: HTTP #{response.code}" unless response.is_a?(Net::HTTPSuccess)

JSON.parse(response.body).map { |entry| entry.fetch("number") }
end

def ci_conclusion(sha)
capture("gh", "run", "list", "--workflow", "ci.yml", "--commit", sha, "--event", "push",
"--json", "conclusion", "--jq", '.[0].conclusion // ""')
end

def current_version = ReleaseTools::Version.new(string: ReleaseTools.version_from_source(File.read(VERSION_FILE)))

def abort_on(problems)
abort(["Release checks failed:", *problems.map { |problem| " - #{problem}" }].join("\n")) if problems.any?
end

def run
yield
rescue ReleaseTools::Error => e
abort(e.message)
end
end

namespace :release do
desc "Set Drip::VERSION to VERSION and roll CHANGELOG.md, on a branch off an up-to-date main"
task :prepare, [:version] do |_task, args|
ReleaseTasks.run do
ReleaseTasks.fetch
ReleaseTasks.abort_on(ReleaseTools::Checks.prepare_problems(ReleaseTools::Checks::PrepareFacts.new(
version: args[:version].to_s,
branch: ReleaseTasks.branch,
clean: ReleaseTasks.clean?,
based_on_origin_main: system("git", "merge-base", "--is-ancestor", "origin/main", "HEAD"),
tags: ReleaseTasks.tags,
changelog: File.read(ReleaseTasks::CHANGELOG)
)))

version = ReleaseTools::Version.new(string: args[:version])
File.write(ReleaseTasks::VERSION_FILE, ReleaseTools.with_version(File.read(ReleaseTasks::VERSION_FILE), version))
unless version.prerelease?
changelog = ReleaseTools::Changelog.release(File.read(ReleaseTasks::CHANGELOG), version, Date.today)
File.write(ReleaseTasks::CHANGELOG, changelog)
end

sh "bundle", "exec", "rake"
sh "git", "--no-pager", "diff", "--stat"
puts <<~NEXT

Prepared #{version.tag}. Next:
git commit -am "Release #{version.tag}"
push the branch, open a PR, and merge it after review
then, on an up-to-date main: bundle exec rake release:tag
NEXT
end
end

desc "Check that main is releasable, then create and push the v<Drip::VERSION> tag"
task :tag do
ReleaseTasks.run do
ReleaseTasks.fetch
head = ReleaseTasks.git("rev-parse", "HEAD")
changelog = File.read(ReleaseTasks::CHANGELOG)
ReleaseTasks.abort_on(ReleaseTools::Checks.tag_problems(ReleaseTools::Checks::TagFacts.new(
branch: ReleaseTasks.branch,
clean: ReleaseTasks.clean?,
head:,
origin_main: ReleaseTasks.git("rev-parse", "origin/main"),
version_source: File.read(ReleaseTasks::VERSION_FILE),
tags: ReleaseTasks.tags,
published_versions: ReleaseTasks.published_versions,
changelog:,
today: Date.today,
ci_conclusion: ReleaseTasks.ci_conclusion(head)
)))

version = ReleaseTasks.current_version
puts "Release notes for #{version.tag}:\n\n#{ReleaseTools::Changelog.notes(changelog, version)}"
print "Type #{version.tag} to create and push the tag: "
abort("Not tagged.") unless $stdin.gets.to_s.strip == version.tag

sh "git", "tag", "-a", version.tag, "-m", version.tag
sh "git", "push", "origin", version.tag
puts <<~NEXT

Pushed #{version.tag}. Watch the release:
gh run watch "$(gh run list --workflow release.yml --event push --limit 1 --json databaseId --jq '.[0].databaseId')"
NEXT
end
end

desc "Print the release notes for VERSION (default: Drip::VERSION)"
task :notes, [:version] do |_task, args|
ReleaseTasks.run do
version = args[:version] ? ReleaseTools::Version.new(string: args[:version]) : ReleaseTasks.current_version
$stdout.write(ReleaseTools::Changelog.notes(File.read(ReleaseTasks::CHANGELOG), version))
end
end

desc "Print release metadata as GitHub Actions outputs, checking TAG against Drip::VERSION when given"
task :metadata, [:tag] do |_task, args|
ReleaseTasks.run do
ReleaseTools.metadata(File.read(ReleaseTasks::VERSION_FILE), args[:tag]).each { |key, value| puts "#{key}=#{value}" }
end
end
end
32 changes: 32 additions & 0 deletions rakelib/release_tools.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

require_relative "release_tools/version"
require_relative "release_tools/changelog"
require_relative "release_tools/checks"

module ReleaseTools
class Error < StandardError; end

VERSION_CONSTANT = /VERSION = "([^"]*)"/

module_function

def version_from_source(source)
source[VERSION_CONSTANT, 1] or raise Error, "no VERSION constant found"
end

def with_version(source, version)
raise Error, "no VERSION constant found" unless source.match?(VERSION_CONSTANT)

source.sub(VERSION_CONSTANT, %(VERSION = "#{version}"))
end

def latest_version(tags) = tags.filter_map { |tag| Version.from_tag(tag) }.max

def metadata(version_source, tag)
version = Version.new(string: version_from_source(version_source))
raise Error, "tag #{tag} does not match Drip::VERSION #{version.tag}" unless tag.nil? || tag == version.tag

{ "version" => version.to_s, "tag" => version.tag, "prerelease" => version.prerelease?.to_s }
end
end
Loading
Loading