How do I access the current Heroku release version programmatically?

吃可爱长大的小学妹 提交于 2020-08-22 08:09:32

问题


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


回答1:


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



回答2:


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 %>">



回答3:


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.




回答4:


if it's not in ENV then there isn't a magical place to pluck variables from.



来源:https://stackoverflow.com/questions/7917523/how-do-i-access-the-current-heroku-release-version-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!