Every time I push my app to heroku I see the line
-----> Launching... done, v43
Is there a way to make that version number apear within the
After every push
, run (assuming a bash
shell):
heroku config:add rel=$(heroku releases | tail -2 | awk '{print $1}')
You can then just access the rel
environment variable from your app.
Alternatively, if you'd like to present the date of the push, which, IMO, represents more meaningful information:
heroku config:add rel=$(heroku releases | tail -2 | awk '{print $5,$6,$7}')
Or, my favorite, which contains date and version with no whitespace:
heroku config:add rel=$(heroku releases | tail -2 | awk '{print $5"t"$6$7"."$1}')
You can make a Ruby file to do the following:
require 'heroku'
heroku = Heroku::Client.new('username','password')
puts heroku.releases('appname')
This returns a JSON document that contains a ton of metadata about your deploys include SHA, version number, etc.
I had the same problem and did it through a deploy POST HTTP hook. Basically the logic is that I created a specific URL in my app and I post the new value to update the config variable.
I did it in Python/Django, but I’m sure the same logic can be used for other languages as well:
import heroku
cloud = heroku.from_key(settings.HEROKU_API_KEY)
app = cloud.apps['mycoolapp']
latest_release = app.releases[-1]
app.config['RELEASE'] = latest_release.name
result['status'] = 200
Why would you want to depend on running a command after every push? The accepted answer is worse than setting the config yourself.
Instead add to your Gemfile:
gem 'heroku-api'
Add your App name and API key to the Heroku config:
$ heroku config:add HEROKU_APP_NAME=myapp HEROKU_API_KEY=bp6ef3a9...
Then put something like this in config/initializers/heroku.rb
:
unless (app_name = ENV["HEROKU_APP_NAME"]).nil?
require 'heroku-api'
heroku = Heroku::API.new(:api_key => ENV["HEROKU_API_KEY"])
release = heroku.get_releases(app_name).body.last
ENV["HEROKU_RELEASE_NAME"] = release["name"]
end
Finally:
puts ENV["HEROKU_RELEASE_NAME"]
=> v42
Now it's fully automated. You can forget about it and continue to work on your app.
In Node.js using JavaScript fetch
(and the forthcoming async/await), you can do it with the following code (no push hooks!):
const fetch = require('node-fetch');
const url = 'https://api.heroku.com/apps/myapp/releases';
const headers = {
Accept: 'application/vnd.heroku+json; version=3',
Authorization: 'Basic '+base64Encode(':'+process.env.HEROKU_API_TOKEN)
};
const response = await fetch(url, { headers });
const releases = await response.json();
const lastRelease = releases[releases.length-1];
const version = lastRelease.version;
const created = lastRelease.created_at;
using
function base64Encode(str) {
return new Buffer(str, 'binary').toString('base64');
}
Note this requires
$ heroku config:set HEROKU_API_TOKEN=\`heroku auth:token`.
See devcenter.heroku.com/articles/platform-api-reference#release-list.
It's now possible to try the Heroku feature Roberto wrote about in his answer, without contacting Heroku. It's called Heroku Labs: Dyno Metadata and you can enable it by
heroku labs:enable runtime-dyno-metadata -a <app name>
and then the information is available (on the next deploy) as environment variables:
~ $ env
HEROKU_APP_ID: <some-hash-appId>
HEROKU_APP_NAME: example-app
HEROKU_DYNO_ID: <some-hash-dynoId>
HEROKU_RELEASE_VERSION: v42
HEROKU_SLUG_COMMIT: <some-hash-slugCommit>
HEROKU_SLUG_DESCRIPTION: Deploy 2c3a0b2
...
We don't have to setup any config file or else.