Ruby in GitHub Actions

The workflow file

The two big hosts describe a pipeline in the same way. This lesson writes the GitHub file; the next one writes the GitLab file.

GitHub reads workflows from .github/workflows/. One YAML file describes when the pipeline runs and what each job does. For the project from the previous lesson the whole file is ten lines of YAML plus the step list:

name: CI on: push: pull_request: jobs: test: 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 test

Three things carry the work. actions/checkout copies the repository into the runner. ruby/setup-ruby installs the chosen Ruby and, with bundler-cache: true, runs bundle install and caches the result keyed on Gemfile.lock. The last step is the command from the previous lesson.

The trigger is the on block: every push and every pull request. A workflow that runs on a branch nobody merges is noise; a workflow that runs only on tags misses the bug before the release.

Check that it parses

A YAML file that does not parse fails before any job starts, and the error points at a line rather than at the mistake. Ruby can parse the file with the standard library, which is the quickest check before a push:

ruby -ryaml -e 'YAML.load_file(ARGV[0])' .github/workflows/ci.yml

No output means the file parsed. To see something useful, ask for one value:

ruby -ryaml -e 'p YAML.load_file(ARGV[0])["jobs"]["test"]["steps"].size' .github/workflows/ci.yml

3

Three steps, as written. One Ruby quirk shows up if you print the whole document: the YAML 1.1 parser reads the key on as the boolean true. GitHub parses the file with its own rules and reads it as a string, so the workflow works; the Ruby dump is the odd one out.

The same steps by hand

The workflow has no secret sauce: it installs and it tests. Run the same two commands locally and compare:

Install the gems

bundle install

Fetching gem metadata from https://rubygems.org/...
Resolving dependencies...
Fetching minitest 5.27.0
Installing minitest 5.27.0
Bundle complete! 2 Gemfile dependencies, 2 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

Run the tests

bundle exec rake test

Run options: --seed 8918

# Running:

...

Finished in 0.001315s, 2281.3081 runs/s, 3041.7441 assertions/s.

3 runs, 4 assertions, 0 failures, 0 errors, 0 skips

The output above is from running these two commands locally; the runner log is the same text with timestamps around it. When a job fails, this is the section of the log to read first: the failing command and the lines above it, not the top of the page.

What to watch for

bundler-cache: true only helps when Gemfile.lock is committed. Without the lock file the cache key changes on every run and the job reinstalls the same gems every time, which looks like a slow pipeline rather than a missing file.

Pin the Ruby version in the workflow rather than saying latest. A new Ruby release then arrives as an intentional change with a failing matrix entry, not as a pipeline that went red during the weekend.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does bundler-cache: true in the setup-ruby step do?

    Answer

    It runs bundle install and caches the installed gems, keyed on Gemfile.lock

    The step installs the gems and keeps them between runs, and the key is the lock file, which must be committed for the cache to be stable.

  2. What does this program print? require "yaml" data = YAML.load("name: CI") p data["name"]

    Answer

    "CI"

    A YAML mapping becomes a Hash with string keys, and p shows the string with its quotes.

  3. What does this program print? require "yaml" p YAML.load("on: push").keys

    Answer

    [true]

    Psych follows YAML 1.1, where on is a boolean, so the key comes back as true; GitHub parses the same file with its own rules and keeps the string.

  4. What does the workflow file of this lesson do on a pull request?

    Answer

    It runs the test job once for the pull request

    The on block names push and pull_request, so each event starts one run of the job.

  5. What does this program print? require "yaml" text = <<~YAML jobs: test: steps: - run: bundle install - run: bundle exec rake test YAML data = YAML.load(text) p data["jobs"]["test"]["steps"].size

    Answer

    2

    The steps sequence has two items, which is the number the page asks of its own workflow file.

  6. What does this program print? require "yaml" puts YAML.dump({ "on" => "push", "jobs" => 1 })

    Answer

    --- / 'on': push / jobs: 1 / ...

    The dump quotes on so that reading it back keeps the string, and it writes the document start and end markers.

  7. Why does the cache key depend on Gemfile.lock?

    Answer

    The lock file is the list of exact gem versions, so it is the identity of the installed set

    A missing or changed lock file changes the key, which either reinstalls every run or shares a set the project no longer uses.

  8. What does this program print? require "yaml" begin YAML.load("a: [1, 2") rescue Psych::SyntaxError => e puts e.class end

    Answer

    Psych::SyntaxError

    An unclosed bracket is a syntax error of the YAML parser, which is why the page checks the file before a push.

  9. What is the difference between actions/checkout and ruby/setup-ruby in the workflow?

    Answer

    checkout copies the repository into the runner, setup-ruby installs Ruby and the gems

    The repository must be there before anything runs, and the Ruby step provides the interpreter and the bundle.

  10. What does this program print? require "yaml" data = YAML.load("ruby: 3.4\ncache: true") p data["ruby"].class

    Answer

    Float

    An unquoted 3.4 is a YAML number, which is why the workflow writes ruby-version: "3.4" in quotes.

Article author: Arthur Isaev