diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..9fb3602 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,42 @@ +name: Release + +on: + push: + tags: + - "[0-9]+.[0-9]+.[0-9]+" + +permissions: + contents: write + id-token: write + +jobs: + release: + name: Release + runs-on: ubuntu-latest + environment: release + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Set up Ruby + uses: ruby/setup-ruby@7f797815d8adceb1c412e7abefdd1ab4abc47a5f # v1.335.0 + with: + bundler-cache: true + + - name: Check tag matches version.rb + run: | + version="$(ruby -Ilib -remailable/version -e 'print Emailable::VERSION')" + if [ "$GITHUB_REF_NAME" != "$version" ]; then + echo "::error::Tag $GITHUB_REF_NAME does not match version.rb version $version. Run 'bundle exec rake release:prepare[x.y.z]'." + exit 1 + fi + + - name: Run tests + run: bundle exec rake test + + - name: Publish to RubyGems + uses: rubygems/release-gem@7f9650160c1a4e7989fdc9855807bdbd421d8b6b # v1.4.1 + + - name: Create GitHub release + run: gh release create "$GITHUB_REF_NAME" --verify-tag --generate-notes + env: + GH_TOKEN: ${{ github.token }} diff --git a/README.md b/README.md index 37056e5..779d77c 100644 --- a/README.md +++ b/README.md @@ -153,6 +153,14 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. +## Cutting a Release + +Run `bundle exec rake "release:prepare[x.y.z]"` on an up to date `master` with +a clean tree, then `git push --follow-tags`. It runs the tests and RuboCop, +bumps `lib/emailable/version.rb`, commits, and tags `x.y.z`. Pushing the tag +triggers the release workflow, which publishes to RubyGems and creates the +GitHub release. + ## Contributing Bug reports and pull requests are welcome on GitHub at diff --git a/Rakefile b/Rakefile index 121155c..66174a4 100644 --- a/Rakefile +++ b/Rakefile @@ -1,6 +1,11 @@ require 'bundler/gem_tasks' require 'rake/testtask' +# Tags are bare versions like 4.2.3, not Bundler's default v4.2.3. +Bundler::GemHelper.instance.define_singleton_method(:version_tag) do + version.to_s +end + Rake::TestTask.new(:test) do |t| t.libs << 'test' t.libs << 'lib' @@ -8,3 +13,33 @@ Rake::TestTask.new(:test) do |t| end task :default => :test + +namespace :release do + desc 'Check, bump, commit, and tag a release, e.g. rake release:prepare[1.2.3]' + task :prepare, [:version] do |_, args| + version = args[:version].to_s + abort 'error: version must be x.y.z' unless version.match?(/\A\d+\.\d+\.\d+\z/) + abort 'error: working tree not clean' unless `git status --porcelain`.empty? + abort 'error: release from master' unless `git branch --show-current`.strip == 'master' + + sh 'git fetch -q --tags origin master' + unless `git rev-parse HEAD` == `git rev-parse origin/master` + abort 'error: master is not in sync with origin' + end + + current = Bundler::GemHelper.instance.gemspec.version + unless Gem::Version.new(version) > current + abort "error: version must be greater than #{current}" + end + abort "error: tag #{version} already exists" unless `git tag -l #{version}`.empty? + + Rake::Task[:test].invoke + sh 'bundle exec rubocop' + + path = 'lib/emailable/version.rb' + File.write(path, File.read(path).sub(/VERSION = '.*'/, "VERSION = '#{version}'")) + sh "git commit -q -am 'Release #{version}'" + sh "git tag -a #{version} -m #{version}" + puts "Tagged #{version}. Push the commit and tag to release: git push --follow-tags" + end +end diff --git a/emailable.gemspec b/emailable.gemspec index f2d1d4f..c35ae6e 100644 --- a/emailable.gemspec +++ b/emailable.gemspec @@ -17,7 +17,8 @@ Gem::Specification.new do |s| s.metadata = { 'bug_tracker_uri' => 'https://github.com/emailable/emailable-ruby/issues', 'documentation_uri' => 'https://emailable.com/docs/api/?ruby', - 'source_code_uri' => 'https://github.com/emailable/emailable-ruby' + 'source_code_uri' => 'https://github.com/emailable/emailable-ruby', + 'rubygems_mfa_required' => 'true' } s.files = `git ls-files`.split("\n")