ruk·si

🟪 Heroku
Ruby on Rails Setup

Updated at 2015-08-08 14:41

Install a Ruby version manager.

# Install Homebrew if you don't have it.
brew update
brew install rbenv ruby-build

rbenv install -l
rbenv install 2.2.2 # Or whatever Heroku currently supports
rbenv global 2.2.2  # Ruby version used if project doesn't have version defined

Installing dependencies for Ruby on Rails for Heroku.

gem install bundler --version 1.9.7 # Or whatever Heroku uses now
gem install rails --no-ri --no-rdoc --version 4.2.3 # Or latest

# Install PostrgreSQL and set it running
brew install postgresql93 # Or whatever version Heroku uses now
rm -r /usr/local/var/postgres
mkdir /usr/local/var/postgres
initdb /usr/local/var/postgres -E UTF8 --locale=en_US.UTF-8

PGDATA=/usr/local/var/postgres postgres # Runs? Good, close it

# Automatic start PostgreSQL on login
ln -sfv /usr/local/opt/postgresql93/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql93.plist

# Optionally install redis and set it running
brew install redis
# follow the install rules to start the redis

# Install Heroku toolbet to get the `foreman` version Heroku uses

Keep Ruby versions in sync. Bundler and Heroku read Ruby version from Gemfile. rbenv and rvm read Ruby version from .ruby-version. rbenv local command creates the .ruby-version.

Initialize a new Ruby on Rails project.

rails new ./my-app --database=postgresql
cd my-app
rbenv local 2.2.2 # Defines Ruby version for this project
rails generate controller pages index # pages controller with index action

# edit your Gemfile
# create Procfile for foreman
# add .env for foreman
bundle install

# Configure `database.yml`.
createuser -s my-app-db-dev # create as superuser
createuser -s my-app-db-test
bundle exec rake db:create
bundle exec rake db:schema:load
bundle exec rake db:migrate
bundle exec rake db:seed # fills database with all essential data
foreman start

# To test migrations before running them in the main database.
bundle exec rake db:test:prepare

# To run tests
bundle exec rake test

Configuring the production environment.

# These will be set on Heroku by default
RAILS_ENV = "production"
RACK_ENV = "production"
DATABASE_URL = A dev database add-on will be provisioned.

To initialize a staging and production Heroku databases. It is good idea to give an identifier to staging Heroku app, like "flagship", "banana" or "merkurius" as you might end up with multiple staging servers if you have multiple developers.

git remote add banana git@heroku.com:<HEROKU_APP>.git
git push banana SOURCE_BRANCH:master # usually feature branch
heroku run rake db:schema:load -a <HEROKU_APP>
heroku run rake db:migrate -a <HEROKU_APP>

# And the same with production Heroku app, replace banna with production.

Publishing to production.

heroku maintenance:on -a <HEROKU_APP>
git push production SOURCE_BRANCH:master
heroku run rake db:migrate -a <HEROKU_APP>
heroku restart -a <HEROKU_APP>
heroku maintenance:off -a <HEROKU_APP>

Some useful commands:

heroku logs -t -a <HEROKU_APP>       #
heroku restart -a <HEROKU_APP>       # Restarts all dynos, not any plugins
heroku releases -a <HEROKU_APP>      # Shows deployed versions.
heroku rollback v101 -a <HEROKU_APP> # Switch to previously deployed version.
heroku config -a <HEROKU_APP>        # Show environmental variables
heroku config:set KEY='value' -a <HEROKU_APP>