A pipeline for a web API

The smallest API

The last project is the one from lesson 13.30, later in the course, with one addition that makes it testable from a pipeline: a health route that answers without touching the data.

require "json" require "webrick" TODOS = [] server = WEBrick::HTTPServer.new(Port: 8000) server.mount_proc("/health") { |_req, res| res.body = "ok" } server.mount_proc("/todos") do |_req, res| res["Content-Type"] = "application/json" res.body = JSON.generate(TODOS) end server.start

Two routes are enough for the pipeline: one that says the process is up and one that returns data a request test can assert on.

Run it and check it

Start the server in one terminal, then use the health check from lesson 13.17 and a plain request for the list:

ruby app.rb

ruby health_check.rb

200 ok

ruby -e 'require "net/http"; puts Net::HTTP.get(URI("http://localhost:8000/todos"))'

[]

An empty list is the correct answer for a fresh process, and it is what a request test asserts before it posts an item. The health check exits zero on the 200; a server that did not start gives a refused connection and a nonzero status.

The pipeline file

The API job adds two things to the pattern from the previous lesson: a service container for the database, and an image that is built after the tests pass:

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 image: needs: test runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - run: docker build -t ci-demo . - uses: actions/upload-artifact@v4 with: name: image-tag path: VERSION

A job that needs a real database declares it as a service, and the tests wait for the port before they connect. In GitLab the same job uses services: with the same image, and the pipeline waits with a health check of its own.

The image build and the registry push are the two steps that need a Docker daemon; on a runner they are ordinary steps, on a laptop they are the reason the last section of this course is read rather than typed.

What to watch for

A service container is not ready when the job starts. Poll the port or the health route with a timeout, and fail with a message that says which service did not come up; a test that fails on a connection refused sends the reader to the wrong file.

Tag the image with the commit, not only with latest. A deploy that pulls latest cannot say which code is running, and a rollback has nothing to roll back to.

Questions

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

  1. Why does the API of the lesson have a health route that touches no data?

    Answer

    So the pipeline can ask whether the process is up without a database behind it yet

    The health route answers as soon as the process runs, and the data route is what the request test asserts on.

  2. What does this program print? require "json" todos = [] p JSON.generate(todos)

    Answer

    "[]"

    An empty array becomes an empty JSON list, which is the correct answer for a process with no items yet.

  3. What does this program print? require "json" body = '[{"id":1}]' p JSON.parse(body).size

    Answer

    1

    The parsed body is an array of hashes, and a request test reads its size or its first item.

  4. What does this program print? code = "200" puts(code == "200" ? "0" : "1")

    Answer

    0

    The health check turns the status into the exit code a pipeline reads, and zero means the service answered.

  5. What does this program print? states = [false, false, true] attempts = states.take_while { |s| !s }.size p attempts

    Answer

    2

    Two failed checks come before the first success, which is the poll a job runs before it connects.

  6. What does this program print? commit = "a1b2c3d" p "ci-demo:#{commit[0, 7]}"

    Answer

    "ci-demo:a1b2c3d"

    The tag is the short commit, so a deploy can say exactly which code it started.

  7. Why poll the service container before the tests connect?

    Answer

    The container is not ready when the job starts, and a refused connection sends the reader to the wrong file

    A readiness loop with a timeout turns a confusing connection error into a message that names the service.

  8. What does this program print? headers = { "Content-Type" => "application/json" } p headers["Content-Type"] == "application/json"

    Answer

    true

    The header is what makes a response JSON, and the test compares the exact value the route sets.

  9. Why tag an image with the commit rather than only with latest?

    Answer

    A deploy that pulls latest cannot say which code is running, and a rollback has nothing to return to

    The commit tag is the identity a rollback needs, and the pipeline already knows the value.

  10. What does this program print? require "json" p JSON.parse('{"a":1}')["a"]

    Answer

    1

    JSON numbers come back as Ruby numbers, and a string key reads the value from the hash.

Article author: Arthur Isaev