Getting the latest commit Hash on Heroku

When you push to Heroku, the contents of the .git directory are usually removed which makes getting the last commit hash a bit tricky.

Luckily there is a Heroku labs extension you can use to get some of the git information back. Just run this command against your app:

heroku labs:enable runtime-dyno-metadata -a <app name>

This will add the following to the env (note - this only happens on the next deploy after configuring):

HEROKU_APP_ID:             4d00253d-b1dc-44dd-9949-106af8002de5
HEROKU_APP_NAME:           <app name>
HEROKU_RELEASE_CREATED_AT: 2018-02-26T21:27:43Z
HEROKU_RELEASE_VERSION:    v13
HEROKU_SLUG_COMMIT:        731b2957b0ec6937cb8bbb0755aa8194e9f02600
HEROKU_SLUG_DESCRIPTION:   Deploy 731b2957

If you want to use this info in Jeykll, you can add a small script to your _plugins folder to make this available:

{% highlight ruby %} module Jekyll

class EnvVars < Generator

def generate(site)
  site.config['source_version'] = ENV['HEROKU_SLUG_COMMIT'] || '<dev>'
  site.config['release_version'] = ENV['HEROKU_RELEASE_VERSION'] || '<dev>'
end

end

end {% endhighlight %}

and you can then access the info under the {{ "{{ site.* " }}}}.