What version of the code is live?

devops git
Posted on: July 31, 2014

If your app has errors in production, it's useful to know what version of the code is running. I just learned that, at my current job, we can check that with a simple curl to the web site: the response includes a custom header like X-Revision with the Git SHA of the code it's running.

There is more than one way to do this. The way we do it is in nginx.conf: there's a line like add_header X-Revision XXXREVISIONXXX;. That last part is just an easily-to-grep string that gets replaced with the actual SHA (found with git rev-parse HEAD) during the deploy process. (We also add a header that identifies which application server generated the response.)

If you're specifically running a Rails app, you can do something similar in a before_action, as Andrew Cholakian has written about.

Update 2017-03-01: if you're running in Heroku, where git isn't available at boot time, you could do sh "heroku config:set GIT_SHA=$(git rev-parse HEAD) --app #{app_name}" at the end of your deploy script to set an environment variable. Thanks to Jay and Jordan for teaching me that.