Heroku - Display hash of current commit in browser
As of 2015-04-01, the Git SHA is now available within the build process as the environment variable SOURCE_VERSION. See: https://devcenter.heroku.com/changelog-items/630
Note that it is not available to the running app, only during the compile step. You could add a custom buildpack to write this to a file that persists in the slug, and then read the file from your application.
I'm testing that approach and have an experimental buildpack here: https://github.com/sreid/heroku-buildpack-sourceversion
Firstly, since heroku "remove[s] unused files, including the .git directory" during slug compilation, you won't be able to execute some git commands from inside your app's directory (on the heroku dyno). This includes things like git rev-parse HEAD
, which is normally an easy way to get the current hash.
Secondly, trying to retrieve information with git ls-remote
on the heroku dyno will invoke ssh, and you'll see messages that say The authenticity of host 'heroku.com (50.19.85.132)' can't be established
, since the heroku public key is not installed on heroku dynos. You won't have permission to install the heroku public key.
You still have at least two options.
Add a post-commit hook to update the hash value.
a) Create or edit the file
.git/hooks/post-commit
b) Add some shell script code like this:hash_name=HEAD_HASH
hash=$(git rev-parse HEAD)
echo Setting $hash_name to $hash
heroku config:set $hash_name=$hash --app yourappname(you can use whatever code you want for git hooks; this is just one option)
Explanation:
HEAD_HASH
is the name of the heroku environment variable. Call it whatever you want. You'll look this up in your main app and display it on the page.git rev-parse HEAD
grabs the hash of the current HEAD commit. Customize this line for whatever you want to display.
Now when you make commits to git theHEAD_HASH
env var will be updated each time. This works, but might be a bit slow, as you'll be waiting for heroku to set the env var each time you commit. If your network connection is out etc. the variable won't be updated. Rumour is that git 1.8.2 will allow a 'pre-push' hook where you could put this code instead.Use a script to push your code
Instead of typing
git push heroku master
to push your code, you could write a shell script that contains the lines from option1.
and addsgit push heroku master
at the end. Then to deploy your code you run this shell script. This will update theHEAD_HASH
only before pushing (instead of after each git commit), and it nicely keeps everything in one place. You'll probably want to add the script to your.slugignore
file too.
It's now possible to try the Heroku feature Roberto wrote about in his answer, without contacting Heroku. It's called Heroku Labs: Dyno Metadata and you can enable it by
heroku labs:enable runtime-dyno-metadata -a <app name>
and then the information is available (on the next deploy) as environment variables:
~ $ env
HEROKU_APP_ID: 9daa2797-e49b-4624-932f-ec3f9688e3da
HEROKU_APP_NAME: example-app
HEROKU_DYNO_ID: 1vac4117-c29f-4312-521e-ba4d8638c1ac
HEROKU_RELEASE_VERSION: v42
HEROKU_SLUG_COMMIT: 2c3a0b24069af49b3de35b8e8c26765c1dba9ff0
HEROKU_SLUG_DESCRIPTION: Deploy 2c3a0b2
...