デプロイと同時にbundle installを実行する

検証環境

Ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]
Rails 3.0.10
Bundler version 1.0.18
Capistrano v2.8.0



準備

準備といってもやるべきことはひとつだけです。BundlerにはあらかじめCapistrano用のcapタスクが用意されているので、$RAILS_ROOT/config/deploy.rbの中からrequireします。

FILE: deploy.rb

--- config/deploy.rb	2011-09-05 16:59:22.000000000 +0900
+++ config/deploy.rb	2011-09-01 12:01:46.000000000 +0900
@@ -1,4 +1,5 @@
 require 'capistrano/ext/multistage'
+require 'bundler/capistrano'
 
 set :application, 'myapp'
 set :repository,  'git+ssh://dev.tetsuyai.com/var/git/myapp.git'



実行

cap deployを実行すると、デプロイ後にbundle installが実行されます。

That's it! Running cap deploy will now automatically run bundle install on the remote server with deployment-friendly options. As list of options that can be changed is available in the help for the cap task. To see it, run cap -e bundle:install.

http://gembundler.com/deploying.html

cap -e bundle:installしてみました。

$ bundle exec cap -e bundle:install
------------------------------------------------------------
cap bundle:install
------------------------------------------------------------
Install the current Bundler environment. By default, gems will be installed to
the shared/bundle path. Gems in the development and test group will not be
installed. The install command is executed with the --deployment and --quiet
flags. If the bundle cmd cannot be found then you can override the bundle_cmd
variable to specifiy which one it should use.

You can override any of these defaults by setting the variables shown below.

N.B. bundle_roles must be defined before you require 'bundler/capistrano' in
your deploy.rb file.

  set :bundle_gemfile,  "Gemfile"
  set :bundle_dir,      File.join(fetch(:shared_path), 'bundle')
  set :bundle_flags,    "--deployment --quiet"
  set :bundle_without,  [:development, :test]
  set :bundle_cmd,      "bundle" # e.g. "/opt/ruby/bin/bundle"
  set :bundle_roles,    {:except => {:no_release => true}} # e.g. [:app, :batch]



インストール先

デフォルトでは#{shared_path}/bundleディレクトリにインストールされます。

$ ls /var/www/myapp/shared
bundle

インストール先を変更するには、deploy.rbの中で

FILE: deploy.rb

--- config/deploy.rb	2011-09-01 12:01:46.000000000 +0900
+++ config/deploy.rb	2011-09-05 17:15:19.000000000 +0900
@@ -8,6 +8,7 @@ set :scm,         :git
 set :user,        'tetsuyai'
 set :use_sudo,    false
 set :deploy_via,  :copy
+set :bundle_dir,  "#{shared_path}/lib"

のように記述します。