One solution is to use git pre-push hook to set the heroku configuration value. Since this was done before compiling push and slug, the config variable will be available as ENV var for your rails application.
.git/hooks/pre-push:
#!/bin/sh remote="$1" url="$2" while read local_ref local_sha remote_ref remote_sha do if [[ $url =~ heroku ]] ; then app=$(git remote show -n $remote | sed -n -E -e 's/[[:space:]]+(Push[[:space:]]+URL)(\/|:).+(:|\/)(.*)\.git$/\4/gp') echo Setting RAILS_CACHE_ID to $local_sha on app $app heroku config:set RAILS_CACHE_ID=$local_sha --app $app fi done exit 0
The pre-push.sample file has documentation about parameters called with hook. I use the verbose output of git remote to determine which application to set the configuration value. The -E option for sed is for Mac OS X — if you use GNU sed replace, use “-r”.
Another solution is to use heroku-api through the profile.d script to get a unique release id. This example uses curl to get the latest release identifier using the RANGE header. This is not a commit link, but it will be unique for each version, including rollbacks and configuration changes. You want to set API_KEY and APP_NAME as heroku configuration variables.
.profile.d/release.sh
# get release id and set as RAILS_CACHE_ID # Heroku config variables that need to be set # API_KEY: heroku api key (get from dashboard or `heroku auth:token` # APP_NAME: set this to your app_name (this could be hardcoded in the profile.d script but # would make it harder to manage apps with multiple environments res=$(curl -s -H "Accept: application/vnd.heroku+json; version=3"\ -H "Authorization: Bearer $API_KEY"\ -H "Range: version ..; order=desc, max=1"\ -X GET https://api.heroku.com/apps/$APP_NAME/releases) release_id=$(ruby -rjson -e "j = JSON.parse('$res'); puts j[0]['id']") export RAILS_CACHE_ID=$release_id
The rails application ENV ['RAILS_CACHE_ID'] should now have the latest release identifier set. You can also use the same strategy in a rail initializer.
source share