How do I access the current Heroku release version programmatically?

后端 未结 5 1612
一整个雨季
一整个雨季 2021-02-18 19:22

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

相关标签:
5条回答
  • 2021-02-18 19:38

    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 %>">
    
    0 讨论(0)
  • 2021-02-18 19:43

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

    0 讨论(0)
  • 2021-02-18 19:55

    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.

    0 讨论(0)
  • 2021-02-18 19:56

    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.

    0 讨论(0)
  • 2021-02-18 19:59

    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
    
    0 讨论(0)
提交回复
热议问题