How do I access the current Heroku release version programmatically?

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

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

5条回答
  •  离开以前
    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.

提交回复
热议问题