Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access the current Heroku release version programmatically?

Tags:

heroku

Is this even possible - is there something like a RELEASE_VERSION environment variable?

like image 522
Gabriel Florit Avatar asked Oct 27 '11 14:10

Gabriel Florit


People also ask

Can you roll back to the latest version after going to a previous version of an app in heroku?

With heroku rollback , you can rollback to a previous, known good release in a single command - providing a fast way to revert in case of bad deploys or config changes. In this post, we give a quick overview of releases and rollbacks on Heroku and how to use them.

How do I stop heroku deployment?

To monitor your Release Phase processes as they execute, you can use the CLI command heroku ps -a YOUR_APP_NAME. as these are normal processes, you can use the ps:kill and ps:scale commands to stop the Release Phase from completing, which in turn, will prevent the latest release from completing.


4 Answers

There is now a released Heroku Labs feature called Dyno Metadata that gives you this information. Once you enable it, your running dyno's environment will contain environment variables with the Heroku version ID of your app, the git commit hash from which your release slug was built, and much more. For example:

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 114
brotskydotcom Avatar answered Sep 19 '22 02:09

brotskydotcom


I know it's an oldie, but I didn't find a definite answer anywhere else, so I'm posting it here in case anyone stumbles upon this question. I added an initializer, called deploy_version.rb with the followin content:

    if ENV['HEROKU_APP']         res = `curl -H "Accept: application/json" -u :#{ENV['HEROKU_API_KEY']} -X GET https://api.heroku.com/apps/#{ENV['HEROKU_APP']}/releases`         last = JSON.parse(res).last         $deploy_version = last['name']     else         $deploy_version = 'local'     end 

Then it's easy to display it in your app:

   <meta name="release" content="<%= $deploy_version %>"> 
like image 24
Tomaž Zaman Avatar answered Sep 17 '22 02:09

Tomaž Zaman


You could do it with a .profile.d script that calls the platform API and set an environment variable:

.profile.d/release.sh

# get the unique release id and set as RELEASE_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 RELEASE_ID=$release_id

In a rails app, for example, ENV['RELEASE_ID'] should now be set to the most recent release id. (Python would be os.environ.get('RELEASE_ID')). The bash script uses ruby to parse the json which I think is part of the default cedar stack for any buildpack.

like image 34
Lukas Eklund Avatar answered Sep 21 '22 02:09

Lukas Eklund


The above approach mentioned by Tomaž Zaman will do the job if you have less that 200 releases for your app. Otherwise will not... please refer to Ranges section from Heroku API Documentation:

https://devcenter.heroku.com/articles/platform-api-reference#ranges

You don't need Heroku Labs feature Metadata. This is what you need:

curl --request GET \
  --url https://api.heroku.com/apps/{APP_NAME_ID}/releases \
  --header 'Accept: application/vnd.heroku+json; version=3' \
  --header 'Authorization: Bearer {AUTH_TOKEN}' \
  --header 'Range: version;order=desc,max=1'

This will retrieve a list with latest releases (in this case only one, max=1) and what you need to do is to get response[0], that's it.

like image 45
Velizar Nenov Avatar answered Sep 17 '22 02:09

Velizar Nenov