Skip to content

Latest commit

 

History

History

deployment

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Deploying to Heroku with Rails & Postgres

Before You Do Anything

  1. Make sure your app is under version control with git. If you're not sure if your project is under version control yet, you definitely haven't been committing often enough. Run git status to check if your project directory is a git repo. If not, run git init to make it into one, then commit everything you have so far. If you have any secret keys (such as API keys), hide them in environment variables before committing!

  2. If you haven't already, sign up for a Heroku account.

  3. If you haven't already, install the Heroku toolbelt.

Create Your Heroku App

  1. Add a new remote to your project repository that points to Heroku's servers. Your project must be a git repo to continue.

In the Terminal, from your project's root directory, run:

```zsh
➜  heroku create YOUR_APP_NAME
```

If you don't supply a name for your app, Heroku will create a random one for you. We strongly suggest giving your app a name to personalize it and reflect its purpose.

  1. Also in the Terminal, from your project's root directory, run:
➜  git remote -v

You should see something like this:

heroku	https://git.heroku.com/YOUR_APP_NAME.git (fetch)
heroku	https://git.heroku.com/YOUR_APP_NAME.git (push)
origin	https://github.com/YOUR_GITHUB_USERNAME/YOUR_GITHUB_REPO_NAME.git (fetch)
origin	https://github.com/YOUR_GITHUB_USERNAME/YOUR_GITHUB_REPO_NAME.git (push)

Prepare Your App for Deploy

  1. Check your Gemfile to make sure you're using the pg gem:
#
# Gemfile
#

gem 'pg'
  1. Add the rails_12factor gem to your Gemfile in the production group:
#
# Gemfile
#

group :production do
  gem 'rails_12factor'
end
  1. Also, move the sqlite3 gem into the development/test block in the Gemfile:

Gemfile

group :development, :test do

gem 'sqlite3'

end



3. Run this command in your Terminal to bundle install locally:

```zsh
➜  bundle install --without production

Deploy to Heroku

  1. You should be all set up now, so add and commit your changes, then push to Heroku:
➜  git status
➜  git add -A
➜  git commit -m "ready for deploy"
➜  git push origin master
➜  git push heroku master

Your Terminal output should look something like this (but a little longer):

Initializing repository, done.
Counting objects: 64, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (53/53), done.
Writing objects: 100% (64/64), 14.57 KiB | 0 bytes/s, done.
Total 64 (delta 5), reused 0 (delta 0)

-----> Ruby app detected
-----> Compiling Ruby/Rails
-----> Using Ruby version: ruby-2.0.0
-----> Installing dependencies using 1.5.2
       New app detected loading default bundler cache
       Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment
       Fetching gem metadata from https://rubygems.org/..........
       Fetching additional metadata from https://rubygems.org/..
       Using i18n (0.6.9)
       .
       .
       .
       Installing sass-rails (4.0.3)
       Installing rails (4.0.4)
       Your bundle is complete!
       Gems in the groups development and test were not installed.
       It was installed into ./vendor/bundle
       Bundle completed (11.82s)
       Cleaning up the bundler cache.
-----> Writing config/database.yml to read from DATABASE_URL
-----> Preparing app for Rails asset pipeline
       Running: rake assets:precompile
       I, [2014-05-02T18:02:09.672047 #732]  INFO -- : Writing /tmp/build_625a98e6-1b9e-4e57-ba48-8f9cd7bf7d18/public/assets/application-c8d048bf2b32f85ef4807549fa44b21b.js
       I, [2014-05-02T18:02:09.694428 #732]  INFO -- : Writing /tmp/build_625a98e6-1b9e-4e57-ba48-8f9cd7bf7d18/public/assets/application-d0b54dd563966c42aad5fd85b1c1f713.css
       Asset precompilation completed (6.52s)
       Cleaning assets
       Running: rake assets:clean
-----> WARNINGS:
       Include 'rails_12factor' gem to enable all platform features
       See https://devcenter.heroku.com/articles/rails-integration-gems for more information.

-----> Compressing... done, 21.4MB
-----> Launching... done, v6
       http://thingsthingsthings.herokuapp.com/ deployed to Heroku

To git.heroku.com/YOUR_APP_NAME.git
 * [new branch]      master -> master
  1. This is a common point for people to run into errors. The most common error that happens here is your assets failing to compile. If you have errors, try this possible solution:
  • From the Terminal, precompile your assets:

    ➜  rake assets:precompile
  • Add and commit new changes, then try pushing to Heroku again:

    ➜  git status
    ➜  git add -A
    ➜  git commit -m "precompiling assets"
    ➜  git push origin master
    ➜  git push heroku master
  1. Your deployed app has a separate database from your development environment. To set up your Heroku database, run your migrations in production:
➜  heroku run rake db:migrate
  1. If all went well, you should be able to visit your live application by running:
➜  heroku open

Debugging

Hopefully your app works on Heroku, however, you may see a sad page that looks like this...

heroku-application-error

  1. If this happens to you, check your Heroku logs in the Terminal:
➜  heroku logs
  1. Scan all of the logs for error messages. If you see obvious error messages, google what they mean. If you still can't find a solution, now would be a good time to ask an instructor for help.

Add Your API Keys

  1. Set environment variables on Heroku:

Change MY_API_KEY to your variable name and your actual key

➜  heroku config:set MY_API_KEY=0932nv8d17vhd72o2e8cfv82csd9n1dcd98
  1. Check that it worked:
➜  heroku config
MY_API_KEY: 0932nv8d17vhd72o2e8cfv82csd9n1dcd98
  1. If you made a mistake and need to unset an API key:
➜  heroku config:unset MY_API_KEY
Unsetting MY_API_KEY and restarting myapp... done, v13

Heroku Doc References

Basic Heroku Rails, PostgreSQL Setup:

Adding Collaborators on Heroku:

Adding a Git Remote:

Your Heroku collaborators should add the project's Heroku remote to their local git repositories.