Rake as the pipeline runner

One task for the pipeline

Both CI files from the previous lessons end with the same command. Keep it that way on purpose: put the steps in a Rake task and let the CI file call the task. The pipeline file stays short, and a developer runs the same thing with one word.

require "rake/testtask" Rake::TestTask.new(:test) do |task| task.libs << "test" task.pattern = "test/*_test.rb" end desc "Run the whole pipeline" task ci: [:test] task default: :test

task ci: [:test] means: run test first, then ci. The ci task has no body of its own, its whole job is to name the prerequisites, so adding a linter later is one more name in that list.

rake -T

A task with a desc line shows up in the task list. That list is the best documentation a small project has:

rake -T

rake ci # Run the whole pipeline
rake test # Run tests

The second line comes from Rake::TestTask, which adds its own description. A task without desc stays out of this list, which is why the default line does not appear.

rake ci

Run the task the pipeline calls:

bundle exec rake ci

Run options: --seed 32431

# Running:

...

Finished in 0.003388s, 885.4180 runs/s, 1180.5574 assertions/s.

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

The output is the test task output, because that is the only prerequisite so far. If the tests fail, Rake stops before the next prerequisite, and the pipeline fails at the first red step, which is the behaviour you want.

Both pipelines call one task

The GitHub workflow and the GitLab file shrink to one line each:

- run: bundle exec rake ci

- bundle exec rake ci

This is the part that keeps a pipeline maintainable. A new check becomes a Rake task and a prerequisite; neither CI file changes, and the local command stays identical to the pipeline command.

What to watch for

Put the Rakefile under version control and keep the task list flat: ci calls test, lint, audit. Tasks that call each other in a cycle fail with a confusing message, and tasks that quietly ignore a failure defeat the pipeline.

Rake stops at the first failing prerequisite, so order the list cheap first: tests take seconds, an audit takes a download.

Questions

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

  1. What does task ci: [:test] mean in a Rakefile?

    Answer

    It defines ci as a task whose prerequisite is test, so test runs first

    The array is the prerequisite list, and Rake runs each name before the task itself.

  2. What does this program print? tasks = { test: [], lint: [:test], ci: [:lint] } order = [] visit = lambda do |name| tasks[name].each { |dep| visit.call(dep) } order << name end visit.call(:ci) p order

    Answer

    [:test, :lint, :ci]

    Each task runs after its prerequisites, so the order is the chain from test up to ci.

  3. What should a CI file contain when the project has a Rakefile?

    Answer

    One call to the task, such as bundle exec rake ci

    The task is the single place where the steps live, so the CI file and the local command stay identical.

  4. What does this program print? tasks = { "ci" => "Run the whole pipeline", "test" => "Run tests" } tasks.each { |name, desc| puts "rake #{name.ljust(4)} # #{desc}" }

    Answer

    rake ci # Run the whole pipeline / rake test # Run tests

    ljust pads the shorter name to four characters, which is how a task list keeps its comments in one column.

  5. Why does default stay out of the rake -T list?

    Answer

    It has no desc line, and -T lists the tasks that have one

    The task list is built from the descriptions, so a task without one exists but is not advertised.

  6. What does this program print? steps = [ -> { puts "test" }, -> { raise "lint failed" }, -> { puts "audit" }, ] begin steps.each(&:call) rescue RuntimeError => e puts e.message end

    Answer

    test / lint failed

    The raise ends the loop at the second step, which is why a failing prerequisite stops the rest of the pipeline.

  7. How does a Rake task report a failure to the pipeline?

    Answer

    It exits non-zero, and the job turns red at that step

    The exit status is the contract: a non-zero status stops Rake and fails the job on either host.

  8. What does this program print? tasks = [ { name: "ci", desc: "Run the whole pipeline" }, { name: "test", desc: "Run tests" }, { name: "default", desc: nil }, ] tasks.each { |t| puts "rake #{t[:name]}" if t[:desc] }

    Answer

    rake ci / rake test

    Only the entries with a description are printed, which is the rule the task list follows.

  9. Why does the page order the prerequisites cheap first?

    Answer

    Rake stops at the first failure, so a fast test gives the answer before a slow download starts

    The run ends at the first red step, so the quick check that fails most often goes first.

  10. What does this program print? ok = system("true") bad = system("false") p [ok, bad]

    Answer

    [true, false]

    system returns true for a zero status and false otherwise, which is the test a Rake task or a pipeline wraps.

Article author: Arthur Isaev