Deploying Ruby on Rails

Ruby on Rails is natively supported on OpenShift. You can simply deploy Rails application the same way as any other Ruby application. There are no hidden catches, it just works.

However, OpenShift gives you some nice-to-have features for deploying Ruby on Rails to make your life easier.

Add the Framework

You can deploy an existing application or you can create a new one. In case you are deploying your existing application, simply copy the code to your OpenShift git repository.

If you are starting a new application, you can also deploy a template. The following command adds Rails 4 to the myapp Ruby application and deploys it to OpenShift. Note that this code requires a database cartridge, so you should add one now if you have not yet done so.

$ cd myapp
$ git pull -s recursive -X theirs https://github.com/openshift/rails4-example.git
$ git push

You could also add the template code at the same time as creating your application, using the --from-code option:

rhc app create myapp ruby-2.0 postgresql-9.2 --from-code=https://github.com/openshift/rails4-example.git -s

The -s option makes the application auto-scaling.

Asset pipeline

To prevent a long and unnecessary compilation of assets on application initial deployment and re-deployments, these two steps must be done.

Step 1
It’s necessary to install sprockets gem by adding the line gem 'turbo-sprockets-rails3' into your Gemfile and run bundle install.

Step 2
After sprockets gem is installed you need to precompile all your assets locally by rake assets:precompile, which compiles all the assets into public/assets. When compiling the assets the sprocket gem creates a file called sources_manifest.yml, located also in public/assets. This manifest contains names of all assets files together with their hash values. This file ensures that only changed assets will be recompiled on re-deployment.

Loading database schema

If your Rails application contains a large amount of migrations it’s good to use db:schema:load on initial deploy and db:migrate on re-deploymnets. You can do this by looking into the database and checking whether one of the DB tables exists.

This example checks, in the deploy hook, if the spree_activators table is present in the database.

if [ echo "use $OPENSHIFT_APP_NAME; show tables" | mysql | grep spree_activators ]
then
    bundle exec rake db:schema:load RAILS_ENV="production"
else
    bundle exec rake db:migrate RAILS_ENV="production"
fi

Using RAILS_ENV=development

In OpenShift you can use the Rails development environment as you do when you are developing the Rails application locally. To instruct OpenShift to deploy your application in development mode, you need to set this user-environment variable:

  • RAILS_ENV (eg. rhc env set RAILS_ENV=development)

When the Rails application run under development environment OpenShift will:

  • Skip the automatic static asset (re)compilation

  • Disable bundle command unless you do modification to the application Gemfile

  • Set web server to run your application in 'development' mode

  • Skip full restart of the Apache as the code is reloaded automatically

The development mode can speed up the development phase of you application in OpenShift, but it is not recommended to use this mode for production.