Deploying a Ruby on Rails application to Heroku is simple and straight forward.
But, what is Heroku? Heroku is a cloud platform that lets companies build, deliver, monitor and scale apps. This cloud platform offers tools to allow us to handle deployment, performance, monitoring, everything to do with infrastructure so we can focus on building our applications.
I am using Heroku as a learning laboratory to deploy to "production" my RoR progress Blog - Personal Site app. So, here are some of the pointers that I need to keep in mind while deploying to Heroku.
My RoR application requires a database. In my development environment, my local Linux Ubuntu, the database is SQLite, which is very convenient, easy to use and it was deployed with Rails. However, Heroku uses PostgreSQL database which means that if I deploy my app "as is" then Heroku issues an error on compilation. To avoid this error I need to create a production group in the gem file and add to it a PostgreeSQL gem "pg" to allow Rails to talk to Postgre.
The production group also needs 'rails_12factor' to allow static asset serving and loggin on Heroku. Adding the production group to the gem file does not affect my local development using SQLite because bundle install can be run with the flag '--without production', like thisBut, what is Heroku? Heroku is a cloud platform that lets companies build, deliver, monitor and scale apps. This cloud platform offers tools to allow us to handle deployment, performance, monitoring, everything to do with infrastructure so we can focus on building our applications.
I am using Heroku as a learning laboratory to deploy to "production" my RoR progress Blog - Personal Site app. So, here are some of the pointers that I need to keep in mind while deploying to Heroku.
Preparing My RoR for deployment
It is recommended to deploy to "production" early in the development process and as often as possible. In this learning exercise I am using an "agile" development approach, therefore, as soon as a sprint finishes a new feature, the production product is updated right away.My RoR application requires a database. In my development environment, my local Linux Ubuntu, the database is SQLite, which is very convenient, easy to use and it was deployed with Rails. However, Heroku uses PostgreSQL database which means that if I deploy my app "as is" then Heroku issues an error on compilation. To avoid this error I need to create a production group in the gem file and add to it a PostgreeSQL gem "pg" to allow Rails to talk to Postgre.
group : production do
gem 'pg'
gem 'rails_12factor'
end
$ bundle install --without production
Publish to Heroku
Publishing to the cloud from my local git repo I need to make sure that it is updated with all recent changes, therefore after changing the gem file I need to commit, then push from git to heroku. Needless to say that the Heroku command line tool needs to be locally installed and that we need to login to Heroku.To publish the master brach to Heroku run this command:
$ git push heroku master
Of course I could push any branch but at this time, the master branch is the one with all features. If the push yields no errors then I am in luck! In the case that there are errors, Heroku stores them in the logs file.
$ heroku logs
Fix the errors and continue. In my case, my 'novice' error was that I neglected to create a production group with Postgre gem in it. I still need the sqlite gem in the gem file to be able to use this SQLite during development. Therefore, a development group was created containing the sqlite gem. The reviewed development and production groups look like this:
group :development do
gem 'sqlite3'
end
group :production do
gem 'pg'
gem 'rails_12factor'
end
At this time Heroku does not know anything about our Object Model and the database has not been created yet, therefore, if I run the app after pushing then my app does not run, but if I run it any way, Heroku issues an app error page and the reason for the failure is written to the log and I could view it with "heroku logs" .
To create my database I need to run the rake db migration command in the Heroku command line tool. As a matter of fact, every time my Active Record model changes I need to run this command in the Heroku command line tool:
$ heroku run rake db:migrate
Now the app can be loaded in the browser using the command:
$ heroku open
There is no data however because that db has not been seeded, but this is a different story. At this time everything works and I can move to other things... mainly keep learning RoR!
Other Useful Heroku Commands
There are other commands that I needed to use and they are:$ heroku login
$ heroku create
$ heroku restart
The above command are self explanatory.
Stopping and Starting the App Running on Heroku
$ heroku ps:scale web=0
Setting "web=0" scales down the app to 0, and the result is the stopped app.
$ heroku ps:scale web=1
To start it up again scale the app to "web=1".
