tags: rails
Originally Published: 2021-09-03

Rapporteur is a nice simple gem for adding a status endpoint to your Rails app. It also has support for including the source version (e.g. git commit) in the status response. While the docs suggest using ENV["REVISION"] for Heroku, I found that wasn’t set automatically in my Heroku runtime stack.

The workaround I developed for PodQueue was to use the SOURCE_VERSION environment variable set in the Heroku “build” stage to automatically write out a file with the source revision which we can then load from the runtime.

First, I made a new rake task which will generate the revision file (in lib/tasks/app.rake):

namespace :app do
  desc 'Generates .source_version'
  task source_version: :environment do
    if ENV['SOURCE_VERSION'].present?
      warn "Writing revision #{ENV['SOURCE_VERSION']} to .source_version"
      File.write(Rails.root.join('.source_version'), ENV['SOURCE_VERSION'])

Then, we hook our new app:source_version task onto the assets:precompile task automatically invoked by the Heroku Rails build by using enhance in our Rakefile (you’ll need to hook this onto something else if your build doesn’t use asset precompilation):

Rake::Task['assets:precompile'].enhance do

Finally, we configure Rapporteur to use the .source_version file we write out during the “build” stage (config/initializers/rapporteur.rb):

if Rails.env.production? && File.exist?(Rails.root.join('.source_version'))
  Rapporteur::Revision.current = File.read(Rails.root.join('.source_version'))

Now, your next deploy should have the correct git revision in its status response, which you can use to do fun things like automatically wait for a deploy to bubble through CI/CD into production.