Project: a pipeline for your own project

The project

The last project takes the log analyzer from lesson 13.32 and gives it everything the CI chapter asked for: a Gemfile, a gemspec, a Rakefile with one task, a workflow file and a badge. It is the shape of a small Ruby project after the course.

logstat ├── Gemfile ├── Rakefile ├── access.log ├── exe │ └── logstat ├── lib │ └── logstat.rb ├── logstat.gemspec └── test └── logstat_test.rb

The gem is the tool from lesson 13.32 with two changes: the executable lives in exe/ and requires "logstat", and the gemspec declares it. The gemspec follows the shape of the one from lesson 13.35, with the name, the summary and the file list changed to logstat; the parsing code and the test are unchanged, and access.log is the five-line sample from lesson 13.32.

One task to run everything

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

The pipeline task has two prerequisites: the tests and the build. A linter would be a third name, and the workflow file would not change.

rake ci

bundle exec rake ci

Run options: --seed 58153 # Running: . Finished in 0.001214s, 823.7164 runs/s, 1647.4329 assertions/s. 1 runs, 2 assertions, 0 failures, 0 errors, 0 skips gem build logstat.gemspec Successfully built RubyGem Name: logstat Version: 0.1.0 File: logstat-0.1.0.gem

Two steps, one command, and a package at the end. The second step fails when the first one fails, because Rake stops at the first failing prerequisite, so a broken test never produces a gem.

The gem installs and runs like the one from the previous lesson:

gem install ./logstat-0.1.0.gem

logstat access.log

Top paths:
  /index.html 2
  /boom 1
  /missing 1
Status codes:
  200 2
  500 2
  404 1

The workflow and the badge

name: CI on: push: pull_request: jobs: ci: 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 ci - uses: actions/upload-artifact@v4 with: name: gem path: "*.gem"

The workflow calls the same task a developer runs, and the built gem leaves the run as an artifact. The badge is one line in the README, with the repository path in the two placeholders:

![CI](https://github.com/USER/logstat/actions/workflows/ci.yml/badge.svg)

A badge that is green because the workflow never ran is worse than no badge. Watch the first few pushes: the badge appears after the first run and turns red with the first failure, which is the only useful state.

What to watch for

Keep the build in the pipeline, not on the laptop. The gem that ships is the one the runner built from the committed tree, and the artifact is the proof.

Do not add a release job to this pipeline yet. A release needs the tag check from lesson 13.14 and a key from lesson 13.11; a project gets them when it has one user, not when it has a badge.

This is the last lesson of the course; the Ruby course tree has every lesson in order.

Questions

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

  1. What does task ci: %i[test build] declare?

    Answer

    A task ci whose prerequisites are test and build

    The names after the colon are prerequisites, so Rake runs the tests and the build before ci itself.

  2. The test prerequisite fails. What does Rake do with the build step?

    Answer

    It stops before the build, so no broken gem is packaged

    A failed prerequisite stops the chain, which is the order that wastes the least work.

  3. Why does the workflow run bundle exec rake ci instead of repeating the commands?

    Answer

    One definition runs on the laptop and on the runner alike

    The pipeline calls the task a developer calls, so a difference between the two is a bug in one place.

  4. What is the artifact of this pipeline?

    Answer

    The built gem file

    The upload step keeps the *.gem the build produced, so the package can be inspected after the run.

  5. What does this program print? p %i[test build]

    Answer

    [:test, :build]

    %i builds an array of symbols, which is the form the prerequisite list uses.

  6. What does this program print? gem = "logstat-0.1.0.gem" p gem.sub(/\.gem\z/, "")

    Answer

    "logstat-0.1.0"

    The anchored pattern removes only the .gem suffix, so the name and version stay.

  7. What does this program print? version = "0.1.0" p version.split(".").map(&:to_i)

    Answer

    [0, 1, 0]

    split gives three strings and the block turns each one into an integer.

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

    Answer

    [:test, :build, :ci]

    A prerequisite is visited before its owner, so the tests finish first and ci comes last.

  9. A workflow runs bundle exec rake ci but never installs the bundle. What happens?

    Answer

    The job fails before the tests run

    Jobs start on a fresh runner, so the bundle has to be installed in the job that runs the task.

  10. Why is a badge that never ran a workflow worse than no badge?

    Answer

    It shows a state that no run has proved

    The badge is a claim about the last run; without a run it claims nothing true.

Article author: Arthur Isaev