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
git ls-remote heroku
works for me. I got this from an answer to a duplicate question here.
Both culix and joshwa have great answers. If you name your heroku git remotes the same as your corresponding heroku apps you can have an even shorter and more robust .git/hooks/pre-push
hook:
#!/bin/bash
remote="$1"
url="$2"
if [[ $url =~ heroku ]] ; then
hash_name=COMMIT_HASH
hash=$(git rev-parse HEAD)
echo Setting $hash_name to $hash on app $remote
heroku config:set $hash_name=$hash --app $remote
fi
exit 0
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
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
...
There is an env var on Heroku containers called SOURCE_VERSION
https://devcenter.heroku.com/articles/buildpack-api
You can use that!
It's also possible to use the Heroku API directly if you're using CI and don't have access to the Heroku client there.
This is the solution I used on codeship.com (which provides the hash in the environment as $CI_COMMIT_ID
:
# you can use `heroku auth:token` to generate this
HEROKU_API_KEY=""
APP_NAME="glorious-flying-birds"
curl -n -X PATCH "https://api.heroku.com/apps/$APP_NAME/config-vars" \
-H "Authorization: Bearer $HEROKU_API_KEY" \
-H "Accept: application/vnd.heroku+json; version=3" \
-H "Content-Type: application/json" \
-d "{\"GIT_COMMIT_HASH\": \"$CI_COMMIT_ID\"}"