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.

Next: Bundler and the cache

Article author: Arthur Isaev

Banner image