Nitrous.IO and Heroku: A Perfect Pair

Thom Parkin
Share

256-spedometer

This article is a follow-up to Nitrous.IO: Rails Development in the Cloud published earlier. If you have not read that article, I recommend you do so. I will wait.

Although it is not a prerequisite, this tutorial assumes you have an account on Nitrous.IO and have setup a Box for Ruby.

Start with a Nitrous Box

If you have not created a Box yet on Nitrous.io, select the Boxes menu and create a new Box.

The Nitrous Box must be connected to your Github account. These instructions in the Nitrous Help Center are the best guide to accomplish that task.

Login to Heroku

This assumes you already have an account with Heroku. If not create one now.
Heroku provides a very detailed API (called Toolbelt) and it is part of the Nitrous instance when you create a Box.

$ heroku login

You are expected to provide your login email and password. This should be greeted with an acknowledgement:

Authentication successful.

If this is the first time using this Nitrous Box to deploy to Heroku, you need to add your SSH key to Heroku. If you are not sure whether this step has been accomplished in the past, it will not hurt to just run the command again.

$ heroku keys:add

Create Your App

Create a Rails Application

Next you can simply create a Rails application. We will specify a PostgreSQL database (because that is what all the ‘cool kids’ are using nowadays).

$ rails new APPNAME --database=postgresql
$ cd APPNAME
Setup Git

The usual (and should be quite familiar) initial commit in Git to get it setup goes like this:

$ git init
$ git config --global user.name "Cody Wrighter"
$ git config --global user.email "C.Wrighter@ParaHacker.com"
$ git add .
$ git commit -m "Initial Commit on Nitrous Box"
Create a New Application on Heroku

With the Heroku Toolbelt you create an app on Heroku with this simple command:

$ heroku create

It will respond with the name of the app on Heroku. I recommend you copy the peculiar (and often poetic) application name for use in a later step.

Since Bundler is also included in the Nitrous instance, add these lines in the Gemfile:

ruby '2.0.0'  # to maintain parity between 'development' and 'production'

gem 'pg'  #this may already exist in the Gemfile
gem 'rails_12factor', group: :production

You should use RVM to install Ruby 2.0.0 on Nitrous. RVM is also included in every Nitrous box, so this is as simple as rvm install 2.0.0 followed by rvm use 2.0.0

Setting up a PostgreSQL database on Heroku is next.

Using the Heroku Toolbelt,

$ heroku addons:add heroku-postgresql:dev

will add a PostgreSQL database (the free level) and return a HEROKU_POSTGRES_COLOR_URL. Take note of the actual COLOR that is returned.

Then

$ heroku pg:promote HEROKU_POSTGRESQL_URL

to establish the database (You need to log into your Heroku Dashboard to get the PostreSQL details).

Normally, when creating a Rails application, the next step would be to edit the config/database.yml file. But with Heroku, your production database details are applied automatically on the Heroku server regardless of what you place in the database.yml file.

To use a Heroku PostgreSQL database for development also, follow the instructions here. If you prefer a local SQLite database for development, those instructions are at the end of this article.

Now it is time for Bundler to work its magic for us and pull all the pieces together.

$ bundle install

Setup the Asset Pipeline

Add a line in your config/application.rb

config.assets.initialize_on_precompile = false

Commit in Git

$ git add .
$ git commit -m "Heroku and Postgresql setup"

Deploy the Application to Heroku

Deploying to Heroku is the simplest part of this whole process. I truly appreciate that because it is the part that I do most often in a continuous development cycle.

$ git push heroku master

Setting Up the Server on Heroku

If you want to remain within the FREE realm, use a single dyno on Heroku.

$ heroku ps:scale web=1
$ heroku ps

Visit the App on the Server

At this point I like to perform a quick “smoke test” just to be sure we didn’t miss any of the essential steps. This saves a lot of headaches later when you are troubleshooting the application because you can be assured it works at the base level.
This is where you will use the [sometimes poetic, sometimes profound] name Heroku created for your application. You can paste the name from your clipboard (if you followed my earlier advice)

$ curl http://<HEROKU_APP_NAME>.herokuapp.com/

Or you can navigate to the instance (based on the name that it returned earlier):
I just open another tab in my browser that is currently connected to Nitrous.

You should get back the Rails default index.html page. We have not yet created any models, controllers, or routes so the “About your application’s environment” link will yield a server error.

That is to be expected.

To view the application logs, with Heroku ToolBelt, use heroku logs. The ‘status=200’ on the very last line is what we expect to see.

Now we need to migrate the database.

$ heroku run rake db:migrate

A Quick Assesment of Our Progress

You have successfully created and deployed a Rails application ENTIRELY FROM WITHIN A BROWSER !

Leveraging Nitrous.IO as a cloud-based development platform and Heroku as a cloud-based SAAS host, you can be completely divorced and indiscriminate about your client hardware and/or software.

Set Up the Development Server

In order to transform this empty, benign Rails application into your next big money-making Disruptive Web Product, we need to do some heavy testing-coding-refactoring Agile development.
Let’s now get our Development environment setup.

In the Gemfile add gem 'unicorn', group: :development and run bundle install to install it locally.

To test (and for each iteration during development) run this command

$ bundle exec unicorn -p 3000 -E development

Use the Nitrous menu Preview -> Port 3000 to test your development environment

One Final Commit
$ git add .
$ git commit -m "use unicorn for local development"
$ git push heroku

If you prefer unicorn for production, here are details on setting it up.

Final Cleanup

Of course, it is important to rm the public/index.html file.

I recommend you also maintain the project in your Github account. As a public repository you can get help from the Open Source community in the form of contributions and Pull Requests. With a Private repository, you can collaborate with other developers on your team and maintain the central source control.

The Nitrous Box must be connected to your Github account. This great article in the Nitrous Help Center explains how to setup the Github Private Key.

First create the repository on Github. Then issue these two commands:

$ git remote add github git@github.com:<github_username>/nitrous_heroku.git
$ git push -u github master

Next, write some tests.
And then you will create some models, controllers and views and begin actually developing the application.

Run the unicorn server with this command

$ bundle exec unicorn -p 3000 -E development

Use the Nitrous menu Preview -> Port 3000 to test your development environment


Update the Development Environment

If you prefer to use sqlite in development (which I do) then add these steps.

Modify the config/database.yml for ‘development’ (and ‘test’) like this:

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

Add sqlite3 to the Gemfile

group :development, :test do   #sqlite3 gem is not supported on Heroku
  gem 'sqlite3'
end

Call on Bundler

$ bundle install

Then Migrate the database

$ export RAILS_ENV=development
$ rake db:migrate

Give it a test drive by calling the unicorn server

$ bundle exec unicorn -p 3000 -E development

And then use the Nitrous menu Preview -> Port 3000 to open it in a browser

You now have complete development and production environments, along with git-based deployment, and you didn’t need to install anything on your local computer.

I am excited to usher-in a new Browser-based workflow.

CSS Master, 3rd Edition