Deploying from a pipeline
A health check
A deploy that returns zero has moved files, not necessarily started a service. The last step of a deploy asks the new version a question only it can answer: the health endpoint.
require "net/http" uri = URI(ARGV[0] || "http://localhost:8000/health") response = Net::HTTP.get_response(uri) puts "#{response.code} #{response.body}" exit(response.code == "200" ? 0 : 1)
The Ruby script exits zero only on a 200. Start a small server with a /health route and run the check against it:
require "webrick" server = WEBrick::HTTPServer.new(Port: 8000) server.mount_proc("/health") { |_req, res| res.body = "ok" } server.start
ruby health_check.rb
200 ok
A wrong path or a server that is not up gives a nonzero exit status, and the pipeline marks the deploy failed. That is the whole idea: the check runs against the thing that was just deployed, not against a file that was copied. Run it while nothing listens on the port and it stops with Errno::ECONNREFUSED, which is the failure the job is supposed to show.
The deploy job
A deploy job goes after the tests and the build, and it waits for a person when the environment is production. In GitHub Actions the gate is an environment with required reviewers:
deploy: needs: test runs-on: ubuntu-latest environment: production steps: - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: ruby-version: "3.4" bundler-cache: true - run: bundle exec rake ci - run: bundle exec ruby health_check.rb https://example.com/health
GitLab has the same idea with when: manual on the deploy job and a protected environment, and the health check is the last line of the script. The address comes from a variable, so the same job deploys to staging and production.
Most Ruby deploys copy files over SSH with scp. Those commands — ssh, scp, a container registry login — are the part the pipeline should keep small: one command whose arguments come from variables, so the job shows the address and never the key.
What to watch for
A health check that always answers 200 ok from a
static file proves nothing. Make it touch the dependency that matters:
the database connection, the cache, the background queue. If the service
can be up and useless, the check must ask about the useless part.
Keep a way back. A deploy job that cannot roll back turns a bad release
into a long outage; the previous image tag or the previous package is
enough, as long as it is named in the job.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
A deploy command exits zero. What has it proved?
Answer
That files were moved, which is not the same as a started service
The health check exists because a successful copy says nothing about the process that should now answer requests.
-
What does this program print?
response = { code: "200", body: "ok" } puts "#{response[:code]} #{response[:body]}" exit(response[:code] == "200" ? 0 : 1)Answer
200 ok
The code and the body are printed in one line and the zero status tells the pipeline the service is up.
-
What does this program print?
require "uri" uri = URI("https://example.com/health") p uri.host p uri.pathAnswer
"example.com" / "/health"
A URI splits the address into a scheme, a host and a path, which is how a check builds its request.
-
The script runs with no argument. What does this program print?
uri = ARGV[0] || "http://localhost:8000/health" p uriAnswer
"http://localhost:8000/health"
The default keeps a local run working while the pipeline passes the deployed address.
-
Why must a health check touch the dependency that matters?
Answer
A service can be up and useless, so a check that answers from a constant proves nothing
The database connection or the queue is what makes the service useful, and the check has to ask about that part.
-
What does this program print?
attempts = 0 begin attempts += 1 raise "not yet" if attempts < 3 puts "up after #{attempts}" rescue RuntimeError retry if attempts < 3 endAnswer
up after 3
retry runs the begin block again, so the third attempt succeeds and the message names it.
-
Why does a deploy job keep a way back?
Answer
A deploy that cannot roll back turns a bad release into a long outage
Naming the previous image tag or package in the job is the cheap insurance against a release that must be undone.
-
What does this program print?
code = "503" puts(code == "200" ? "ok" : "unhealthy")Answer
unhealthy
Any status other than 200 is a failure for the check, and the job turns red on it.
-
What does this program print?
code = "500" p code.to_i >= 400Answer
true
The status is text from the response, and to_i turns it into the number a comparison needs.
-
What must a deploy job show in its log?
Answer
The address and the command, never the key
One command whose arguments come from variables keeps the secret out of the log while the address stays visible.
Article author: Arthur Isaev