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 app so other people can see that number?
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 app so other people can see that number?
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.
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.
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}')
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
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.
AFAIK you can only get the version via the CLI:
heroku releases --app <YOUR_APP_NAME>
You could do this via the Heroku gem from you app, but this is probably more trouble that it's worth.
I am using Codeship so I plan on just adding this to the push config:
heroku config:add HEROKU_RELEASE_VERSION=$(heroku releases | head -2 | awk 'NR==2' | awk '{print $1}')
Note that the other similar answer is invalid since it is grabbing some of the later versions (it uses tail instead of head) and it tries to set it to two versions instead of just one (ie: v100 \n v101
).
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.
Following @jassa answer - but using the more recent PlatformAPI
if (app_name = ENV["HEROKU_APP_NAME"]).present? and ENV['HEROKU_API_KEY'].present?
require 'platform-api'
heroku = PlatformAPI.connect(ENV['HEROKU_API_KEY'], default_headers: {'Range' => 'version ..; order=desc'})
released_at_s = heroku.app.info(app_name)['released_at']
released_at_d = Time.parse(released_at_s).strftime('%Y-%m-%d')
release = heroku.release.list(app_name).first
deploy_v = release['description']
version = release['version']
ENV['HEROKU_RELEASE_NAME'] = "#{version} (#{deploy_v}) #{released_at_d}"
end