Get Heroku Release Number / Version Version from Running Dyno

Is it possible to get a unique key for each pool / release from running dyno? After this article, I set RAILS_CACHE_ID (to expire etags after deployment), but found that dynos are no longer shipped with the GIT that causes this error):

 fatal: Not a git repository (or any parent up to mount point /app) Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set). 

I also thought about setting it to config/initializers at the current time, but obviously this will not work on multiple speakers. Any ideas?

+6
source share
2 answers

There is a new (November 2015) laboratory, which does exactly what you need "Dyno Metadata" https://devcenter.heroku.com/changelog-items/768

 heroku labs:enable runtime-dyno-metadata -a <app name> 

Then on heroku:

 ~ $ 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 
+9
source

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.

0
source

Source: https://habr.com/ru/post/979527/


All Articles