Build and release a gem

Version and gemspec

A gem needs a name, a version and a list of files. Keep the version in one place, a small file the gemspec reads, so the tag, the changelog and the package always agree.

lib/version.rb

module CiDemo VERSION = "0.1.0" end

ci-demo.gemspec

require_relative "lib/version" Gem::Specification.new do |spec| spec.name = "ci-demo" spec.version = CiDemo::VERSION spec.summary = "A tiny todo library used by the CI lessons" spec.authors = ["Arthur Isaev"] spec.license = "MIT" spec.homepage = "https://www.aredel.com/dev/ruby/course/" spec.files = ["lib/todo.rb", "lib/version.rb"] spec.require_paths = ["lib"] spec.required_ruby_version = ">= 3.0" end

gem build

Build the package from the gemspec:

gem build ci-demo.gemspec

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

The file lands next to the gemspec. Its name carries the version, so a new release is a new file and never an overwrite.

Install the package

Install the local file before pushing anywhere. A path with a slash or a dot is installed as a local package, which is the quickest end-to-end check:

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

If the install succeeds but require "todo" fails, the files list in the gemspec is incomplete: the package was built without that file. This is the most common gemspec mistake, and the local install is where it shows up.

gem push

Publishing sends the package to RubyGems, where Bundler can find it:

gem push ci-demo-0.1.0.gem

Publishing needs a rubygems.org account and an API key; the command reads the key from ~/.gem/credentials or from a secret that the pipeline passes in. The example gem of this course was not pushed, so there is no output to show for it: the command is here so you know where the release ends.

Keep the credentials file out of the repository and pass the key as a secret to the job.

In a pipeline, push only from a tag: build the package, check that the version in the gemspec matches the tag name, and then push. A release from a branch is a release nobody can reproduce.

What to watch for

The version lives in one file, and the git tag repeats it. A mismatch between the tag and the gemspec is easy to create by hand and hard to notice later, so let the pipeline read CiDemo::VERSION and compare it with the tag before the push.

Build the package in CI rather than on a laptop. The `.gem` file is reproducible from the repository only if nothing local leaks into the file list, and a build on a fresh machine is the easiest way to prove it.

Questions

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

  1. Why does the gemspec read the version from lib/version.rb?

    Answer

    So the tag, the changelog and the package all read one value that is written once

    One constant is the single place to edit, and the pipeline can compare the same constant with the git tag.

  2. What does this program print? module CiDemo VERSION = "0.1.0" end p CiDemo::VERSION

    Answer

    "0.1.0"

    The constant is a string and p prints it with its quotes; the gemspec uses the same value without printing it.

  3. What does this program print? p Gem::Version.new("0.1.0") < Gem::Version.new("0.2.0")

    Answer

    true

    Gem::Version compares the parts as numbers, which is the comparison a release check needs.

  4. What does this program print? spec = Gem::Specification.new do |s| s.name = "ci-demo" s.version = "0.1.0" end p spec.full_name

    Answer

    "ci-demo-0.1.0"

    full_name is the name and the version together, which is the name of the .gem file gem build writes.

  5. The gem installs but require "todo" fails. What is the usual cause?

    Answer

    The files list of the gemspec does not include lib/todo.rb

    The package holds only the files the gemspec lists, so a missing entry is a file that never reached the .gem.

  6. What does this program print? version = "0.1.0" tag = "v0.1.0" puts tag.delete_prefix("v") == version

    Answer

    true

    The tag carries a v that the version does not, and removing it is the comparison a release job makes before a push.

  7. Why should a gem be pushed only from a tag?

    Answer

    A release from a branch is one nobody can reproduce from the history

    The tag is the fixed point the version is compared with, so the package can be rebuilt from it later.

  8. What does this program print? versions = ["0.10.0", "0.2.0", "0.1.0"].sort_by { |v| Gem::Version.new(v) } p versions

    Answer

    ["0.1.0", "0.2.0", "0.10.0"]

    A string sort would put 0.10.0 before 0.2.0, and the version objects put the numbers in their real order.

  9. What does spec.require_paths = ["lib"] tell RubyGems?

    Answer

    Which folders a require searches inside the installed gem

    The installed files keep their layout, and require_paths is what makes require "todo" find lib/todo.rb.

  10. What does this program print? files = ["lib/todo.rb", "lib/version.rb"] installed = ["lib/todo.rb"] p (files - installed)

    Answer

    ["lib/version.rb"]

    The difference is the file the package does not have, which is exactly the file whose require fails after the install.

Article author: Arthur Isaev