Deploying a Rails Application

Prerequisites

To deploy your Rails application on a remote server, you require a deployment tool like Capistrano.

To use Capistrano, add the following gems to your Gemfile. This will enable you to use Bundler and the Rails Asset Pipeline in conjunction with Capistrano.

gem 'capistrano'
gem 'capistrano-bundler'
gem 'capistrano-rails'

Afterwards, install the gems using bundler:

$ bundle install

The following instructions assume that you have installed Infopark Rails Connector to your Rails application. Note that Capistrano will only deploy applications from a git repository. Therefore, make sure that you have placed your application into a Git repository that can be accessed by the targeted server.

Configuration

Configure Capistrano for your Rails application:

$ cap install
mkdir -p config/deploy
create config/deploy.rb
create config/deploy/staging.rb
create config/deploy/production.rb
mkdir -p lib/capistrano/tasks
Capified

To prepare Capistrano to use Bundler and the Asset Pipeline, the corresponding modules need to be activated in the Capfile. For this, comment the following lines in:

require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'

Afterwards, please edit the my-project/config/deploy.rb file containing the general configuration:


set :application, '<em>my-project</em>'
set :repo_url, '<em>git-repository-url</em>'

set :deploy_to, '<em>target-directory</em>'

set :scm, :git

set :linked_files, %w{config/database.yml}

set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/syst    em}

namespace :deploy do
  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      execute :touch, release_path.join('tmp/restart.txt')
    end
  end
  after :publishing, :restart
  after :restart, :clear_cache
end

SSHKit.config.command_map[:rake] = "bundle exec rake"

Configure the specific servers and their credentials in the environment to be deployed. The following instructions illustrate this for the production environment, and are therefore applied to config/deploy/production.rb. Other environments can be configured analogously.

role :app, %w{remote-login@production-app-server}
role :web, %w{remote-login@production-web-server}
role :db,  %w{remote-login@production-db-server}

server 'production-app-server', user: 'remote-login', roles: %w{app}
server 'production-web-server', user: 'remote-login', roles: %w{web}

set :ssh_options, {
    user: 'remote-login',
    auth_methods: %w(publickey)
}

Adjust the configuration according to your needs:

  • my-project is the folder name of your Rails application.
  • git-repository-URL is the Git repository address (git checkout) of your Rails application.
  • target-path is the target directory path on the remote host.
  • remote-login is the user name under which your Rails application will be deployed on the target host.
  • production-app-server is the name of the server on which the Rails app is hosted.
  • production-web-server is the name of the server on which the Rails app is hosted for delivery. When using the Rails Connector, this corresponds to your application server.
  • production-db-server is the name of the server that makes the application database available.

Once a variable is set, it can be used as part of another variable. If, for example, you have used the application name as part of the repository address, you can refer to it like this: https://github.com/account/#{application}.

Note that your Git repository must have been provided with a Deploy Key for the remote login to grant the target server access to the repository.

Initial Deployment

An environment needs to be set up before the Rails app can be deployed to it. This can be achieved by means of the following command:

$ cap production deploy:check

DEBUG[bc509946]   mkdir: created directory 'target-path/my-project/shared/bin'
DEBUG[bc509946]   mkdir: created directory 'target-path/my-project/shared/log'
DEBUG[bc509946]   mkdir: created directory 'target-path/my-project/shared/tmp'
DEBUG[bc509946]   mkdir: created directory 'target-path/my-project/shared/tmp/pids'
DEBUG[bc509946]   mkdir: created directory 'target-path/my-project/shared/tmp/cache'
DEBUG[bc509946]   mkdir: created directory 'target-path/my-project/shared/tmp/sockets'
DEBUG[bc509946]   mkdir: created directory 'target-path/my-project/shared/vendor'
DEBUG[bc509946]   mkdir: created directory 'target-path/my-project/shared/vendor/bundle'
DEBUG[bc509946]   mkdir: created directory 'target-path/my-project/shared/public'
DEBUG[bc509946]   mkdir: created directory 'target-path/my-project/shared/public/system'

Once all the requirements are met, you can deploy the application using the following command:

$ cap production deploy

If your database schema changes in the course of an update, use cap production deploy:migrations to level up the database.

Commands for other Administration Tasks

Capistrano also provides commands for performing further administrative tasks along with the tasks described above. You can list the available commands using:

$ cap -T

cap deploy                         # Deploy a new release
cap deploy:check                   # Check required files and directories exist

cap deploy:updated                 # Updated
cap deploy:updating                # Update server(s) by setting up a new release
cap install                        # Install Capistrano, cap install STAGES=staging,production