Heroku app aliases
Using git remotes to alias your apps
We have four permanent Heroku apps running in production (our live site, our staging site, a development site and a demo) as well as various ephemeral apps that get created for long-running branches. Running Heroku CLI commands can become a bit of a pain in scenarios like this as you have to append the app name as an option:
# this will only work if you have a single 'heroku' git remote set up
$ heroku logs
becomes
# use the --app / -a option to specify the Heroku app you are targeting
$ heroku logs --app frosty-poptart-367
The app names are often longer than the command itself, and they have no obvious logic to them (by design - they are random). You can change the name of your apps, but the names have to be globally unique, so you can't have logical names like live
, uat
, dev
, demo
.
It would be really useful to be able to alias the real app names with your own logical names, and it turns out you can, using git remotes.
As well as the --app
option, the Heroku CLI supports the use of the --remote
option, which can be used instead of --app
wherever you might use that, once you have the relevant git remotes set up.
From within your git repo:
# set up the git remote 'dev' pointing to the Heroku app `frosty-poptart-367`
$ git remote add dev git@heroku.com:frosty-poptart-367
# you can now use either the app name or its git remote alias
$ heroku logs --app frosty-poptart-367
$ heroku logs --remote dev
As you'd expect, the --remote
option can be shortened to -r
:
# use the -r as the short version of --remote
$ heroku logs --remote dev
$ heroku logs -r dev
If you still think this is too much typing, you can of course alias it in your .bash_profile
or .bashrc
:
# pay it forward by always using the long option name in scripts -
# it helps those you come after you and have to read it
function hd(){
heroku "$@" --remote dev
}
Which then gives you the ultimate in shortening:
# all of the following do the same thing
$ heroku logs --app frosty-poptart-367
$ heroku logs -a frosty-poptart-367
$ heroku logs -remote dev
$ heroku logs -r dev
$ hd logs
Thanks to @_EvansD for pointing out the --remote
option, @dddagradi for confirming it works, and @MarkPundsack for his gist with the alias functions.
Making Freelance Work
Posted in: heroku