I want to display the hash of the current git commit in the browser so that testing team (which does not have an access to run heruko commands) will be able to include the c
Heroku has new functionality to support dyno metadata, if you email support you can probably get added to the beta. Here's a place where Heroku themselves are using it:
https://github.com/heroku/fix/blob/6c8ab7a/lib/heroku_dyno_metadata.rb
The additional functionality writes out a JSON file to /etc/heroku/dyno
- the contents look like this:
{
"dyno":{
"physical_id":"161bfad9-9e83-40b7-b385-78305db2f168",
"size":1,
"name":"run.7145"
},
"app":{
"id":null
},
"release":{
"id":50,
"commit":"2c3a0b24069af49b3de35b8e8c26765c1dba9ff0",
"description":null
}
}
..so release.commit
is the field you're after.
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 the HEAD_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 option 1.
and adds git push heroku master
at the end. Then to deploy your code you run this shell script. This will update the HEAD_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.
Git >1.8.2 now supports a pre-push hook, which is a better match for our usecase. Here's my current hook script:
#!/bin/sh
remote="$1"
url="$2"
if [[ $url =~ heroku ]] ; then
if [[ $url =~ staging ]] ; then
appname=YOUR_APP_NAME_STAGING
else
appname=YOUR_APP_NAME
fi
hash_name=COMMIT_HASH
hash=$(git rev-parse HEAD)
echo Setting $hash_name to $hash
heroku config:set $hash_name=$hash --app $appname
fi
exit 0