Contents

Using Capistrano to Deploy Rails App

Contents

After using Docker to deploy last month, I’ve decided to try deploying using Capistrano and nginx for my current project.

Helpful links: http://robmclarty.com/blog/how-to-deploy-a-rails-4-app-with-git-and-capistrano

https://www.digitalocean.com/community/tutorials/how-to-automate-ruby-on-rails-application-deployments-using-capistrano

https://www.phusionpassenger.com/library/walkthroughs/deploy/ruby/ownserver/nginx/oss/precise/deploy_app.html

/etc/nginx/sites-enabled/myapp.conf

server {
    listen 80;
    server_name mydomain.com;

    # Tell Nginx and Passenger where your app's 'public' directory is
    root /var/www/myapp/current/public;

    # Turn on Passenger
    passenger_enabled on;
    passenger_ruby /home/bchoy/.rvm/gems/ruby-2.2.3/wrappers/ruby;
}

Required the following in my Capfile:

require 'capistrano/passenger # Restarts Passenger'
require 'capistrano/rails/assets # Precompiles assets'
require 'capistrano/rvm # Uses RVM'
require 'capistrano/bundler # Runs bundle install'

deploy.rb precompile assets:

# set the locations that we will look for changed assets to determine whether to precompile
set :assets_dependencies, %w(app/assets lib/assets vendor/assets Gemfile.lock config/routes.rb)

# clear the previous precompile task
Rake::Task["deploy:assets:precompile"].clear_actions
class PrecompileRequired < StandardError; end

# set the locations that we will look for changed assets to determine whether to precompile
set :assets_dependencies, %w(app/assets lib/assets vendor/assets Gemfile.lock config/routes.rb)

# clear the previous precompile task
Rake::Task["deploy:assets:precompile"].clear_actions
class PrecompileRequired < StandardError; end

namespace :deploy do
  namespace :assets do
    desc "Precompile assets"
    task :precompile do
      on roles(fetch(:assets_roles)) do
        within release_path do
          with rails_env: fetch(:rails_env) do
            begin
              # find the most recent release
              latest_release = capture(:ls, '-xr', releases_path).split[1]

              # precompile if this is the first deploy
              raise PrecompileRequired unless latest_release

              latest_release_path = releases_path.join(latest_release)

              # precompile if the previous deploy failed to finish precompiling
              execute(:ls, latest_release_path.join('assets_manifest_backup')) rescue raise(PrecompileRequired)

              fetch(:assets_dependencies).each do |dep|
                # execute raises if there is a diff
                execute(:diff, '-Naur', release_path.join(dep), latest_release_path.join(dep)) rescue raise(PrecompileRequired)
              end

              info("Skipping asset precompile, no asset diff found")

              # copy over all of the assets from the last release
              execute(:cp, '-r', latest_release_path.join('public', fetch(:assets_prefix)), release_path.join('public', fetch(:assets_prefix)))
            rescue PrecompileRequired
              execute(:rake, "assets:precompile")
            end
          end
        end
      end
    end
  end
end

Source: https://coderwall.com/p/aridag/only-precompile-assets-when-necessary-rails-4-capistrano-3