Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku - Display hash of current commit in browser

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 corresponding commit hash in bug reports.

First I tried grit, but something is broken and it doesn't work on Heroku (on local it works great, I don't know why it fails on Heroku).

So I found out that there are two environment variables on Heroku:

ENV["COMMIT_HASH"] ENV["LAST_COMMIT_BY"] 

But neither of them is available (both are nil).

I also checked with:

heroku config 

But again, neither is set.

Is there a way to retrieve the hash information? Is there any way to have more git information, such as date for example?

like image 400
guyaloni Avatar asked Jan 29 '13 12:01

guyaloni


People also ask

How do I see current commit hash?

# open the git config editor $ git config --global --edit # in the alias section, add ... [alias] lastcommit = rev-parse HEAD ... From here on, use git lastcommit to show the last commit's hash.

How do I see my heroku commits?

To view a specific N commits, add the -n option, as in git log -n 100 to get the past 100 commits. If you're only interested in the date and the commit message, you can format the log output to only show the relevant info, as in git log --pretty="%aD : %s" .

How do you check which branch is deployed on Heroku?

To see what's been deployed on the Heroku dashboard: Just click the Overview tab. You'll see an Activity view on the right that shows recent deployments with commit hashes. This should be the accepted answer.


2 Answers

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 ... 
like image 178
dentarg Avatar answered Oct 17 '22 18:10

dentarg


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.

  1. 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.

  2. 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.

like image 45
culix Avatar answered Oct 17 '22 18:10

culix