Project: a pipeline for your own project

The project

The last project takes the log analyzer from lesson 79 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 79 with two changes: the executable lives in exe/ and requires "logstat", and the gemspec declares it. The parsing code and the test are unchanged.

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

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 64 and a key from lesson 61; a project gets them when it has one user, not when it has a badge.

This is the last lesson of the course. Next: Ruby course has the full tree.

Article author: Arthur Isaev

Banner image