Heroku CLI Cheat Sheet
Sooner or later our application is finished and ready to be seen by the world. So, there is a question: what should be done next? As a rule, you need to make it available for users on the web. In this case, Heroku can be quite helpful at this phase to easily get your application up and running.
Heroku is a cloud PAAS-platform supporting a bunch of programming languages. It is definitely not the only one alternative, but this article is dedicated precisely to it.
So, currently we are going to focus our attention on Heroku CLI (Command Line Interface). Unlike web applications, CLIs are much more powerful and require fewer resources to run. With the web version, you can use only the features the developers have implemented. With CLIs, you can easily mash-up multiple tools together for you to perform advanced tasks. They require a more technical expertise to be used but still work well for admin tasks, power-user tasks or developer products. Thus, Heroku CLI is the bare minimum which you will need to set up your application on Heroku.
CLI INSTALLATION
It is easy to install Heroku CLI using just the following command:
sudo snap install --classic heroku
In case your version of Ubuntu does not support snaps, you can type:
sudo curl https://cli-assets.heroku.com/install.sh | sh
To know more check out the Heroku documentation.
COMMANDS
- You can check the version of Heroku CLI
heroku --version
For example, this is the version I had on April 1, 2018:
- We can log into Heroku using the following command. Once you have entered the command, the Heroku website will be opened in your default browser. To log in, use the credentials that you have entered on the heroku.com.
heroku login
- If you would like to login without opening the browser, type:
heroku login -i
You will be prompted to enter your credentials in the CLI.
- After having logged in to Heroku for the first time, you may want to create your first application. Let us say you named it NAME_APP. Then enter the following:
heroku create NAME_APP
Here, the name of the application is optional. If it is omitted, Heroku will assign a unique name to your application.
- To list all your applications, use:
heroku apps
- The following command shows information about the application (here and further let us pretend that the application has the name NAME_APP)
heroku info NAME_APP
Here you can find the size of the application, URL, Git URL, the list of add-ons, etc:
- To rename the application, for example, from OLD_NAME to NEW_NAME, use:
heroku apps:rename NEW_NAME --app OLD_NAME
- To delete the application with the name NAME_APP, enter:
heroku apps:destroy --app NAME_APP --confirm NAME_APP
- You can also deploy your application in the form of a jar file. Let us say the name of the application is NAME_APP and the jar is named NAME_JAR. Then it can be done in the following way:
heroku deploy:jar NAME_JAR.jar --app NAME_APP
It is worth mentioning that Heroku has a default application startup timeout, which covers 90 seconds. If the application fails to start within that period, you will get an error. However, this is not a big deal, because you can retry multiple times. If the timeout is too short, you can contact tech support, they can change the default startup timeout. Unfortunately, this feature is available only in the paid plans.
Also, you need to have a Procfile in the same directory with the jar file and installed plugin-java for Heroku. For your convenience, Procfile and plugins will be described further below.
- To see the logs of the application, you can use the following command:
heroku logs --tail --app NAME_APP
- To restart the application, enter:
heroku restart --app NAME_APP
- To open the application, use:
heroku open --app NAME_APP
- To see the environment properties of the application on Heroku, enter:
heroku config --app NAME_APP
- To see all the environment properties of the application, try:
heroku run printenv --app NAME_APP
- To launch the application in the One-off dynos mode, enter the below mentioned command. A separate copy of the application will be launched from the last deployment. It is convenient for database migration or debugging. So, you need to execute the following command:
heroku run bash -a NAME_APP
By the way, here we used a shortened version (replaced -app with -a) and then you need to provide the command which launched the application (it is basically the content of the Procfile):
java -Dserver.port=$PORT -Dspring.profiles.active=dev $JAVA_OPTS -jar NAME_JAR.jar $JAR_OPTS
PROCFILE
Procfile is a regular text file that should be in the same directory with a jar file. It contains the command that will be run on the application startup.
Here is an example of a Procfile:
web: java $JAVA_OPTS -cp target/classes:target/dependency/* HelloWorldweb: java -Xmx256m -Dserver.port=$PORT $JAVA_OPTS -jar site-0.1.0-SNAPSHOT.jar
- To see the content of the Procfile of the currently deployed application, enter the following command:
heroku ps --app NAME_APP
PLUGINS
Also, you can use Heroku plugins. Here are some examples of commands to work with plugins:
- To see the list of installed plugins, enter:
heroku plugins
- To install a plugin with the name NAME_PLUGIN, use the following command:
heroku plugins:install NAME_PLUGIN
- To delete a plugin, type:
heroku plugins:uninstall NAME_PLUGIN
ADD-ONS
What if an application, for example, uses а database? Is it possible to install it on Heroku? Sure, it is. There is a great variety of different add-ons (Heroku Postgres, ClearDB MySQL, JawsDB MySQL, Bonsai Elasticsearch, Logentries, Papertrail, etc).
For instance:
- To see the list of installed add-ons, use:
heroku addons
- To add an add-on named NAME_ADD_ON to the application with the name NAME_APP and pricing plan PLAN_ADD_ON, enter the following command:
heroku addons:create NAME_ADD_ON:PLAN_ADD_ON -a NAME_APP
- To delete an add-on with an alias ALIAS_ADD_ON, use:
heroku addons:destroy ALIAS_ADD_ON --confirm NAME_APP
- To open the add-on with the alias ALIAS_ADD_ON, try:
heroku addons:open ALIAS_ADD_ON
To summarize, I would say this is the bare minimum required for the basic understanding of deploying applications to Heroku. Of course, this is not the only way of deploying, for example, you can visit this page, create your user and application, as well as perform the deployment via the visual interface. However, using CLI may be more convenient in case there is no browser available or it is not possible to use the web interface.
Tap the 👏 button if you found this article useful!
About the Author
Fedir is a code review ninja of Server-side Team at OmiSoft.
Do you need an up-to-date back-end for your system? Click here to get an estimate! Or find us on Facebook and Twitter.