Deploy

You’ll need to prepare your Rails application for deployment to Heroku.
Gemfile

We need to modify the Gemfile for Heroku.

We add a group :production block for gems that Heroku needs:

    pg – PostgreSQL gem
    thin – web server
    rails_12factor – logging and static assets

Heroku doesn’t support the SQLite database; the company provides a
PostgreSQL database. Though we won’t need it for our tutorial
application, we must include the PostgreSQL gem for Heroku. We’ll mark
the sqlite3 gem to be used in development only.

The Thin web server is easy to use and requires no configuration. Note
that Heroku recommends Unicorn for handling higher levels of traffic
efficiently. Unicorn can be difficult to setup and configure, so we’re
using Thin for our tutorial application.

On Heroku, Rails needs an extra gem to handle logging and serve CSS and
JavaScript assets. The rails_12factor gem provides these services.
Enable Email and Set Heroku Environment Variables

Enable Email

You’ll need to enable email for production or else you’ll get errors
when your application tries to send email from Heroku.

To use Gmail from Heroku, add the following to your
config/environments/production.rb file:

config.action_mailer.default_url_options = { :host =>
Rails.application.secrets.domain_name }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.smtp_settings = {
  address: "smtp.gmail.com",
  port: 587,
  domain: Rails.application.secrets.domain_name,
  authentication: "plain",
  enable_starttls_auto: true,
  user_name: Rails.application.secrets.email_provider_username,
  password: Rails.application.secrets.email_provider_password
}

Be sure to add the new settings before the end keyword in the file. The
settings can be added anywhere, as long as they precede the end keyword!

You’ll need to specify the unique name you’ve selected for your hosted
application. We’re using the Rails.application.secrets.domain_name
configuration variable in two places in the file. The config/secrets.yml
file sets configuration variables for use in production, which are
obtained from Heroku environment variables.

Next we’ll set Heroku environment variables.
Set Heroku Environment Variables

You’ll need to set Heroku environment variables to provide configuration
values for the config/secrets.yml file. Heroku doesn’t have a .bashrc or
.bash_profile file, so you’ll set set environment variables on Heroku
with heroku config:add.

$ heroku config:add GMAIL_USERNAME='myname@gmail.com'
GMAIL_PASSWORD='secret'
$ heroku config:add MAILCHIMP_API_KEY='mykey'
MAILCHIMP_LIST_ID='mylistid'
$ heroku config:add OWNER_EMAIL='me@example.com'
DOMAIN_NAME='myapp.herokuapp.com'

When you set myapp.herokuapp.com, replace myapp with the name that
Heroku is using for your application. If you want to use a custom domain
name, you’ll need to set up DNS (domain name service), which we won’t
cover in this tutorial.

Check that the environment variables are set with:

$ heroku config

See the Heroku documentation on Configuration and Config Vars and the
article Rails Environment Variables for more information.