ruk·si

Heroku Basics

Updated at 2015-09-26 23:05

Basic commands how to use Heroku.

Setup:

brew install heroku

# the most important command
heroku help <COMMAND>

heroku login            # add account credentials and uploads public SSH key
cat ~/.netrc            # contains your Heroku API tokens
heroku auth:whoami      # shows currently active user
heroku keys             # shows your active SSH keys

Creating a new application or joining an existing one.

mkdir my-app
cd my-app
git init
heroku create my-app                    # create app
heroku create my-app --org my-org       # create app in organization

# link code repository to an Heroku app
git clone git@github.com:organization/my-app.git
cd my-app/
heroku git:remote -r production -a my-app
git remote -v # shows that you have new remote named `production`

Getting application info.

heroku apps                     # lists your apps
heorku apps:info -a my-app      # info on a single app
heroku logs -t -a my-app        # see what the app is doing
heroku ps -a my-app             # see all running dynos

Configuring applications.

heroku config                         # list environmental variables
heroku config:set MANDRILL_PORT=587   # add environmental variable
heroku config:unset MANDRILL_PORT     # remove environmental variable

Running commands in your application server.

# run one of commands
heroku run rake db:schema:load -a my-app    # initialize database schema
heroku run rake db:migrate -a my-app        # run pending migrations
heroku run console -a my-app                # opens Rails console

Deployment flow

# if no database changes have been made
git push production master:master

# if there are pending migrations, causes small unavailability window
heroku maintenance:on -a my-app
git push production master:master
heroku run rake db:migrate -a my-app
heroku restart -a my-app
heroku maintenance:off -a my-app

Rollbacking application deployment.

heroku releases -a my-app
heroku rollback v101 -a my-app

# if were database changes in the previous release
heroku maintenance:on -a my-app
heroku rake db:rollback
heroku rollback v101 -a my-app
heroku maintenance:off -a my-app

Adding addons.

# creates free postgresql
heroku addons:create heroku-postgresql -a my-app

# creates $9/month one
heroku addons:create heroku-postgresql:hobby-basic -a my-app

# wait database to be created
heroku pg:wait

Use Rails console to debug query problems on staging or production. Also possible to modify the database for hotfixes, but use sparingly.

heroku run console -a my-app

# debug what queries Rails generates
ActiveRecord::Base.logger = Logger.new(STDOUT)
User.first

# try out path helpers for validation
app.user_path(User.first)

# you can access all the helpers
helper.link_to "Home", app.root_path