Rails 3 – Bundler / Capistrano错误

我有一个基本的Rails 3应用程序在我的开发框本地工作,但是想要testing一下部署,以确保一切正常。 我正在使用Capistrano进行部署。

当我运行cap deploy (所有其他必要的设置之后),它打破了这个命令与此错误:

 [...] * executing 'bundle:install' * executing "bundle install --gemfile /var/www/trex/releases/20100917172521/Gemfile --path /var/www/trex/shared/bundle --deployment --quiet --without development test" servers: ["www.[my domain].com"] [www.[my domain].com] executing command ** [out :: www.[my domain].com] sh: bundle: command not found command finished [...] 

所以看起来它在服务器上找不到bundle命令。

但是,当我login到服务器…

 $ ruby -v ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux] $ rails -v Rails 3.0.0 $ bundle -v Bundler version 1.0.0 

bundle命令工作得很好。

有什么可能会出错?

(此外,为了完整:)

 $ which ruby ~/.rvm/rubies/ruby-1.9.2-p0/bin/ruby $ which rails ~/.rvm/gems/ruby-1.9.2-p0/bin/rails $ which bundle ~/.rvm/gems/ruby-1.9.2-p0/bin/bundle 

更新:

对于RVM> = 1.11.3,您现在应该只使用rvm-capistrano gem 。 对于较旧的RVM> = 1.0.1,下面的答案仍然适用。


原文答案:

好吧,虽然我没有得到全面的cap deploy上class,我确实解决了这个问题。 问题是Capistrano试图为Bundler(和其他gem)使用不同的path而不是RVMpath。

检查你的Capistranopath,通过做cap shell ,然后echo $PATH 。 你可能会看到你的标准/usr/local/bin/usr/bin ,但是这不是存储Bundler等的RVM的地方。

编辑您的Capistrano config/deploy.rb文件,并按照以下说明添加以下行:

 # Add RVM's lib directory to the load path. $:.unshift(File.expand_path('./lib', ENV['rvm_path'])) # Load RVM's capistrano plugin. require "rvm/capistrano" set :rvm_ruby_string, '1.9.2' set :rvm_type, :user # Don't use system-wide RVM 

终于让Capistrano看到Bundler,并开始适当地加载gem。

没有findBundler,因为.bash_profile没有被加载,所以你的PATH是错误的。 这可能是因为你有.bash_profile中的RVM脚本。

简单的答案是将RVM脚本从.bash_profile移动到.bashrc,Capistrano应该能够find它(也validation.bash_profile来源.bashrc)。

Capistrano使用SSH通过非交互式shell在服务器上执行命令。 这个shell会话将产生.bashrc而不是.bash_profile 。 我向两者添加了ECHO语句,并通过SSH运行LS。 您可以在下面的结果中看到只有.bashrc来源:

 $ ssh user@123.amazonaws.com ls .bashrc loaded git file1 file2 

我有一个使用rbenv相同的问题。 解决的办法是从我的.bashrc文件的底部取rbenv的特定行,并把它们放在最上面。 如果shell没有在交互模式下运行,我的.bashrc文件的第一行正在返回中止。

最后一行应该是

 set :rvm_type, :user 

也就是说,用户必须是符号而不是variables,否则就会得到

 undefined local variable or method `user' 

没有rvm/capistrano为我工作。 我发现最好的解决scheme是添加到deploy.rb文件下面的行(这是非系统范围的RVM):

set :bundle_cmd, 'source $HOME/.bash_profile && bundle'

我的理解是bundle命令没有find,因为在用户的〜/ .bash_profile中定义的PATHvariables没有被Capistrano加载。

为了解决这个问题,我创build了一个任务:bundle_gems。

 task :bundle_gems do run "cd #{deploy_to}/current && export PATH=/usr/local/pgsql/bin:/opt/ruby-enterprise-XXX/bin:$PATH && bundle install vendor/gems" end 

请注意,我还包括PostgreSQL二进制文件的path – 安装pg gem失败,因为找不到它们,即使可以find它们。

虽然这似乎是一个混乱的方法。 据推测,有一个更“全球”的地方来定义我不知道的二进制文件的path。

更新23/12

为所有用户添加一个目录到$ PATH: https : //serverfault.com/questions/102932/adding-a-directory-to-path-in-centos

但是这仍然不会被加载,因为它是一个非交互式的非loginshell。

一个build议是将path添加到/ etc / bashrc中: 如何设置$ PATH以使`ssh user @ host command`起作用?

然而,这也没有为我工作。 我相信它是因为SSH不加载/ etc / bashrc。

另一个build议是编辑〜/ .ssh / environment: http : //www.ruby-forum.com/topic/79248 。 然而,这与在deploy.rb中指定path几乎一样麻烦。

这个为我工作:

set:bundle_cmd,'source $ HOME / .bash_profile && bundle'

我尝试了一些build议。 在为RVM环境的deploy.rb文件中设置path时遇到问题。 我的最终解决scheme是包括以下内容:

在config / deploy.rb文件中添加:

 require "bundler/capistrano" 

另外在config / deploy.rb中,或者在我的情况下config / production.rb,因为我使用Capistrano的多级选项

 after "deploy", "rvm:trust_rvmrc" 

这一步简单地确保我们不再需要“你想要信任.rvmrc文件”,而是调用deploy.rb文件中的一个任务,例如:

 namespace :rvm do task :trust_rvmrc do run "rvm rvmrc trust #{release_path}" end end 

在进行这些细微的改动之后,我能够运行cap production deploy ,检查了代码; 执行资产pipe道部署,将发布文件夹链接到当前,执行的bundle install和清理。