Question

I'm trying to set up Capistrano deployment for the first time and I wanted to test it on my development directory before attempting it on production. Normally I wouldn't even bother with Capistrano in the dev environment, but I'm having issues when deploying. It seems that Capistrano wants to:

A) run the bundle command: bundle --without development test

and

B) run rake assets:precompile in the development environment. I don't want that. Why would I? Maybe in the 'staging' environment if I ever wanted to do that, but certainly not in development mode.

The biggest hurdle at the moment is the fact that it bundles thinking it's in production mode and thus skips over gems which are required when it DOES correctly use the development environment when precompiling assets.

EDIT: Here's a sample of two scripts which are run - the first scripts run bundler as if we're in the production environment, and the last runs it in development environment (RAILS_ENV=development). We of course get the error because BetterErrors is a gem that's loaded only in the development environment and so it can't find BetterErrors because the call to bundle was in the production environment.

 INFO [9797fc64] Running ~/.rvm/bin/rvm default do bundle install --binstubs /home/vps_user/rails_deployments/dev.www/shared/bin --path /home/vps_user/rails_deployments/dev.www/shared/bundle --without development test --deployment --quiet on localhost
DEBUG [9797fc64] Command: cd /home/vps_user/rails_deployments/dev.www/releases/20140217224858 && ~/.rvm/bin/rvm default do bundle install --binstubs /home/vps_user/rails_deployments/dev.www/shared/bin --path /home/vps_user/rails_deployments/dev.www/shared/bundle --without development test --deployment --quiet
 INFO [9797fc64] Finished in 1.883 seconds with exit status 0 (successful).
DEBUG [da905ff7] Running /usr/bin/env if test ! -d /home/vps_user/rails_deployments/dev.www/releases/20140217224858; then echo "Directory does not exist '/home/vps_user/rails_deployments/dev.www/releases/20140217224858'" 1>&2; false; fi on localhost
DEBUG [da905ff7] Command: if test ! -d /home/sprvps_userucewo/rails_deployments/dev.www/releases/20140217224858; then echo "Directory does not exist '/home/vps_user/rails_deployments/dev.www/releases/20140217224858'" 1>&2; false; fi
DEBUG [da905ff7] Finished in 0.044 seconds with exit status 0 (successful).
 INFO [0562438c] Running ~/.rvm/bin/rvm default do bundle exec rake assets:precompile on localhost
DEBUG [0562438c] Command: cd /home/vps_user/rails_deployments/dev.www/releases/20140217224858 && ( RAILS_ENV=development ~/.rvm/bin/rvm default do bundle exec rake assets:precompile )
DEBUG [0562438c]        rake aborted!
DEBUG [0562438c]        uninitialized constant BetterErrors

Is there something wrong with my setup? I'm using Capistrano v3+, with Ruby v2.1.0 inside rvm.


Gemfile:

if RUBY_PLATFORM !~ /mingw/
    gem 'capistrano-rails'
    gem 'capistrano-rvm'
    gem 'capistrano-bundler'
end

Capfile:

# Load DSL and Setup Up Stages
require 'capistrano/setup'

# Includes default deployment tasks
require 'capistrano/deploy'

require 'capistrano/rails'

require 'capistrano/rvm'
# require 'capistrano/rbenv'
# require 'capistrano/chruby'
require 'capistrano/bundler'
# require 'capistrano/rails/assets'
# require 'capistrano/rails/migrations'

# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }

deploy.rb:

# config valid only for Capistrano 3.1
lock '3.1.0'

set :application, 'website'
set :repo_url, 'git@bitbucket.org:MyUserName/website.git'
set :user, 'vps_user'

set :tmp_dir, '/home/vps_user/tmp'

# Default value for keep_releases is 5
set :keep_releases, 3

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

# Common directories (usually assets)
set :linked_dirs, %w{ public/assets/emails public/assets/events public/assets/photographs public/assets/updates public/assets/video public/assets/wines }

namespace :deploy do

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      # Your restart mechanism here, for example:
      execute :touch, release_path.join('tmp/restart.txt')
    end
  end

  after :publishing, :restart

  after :restart, :clear_cache do
    on roles(:web), in: :groups, limit: 3, wait: 10 do
      # Here we can do anything such as:
      # within release_path do
      #   execute :rake, 'cache:clear'
      # end
    end
  end

end

deploy/development.rb

set :branch, :develop
set :stage, :development
set :rails_env, 'development'
set :deploy_to, '/home/vps_user/rails_deployments/dev.www'
server 'localhost', user: 'vps_user', roles: %w{web app}

deploy/production.rb

set :branch, :master
set :stage, :production
set :rails_env, 'production'
set :deploy_to, '/home/vps_user/rails_deployments/www'
server 'localhost', user: 'vps_user', roles: %w{web app}

And I run the deploy command as such: bundle exec cap development deploy

Was it helpful?

Solution

OK so looking at Capistrano::Bundler under 'Usage' I saw an option in there called :bundle_without and it looked promising. So I put set :bundle_without, 'production' inside of my development.rb deploy script and it worked!

This doesn't solve the issue that Capistrano keeps trying to generate precompiled assets but I'm sure there's a solution out there like overriding the rake task or something.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top