One job, several Ruby versions
The matrix
One job tests one Ruby version. A matrix turns that job into several jobs, one per version, without copying the steps. The workflow keeps the step list and gains a list of versions:
jobs: test: runs-on: ubuntu-latest strategy: fail-fast: false matrix: ruby: ["3.3", "3.4", "4.0"] steps: - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.ruby }} bundler-cache: true - run: bundle exec rake ci
fail-fast: false keeps the other jobs running when one version fails. The default stops the rest as soon as the first job fails, which is faster on a broken branch and hides whether the problem is one version or all of them.
Check that it parses
The version list is data, so the YAML check can print it:
ruby -ryaml -e 'p YAML.load_file(ARGV[0])["jobs"]["test"]["strategy"]["matrix"]["ruby"]' .github/workflows/ci.yml
["3.3", "3.4", "4.0"]
Three jobs, each with the same steps and one different value. The ${{ matrix.ruby }} is replaced before the job starts, so the runner only ever sees a plain string.
The same check locally
A matrix only helps if the code actually runs on every version in it. The interpreter can tell a script which version it is:
ruby -e 'puts RUBY_VERSION'
3.4.12
A guard for a feature that needs a newer Ruby compares versions properly instead of string by string:
ruby -e 'puts Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.0")'
true
String comparison would call "3.10" smaller than "3.9"; Gem::Version reads the dots as numbers. Put the minimum version in the gemspec with required_ruby_version and the matrix and the package then agree.
What to watch for
Every matrix entry costs a full job: install the gems, run the tests. Two
versions are usually enough for a course project; three is a statement
that the library supports the old line. Drop a version when it reaches
end of life, and add the new one in the same commit.
A matrix that is red on the oldest version only is the useful case: the
code uses something newer than the gemspec promises. Read the failing
job before removing the version from the list.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
fail-fast: falsedo in a matrix?Answer
It lets the other jobs finish when one version fails, so the log shows which versions broke
The default cancels the rest, which is fast on a branch that is broken everywhere and unhelpful when only one version is wrong.
-
What does this program print?
p RUBY_VERSION.split(".").first(2).join(".")Answer
"3.4"
The first two parts are the major and minor version, the string a matrix entry and a gemspec use.
-
What does this program print?
p "3.10" > "3.9"Answer
false
A string comparison reads character by character, so "3.1" loses against "3.9" and the real version order is lost.
-
What does this program print?
p Gem::Version.new("3.10") > Gem::Version.new("3.9")Answer
true
Gem::Version splits on the dots and compares the numbers, which is the comparison a version guard needs.
-
What does this program print?
require "yaml" text = "matrix:\n ruby: [\"3.3\", \"3.4\"]\n" p YAML.load(text)["matrix"]["ruby"].sizeAnswer
2
A flow sequence becomes an array, and the size is the number of matrix jobs on that axis.
-
What does one matrix entry cost?
Answer
A full job: install the gems and run the tests again
Each version is a separate job, which is why a course project stays at two versions and drops a line at end of life.
-
What does this program print?
versions = ["3.3", "3.4"] os = ["ubuntu", "macos"] jobs = versions.product(os) p jobs.sizeAnswer
4
A two-axis matrix is every pair, so two versions times two systems make four jobs.
-
Where does the minimum Ruby version belong so the parser reads it?
Answer
In the gemspec, as required_ruby_version
The gemspec is the file Bundler and RubyGems read, so the matrix and the package then say the same thing.
-
A matrix is red only on the oldest Ruby. What does that mean?
Answer
The code uses something newer than the gemspec promises, and the failing job names it
The oldest entry is the useful one: it finds the exact feature that quietly raised the real minimum.
-
What does this program print?
p RUBY_VERSION.split(".").first.to_i >= 3Answer
true
The major version is read as a number, which is a rough guard; the exact comparison uses Gem::Version.
Article author: Arthur Isaev