How to get Heroku app name/URL from inside the app
HerokuHeroku Problem Overview
I'm working on a registration agent for http://www.dif.io to enable tracking of apps deployed on Heroku. I'm missing some pieces of information for the deployed app. The registration agent is a script (usually written in the app native language) which is designed to be executed after deployment (heroku run for example or automaticaly via some post deploy hook if any).
How do I get the application name, URL and some UUID identifier from inside the app, preferably from some ENV variables? I need it to be portable between languages.
I explored a sample Python application and all of the above info is missing. There are only couple more ENV variables related to Python. The dyno hostname however looks like an UUID.
I could use something like this but without the user/password requirements: https://addons.heroku.com/provider/resources/technical/reference/app-info
Please point me to the correct docs.
Heroku Solutions
Solution 1 - Heroku
The command app:info
no longer seems to be a heroku
command but just info
will do the same thing so:
heroku info -s | grep web_url | cut -d= -f2
or to set the variable
heroku config:set HEROKU_URL=$(heroku info -s | grep web_url | cut -d= -f2)
Solution 2 - Heroku
Dyno Metadata was introduced December 2015. It’s currently a labs feature which must be enabled first.
> Dyno metadata gives the dyno easy access to information about the app and environment. Examples of available dyno metadata include details about the release, dyno size, application name as well as the unique identifier for the particular running dyne.
Solution 3 - Heroku
There's a labs add-on that came out in Aug 2016 which puts all of these in your environment:
~ $ env
HEROKU_APP_ID: 9daa2797-e49b-4624-932f-ec3f9688e3da
HEROKU_APP_NAME: example-app
HEROKU_DYNO_ID: 1vac4117-c29f-4312-521e-ba4d8638c1ac
HEROKU_RELEASE_CREATED_AT: 2015-04-02T18:00:42Z
HEROKU_RELEASE_VERSION: v42
HEROKU_SLUG_COMMIT: 2c3a0b24069af49b3de35b8e8c26765c1dba9ff0
HEROKU_SLUG_DESCRIPTION: Deploy 2c3a0b2
Solution 4 - Heroku
Dynos themselves don't have any information about the application name or URL they are running, as far as I can tell. The documentation on Dynos lists what local environment variables are set, but it's only has PORT and DYNO.
One workaround for that we use in hubot is to require the user to add a config like HEROKU_URL with the app hostname.
I've found that you can script most of this, by using heroku app:info
with the -s
flag for shell-friendly, with a little bit of grepping and cutting:
$ heroku apps:info -s | grep web_url | cut -d= -f2
http://hubotio-campfire.herokuapp.com/
And to set HEROKU_URL from that:
$ heroku config:set HEROKU_URL=$(heroku apps:info -s | grep web_url | cut -d= -f2)
Solution 5 - Heroku
Here's what worked for me for the app name only:
heroku info -s | grep git-url | sed 's#.\+\.com/\(.\+\)\.git$#\1#g'
My use case was to find the app name for use in a shell script which needed the app name specifically (not the domain name). YMMV
I used the git-url
instead of web-url
because the domain name can be overridden to something custom, but the app name will always remain mapped to the git url. If looking for the domain name, check one of the other answers.
Solution 6 - Heroku
App name store in your url something like appname.herokuapp.com
.
You can get it in many way e.g. ENV['URL'].split(".").first
in python to get the first value form url when split by "." that represent your appname
Solution 7 - Heroku
For those who came here, trying to find a way to get the app URL for the review apps in Heroku, we have two additional environment variables available:
- HEROKU_BRANCH
- HEROKU_APP_NAME
So to infer the URL is just: "http(s)://HEROKU_APP_NAME.herokuapp.com"
Solution 8 - Heroku
For me it was as simple as:
heroku info
heroku info
=== secure-inlet-07449
Addons: heroku-postgresql:hobby-dev
Auto Cert Mgmt: false
Dynos: web: 1
Git URL: https://git.heroku.com/secure-inlet-07449.git
Owner: [email protected]
Region: us
Repo Size: 45 KB
Slug Size: 33 MB
Stack: heroku-20
Web URL: https://secure-inlet-07449.herokuapp.com/
The last line shows the app url: https://secure-inlet-07449.herokuapp.com/