A pipeline for a CLI gem

Give the gem a command

A gem becomes a command when the gemspec names an executable. The file goes into exe/, starts with a shebang, and loads the library like any other Ruby program:

#!/usr/bin/env ruby require "version" puts "ci-demo #{CiDemo::VERSION}"

The gemspec gains two lines and one file in its list:

spec.files = ["lib/todo.rb", "lib/version.rb", "exe/ci-demo"] spec.bindir = "exe" spec.executables = ["ci-demo"]

bindir says where the executables live and executables says which ones the package owns. RubyGems writes a launcher for each into the bin directory, so ci-demo works after the install without a path.

Build and install it

gem build ci-demo.gemspec

Successfully built RubyGem
Name: ci-demo
Version: 0.1.0
File: ci-demo-0.1.0.gem

gem install ./ci-demo-0.1.0.gem

Successfully installed ci-demo-0.1.0
Parsing documentation for ci-demo-0.1.0
Installing ri documentation for ci-demo-0.1.0
Done installing documentation for ci-demo after 0 seconds
1 gem installed

ci-demo

ci-demo 0.1.0

The three commands are the release path in miniature: build the package, install what you built, run it. A pipeline that does the same three steps in that order catches a broken gemspec before it reaches RubyGems.

The pipeline file

One file, three jobs, and a release job that only runs on a tag:

name: CI on: push: branches: [main] tags: ["v*"] 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 lint: 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 rubocop release: if: startsWith(github.ref, 'refs/tags/v') needs: [test, lint] 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 release:check env: TAG: ${{ github.ref_name }} - run: gem build ci-demo.gemspec - run: gem push *.gem env: GEM_HOST_API_KEY: ${{ secrets.RUBYGEMS_API_KEY }}

The if: line is the gate: on a branch the release job is skipped, on a tag it runs after the tests and the linter. The version check from lesson 13.14 fails the job before the push when the tag and the gemspec disagree.

What to watch for

Test the installed package, not only the code in the repository. A file missing from spec.files runs fine in the project folder and fails from the installed gem; the three commands above, run in one job, catch it.

Keep the build and the push in different steps. A build that fails before the push leaves no published version behind, and the step log says which of the two failed.

Questions

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

  1. What does the executables line of a gemspec declare?

    Answer

    The executables the package owns, which RubyGems installs as launchers

    bindir names the folder and executables names the files, and the install writes a launcher for each one into the bin directory.

  2. What does this program print? version = "0.1.0" puts "ci-demo #{version}"

    Answer

    ci-demo 0.1.0

    The executable prints its name and the version constant, which is how a reader checks which install they are running.

  3. What does this program print? ref = "refs/tags/v0.1.0" p ref.start_with?("refs/tags/v")

    Answer

    true

    The release job gate is the same test: only a tag that starts with v runs the publish steps.

  4. What does this program print? ref = "refs/heads/main" p ref.start_with?("refs/tags/v")

    Answer

    false

    A branch does not match the tag pattern, so the release job is skipped on a normal push.

  5. What does the three-command path (build, install, run) catch that a test suite does not?

    Answer

    A missing file in spec.files, which runs fine in the project folder and fails from the installed gem

    The install is the only step that reads the package as a user would, so a file the gemspec forgot shows up there.

  6. What does this program print? p ["ci-demo", "rake"].grep(/^ci/)

    Answer

    ["ci-demo"]

    The pattern keeps the name that starts with ci, which is how a script finds the command of the project among the installed ones.

  7. Why keep the build and the push in different steps?

    Answer

    A build that fails leaves no published version behind, and the log says which step failed

    The push is the step that cannot be undone, so it runs only after the package is built and checked.

  8. What does this program print? p "exe/ci-demo".split("/")

    Answer

    ["exe", "ci-demo"]

    The path has a folder and a file, and the gemspec names the two parts separately.

  9. Where does a gem executable live before the install?

    Answer

    In the project, in the folder bindir names

    The package copies the file from bindir, and the install writes the launcher that finds it.

  10. What does this program print? spec_executables = ["ci-demo", "ci-demo-lint"] p spec_executables.size

    Answer

    2

    The array has two names, so the install writes two launchers.

Article author: Arthur Isaev