Automated releases
A release is a tag
A branch moves; a tag does not. That is why a release starts from a tag
and why the pipeline can treat a tag as a promise: the code behind
v0.1.0 never changes, so the package built from it
can always be built again.
The other half of the promise is the version inside the project. It lives
in lib/version.rb, and the gemspec and the
executable both read it from there.
module CiDemo VERSION = "0.1.0" end
Check the tag against the version
Two places hold the same number, so they can disagree. A small Rake task compares the tag that the pipeline passes in with the constant, and it stops the release before anything is built:
require_relative "lib/version" namespace :release do desc "Check that the tag matches the version" task :check do tag = ENV.fetch("TAG", "") version = CiDemo::VERSION abort "set TAG=v#{version}" if tag.empty? abort "#{tag} does not match #{version}" unless tag == "v#{version}" puts "ok: #{version}" end end
Three runs show the three answers:
TAG=v0.1.0 bundle exec rake release:check
ok: 0.1.0
TAG=v0.2.0 bundle exec rake release:check
v0.2.0 does not match 0.1.0
bundle exec rake release:check
set TAG=v0.1.0
The last two exit nonzero, so a pipeline stops on them. A release job that starts with this task cannot publish a gem whose tag and version disagree.
The release job
The job runs only when a tag is pushed, checks the version, builds and publishes:
on: push: tags: - "v*" jobs: release: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: ruby-version: "3.4" bundler-cache: true - run: bundle exec rake release:check env: TAG: ${{ github.ref_name }} - run: gem build ci-demo.gemspec - run: gem push *.gem env: GEM_HOST_API_KEY: ${{ secrets.RUBYGEMS_API_KEY }}
github.ref_name is the tag without the refs/tags/ prefix, which is exactly what the task compares. On a branch push the job does not run at all.
What to watch for
Bump the version in one commit with the changelog line, then tag that
commit. A tag on a commit that changes later is not a release, and a
version bumped after the tag is a package nobody can find.
Test the release path with a dry run before the first real tag: build the
gem, install the file locally, run the executable. A release job that
fails at the push is a tag that already exists and a package that does
not.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Why does a release start from a tag rather than a branch?
Answer
A tag never moves, so the package can be rebuilt from exactly those files
The promise of a release is a fixed point in the history, which a branch cannot give.
-
What does this program print?
version = "0.1.0" tag = "v0.1.0" abort "set TAG=v#{version}" if tag.empty? abort "#{tag} does not match #{version}" unless tag == "v#{version}" puts "ok: #{version}"Answer
ok: 0.1.0
Both guards pass, so the task reports the version it checked and exits zero.
-
What does this program print?
version = "0.1.0" tag = "v0.2.0" puts(tag == "v#{version}" ? "ok" : "#{tag} does not match #{version}")Answer
v0.2.0 does not match 0.1.0
The tag names the version that was released, and the constant says which one this code is.
-
What does this program print?
ref = "refs/tags/v0.1.0" p ref.delete_prefix("refs/tags/")Answer
"v0.1.0"
github.ref_name is the tag without the prefix, and this is the same transformation a script makes.
-
What does
abort "message"do?Answer
It prints the message to standard error and exits with a nonzero status
The nonzero status is what stops Rake and the job, and the message goes to the log next to it.
-
What does this program print?
tags = ["v0.1.0", "v0.2.0", "hotfix"] p tags.select { |t| t.start_with?("v") }.sizeAnswer
2
The filter keeps the two tags that start with v, which is the shape of the trigger pattern v*.
-
Why is the release job triggered on a tag pattern rather than on every push?
Answer
A branch push must not publish; only the fixed point of a tag is a release
The trigger is the first gate of the release: no tag, no job, no package.
-
What does this program print?
failed = false exit 1 if failed puts "still here"Answer
still here
The exit is guarded by a false flag, so the program continues and the status a pipeline reads is zero.
-
In which order does a release happen?
Answer
Bump the version and the changelog in one commit, then tag that commit
The tag must point at a commit whose version is already the released one, or the package and the tag disagree.
-
What does this program print?
version = "0.1.0" puts "tag v#{version} is the release"Answer
tag v0.1.0 is the release
The constant is read at run time, which is how the message and the tag stay in one place.
Article author: Arthur Isaev