Skip to content

Instantly share code, notes, and snippets.

@DeepNeuralAI
Last active April 25, 2019 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DeepNeuralAI/08d590f6de75e15bd76b2de003f6289d to your computer and use it in GitHub Desktop.
Save DeepNeuralAI/08d590f6de75e15bd76b2de003f6289d to your computer and use it in GitHub Desktop.
Rails - Heroku Deployment

Rails - Heroku Deployment

image


Heroku is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud - Source: Heroku

Local Setup - Heroku Command Line Interface

Install Heroku CLI

Once you install Heroku CLI, you will be able to run the heroku command.

Run heroku login

image

image

Add the pg gem

If you are using sqlite3, we are going to need to add the pg gem. If you created your rails app with --database=postgresql, you do not need to add the pg gem.

Verify database.yml file

In addition to using the pg gem, ensure that your config/database.yml file is using the postgresql adapter.

Sample config/database.yml:

# PostgreSQL. Versions 9.1 and up are supported.
default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: myapp_development

  # The specified database role being used to connect to postgres.
  # To create additional roles in postgres see `$ createuser --help`.
  # When left blank, postgres will use the default role. This is
  # the same name as the operating system user that initialized the database.
  #username: myapp

  # The password associated with the postgres role (username).
  #password:

  # Connect on a TCP socket. Omitted by default since the client uses a
  # domain socket that doesn't need configuration. Windows does not have
  # domain sockets, so uncomment these lines.
  #host: localhost

  # The TCP port the server listens on. Defaults to 5432.
  # If your server runs on a different port number, change accordingly.
  #port: 5432

  # Schema search path. The server defaults to $user,public
  #schema_search_path: myapp,sharedapp,public

  # Minimum log levels, in increasing order:
  #   debug5, debug4, debug3, debug2, debug1,
  #   log, notice, warning, error, fatal, and panic
  # Defaults to warning.
  #min_messages: notice

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: myapp_test

# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
#   DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
#   production:
#     url: <%= ENV['DATABASE_URL'] %>
#
production:
  <<: *default
  database: myapp_production
  username: myapp
  password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>

Root Page

Rails 5+ no longer has a static index page in production. Make sure that your app has a root page in your config/routes.rb

Verify in your config/routes.rb file:

root 'books#index'

Optional: No Root Page Exists

If you do not have a root page, you could create a controller called welcome

rails generate controller welcome

Add an index page:

In file app/views/welcome/index.html.erb, add a quick html page:

<h2>Hello World</h2>
<p>
  The time is now: <%= Time.now %>
</p>

Store your app in Git

Heroku relies completely on Git. You can deploy rails apps on Heroku from your local machine.

It is also good practice to push to Github as well.

Inside your rails application directory, run the following:

git add .
git commit -m "init"

Deploy your Application to Heroku

Run heroku create:

 $ heroku create
 > Creating app... done...

You can verify the remote was added by running: git config --list | grep heroku

If you see an error, for example, fatal: not in a git directory, you are not in your project folder.

Deploy Code:

Run git push heroku master

$ git push heroku master
remote: Compressing source files... done.
remote: Building source:
remote:
remote:  !     Warning: Multiple default buildpacks reported the ability to handle this app. The first buildpack in the list below will be used.
remote:             Detected buildpacks: Ruby,Node.js
remote:             See https://devcenter.heroku.com/articles/buildpacks#buildpack-detect-order
remote: -----> Ruby app detected
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-2.6.0
remote: -----> Installing dependencies using

....

Migrate your database

Run heroku run rake db:migrate

All commands that start heroku run are executed on the Heroku dyno.

You can also run an interactive shell by running heroku run bash.

Side Note: What's a Dyno?

All Heroku apps are run on lightweight Linux containers called dynos.

More information: Dynos

Heroku Dashboard

image

Adding API Keys and ENV Variables in Production

If you are using add-ons or your app communicates with third-party APIs, we can add environmental variables to our Heroku instance (production). We can also change our Heroku app's url in the dashboard.

image

Troubleshooting

Checking Dyno Running

Run heroku ps:scale web=1

Check the app's dyno state:

Run heroku ps Example output:

$ heroku ps
Free dyno hours quota remaining this month: 967h 44m (96%)
Free dyno usage for this app: 0h 0m (0%)
For more information on dyno sleeping and how to upgrade, see:
https://devcenter.heroku.com/articles/dyno-sleeping

View Logs

You can view information about your app using logs. Run heroku logs.

You can also get the verbose logs by running instead heroku logs --tail

Running a Rails console

Run heroku run rails console

OR

Run heroku run bash

Run Rake Commands

Run heroku run rake db:migrate

Add .env to .gitignore

echo ".env" >> .gitignore
git add .gitignore
git commit -m "add .env to .gitignore"

PaaS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment