Essential Heroku Commands for Java (Spring Boot) developers

This blog post captures the command used to create and configure java apps or spring boot apps deployed to Heroku Cloud. I use these commands in CI/CD deployment steps for continuous delivery and also to access the application's information from my local development machine.

These commands are tested with Heroku Client version: heroku/7.0.33 darwin-x64 node-v10.0.0


View help

heroku help

Login/Logout to heroku With email and password

heroku auth:login
heroku auth:whoami
heroku auth:logout

Login/Logout to heroku With token

# set HEROKU_API_KEY environment variable to login
export HEROKU_API_KEY=<<API Key from https://dashboard.heroku.com/account>>
heroku auth:whoami

# unset HEROKU_API_KEY environment variable to logout
unset HEROKU_API_KEY

# clear the history to clear the API Key
history -cw && cat /dev/null > ~/.bash_history

Show available regions for deployment

heroku regions

Create an application

heroku apps:create --app app-name --region eu --no-remote

# for deployment script
export APP_NAME=app-name
isAppAlreadyCreated=$(heroku apps | grep -c $APP_NAME)
if [ $isAppAlreadyCreated -eq 0 ]
then
    heroku apps:create --app $APP_NAME --region eu --no-remote
else
    echo "An existing application found"
fi

List applications

heroku apps

Display application's information

heroku apps:info --app app-name

Config variables (environment variable)

# list existing variables
heroku config --app app-name

# set a new variable
heroku config:set ENVIRONMENT_VARIABLE1="Some Value1" --app app-name

# unset an existing variable
heroku config:unset ENVIRONMENT_VARIABLE1 --app app-name

Deploy a spring boot jar file with system properties

heroku plugins:install heroku-cli-deploy

echo 'web: java -Dspring.profiles.active=live -Dserver.port=$PORT $JAVA_OPTS -jar target/my-app.jar' > Procfile

heroku deploy:jar --jar target/my-app.jar --app app-name --jdk 8

Open the application in browser

heroku apps:open --app app-name

Add custom domain

heroku domains:add test.example.com --app app-name

# for deployment script
export APP_NAME=app-name
isDomainAlreadyApplied=$(heroku domains --app $APP_NAME | grep -c "test.harishkannarao.com")
if [ $isDomainAlreadyApplied -eq 0 ]
then
    heroku domains:add test.harishkannarao.com --app $APP_NAME
else
    echo "Domain already applied"
fi

Upgrade to paid hobby plan and enable heroku SSL

heroku ps:resize web=hobby --app app-name

heroku certs:auto:enable --app app-name

Downgrade to free plan and disable SSL

heroku ps:resize web=free --app app-name

Check the application logs

heroku logs --app app-name --tail

List current scalability of an application

heroku ps:scale --app app-name

Stop or scale down an application

heroku ps:scale web=0 --app app-name

Start or scale up an application

heroku ps:scale web=1 --app app-name

Restart an application

heroku ps:restart --app app-name

Put and resume an application to/from maintenance mode

heroku maintenance:on --app app-name

heroku maintenance:off --app app-name

Delete an application

heroku apps:destroy --app app-name --confirm app-name

Comments

Popular posts from this blog

JSON with curl and jq

Import self signed in Linux for Chrome / Chromium headless testing

Colima - Drop In replacement for Docker Desktop for Mac and Linux