在rake任务中运行时,bundle包失败

Hor*_*cio 7 ruby bundle rake-task

我的环境

香草Ubuntu 12.10,没有rvm或renv.

> gem --version
1.8.23

> ruby --version
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]

> bundle --version
Bundler version 1.2.1
Run Code Online (Sandbox Code Playgroud)

我的问题

我有一个rake任务来打包我的宝石并将它们上传到我的开发和生产服务器.问题是当Gemfile包含git或path gems时,rake任务失败.Bundler已经支持这些类型的宝石的包装,它在我的终端上工作得很好但是在运行rake任务时失败了,我找不到原因.

我的佣金任务

> cat lib/tasks/upload.rake

namespace :deploy do
  desc "Package all gems and upload to remote server"
  task :upload => [:environment] do |t, args|
    if ! system("bundle package --all")
      raise "TOTAL FAIL"
    end

    # Magic method to upload vendor/cache to remote server
  end
end
Run Code Online (Sandbox Code Playgroud)

我的尝试

在终端中运行捆绑包工作:

> bundle package --all
....
Using bson (1.7.0) 
Using bson_ext (1.7.0) 
Using cancan (1.6.8) from git://github.com/ryanb/cancan.git (at /home/ryujin/Projects/rails/Encluster4/vendor/cache/cancan-4dcd54459482) 
Using carrierwave (0.7.0) 
Using coffee-script-source (1.4.0) 
....
Updating files in vendor/cache
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
Updating files in vendor/cache
Run Code Online (Sandbox Code Playgroud)

使用irb 运行捆绑包也可以:

> irb
irb> system("bundle package --all")
...
Using ansi (1.4.3) 
Using bson (1.7.0) 
Using bson_ext (1.7.0) 
Using cancan (1.6.8) from git://github.com/ryanb/cancan.git (at /home/ryujin/Projects/rails/Encluster4/vendor/cache/cancan-4dcd54459482) 
Using carrierwave (0.7.0) 
Using coffee-script-source (1.4.0) 
...
Updating files in vendor/cache
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
Updating files in vendor/cache
=> true
Run Code Online (Sandbox Code Playgroud)

但是在我的简单rake任务中执行时,bundle package不起作用:

> bundle exec rake deploy:upload
Updating files in vendor/cache
Could not find cancan-1.6.8.gem for installation
rake aborted!
TOTAL FAIL

Tasks: TOP => deploy:upload
(See full trace by running task with --trace)
Run Code Online (Sandbox Code Playgroud)

我发现没有理由说这可能会失败.我一直在同一个环境中运行.我已经检查了捆绑exec文件在所有三种情况下是相同的(/ usr/local/bin/bundle).我没有痕迹或rvm或renv或类似的东西.还试过没有bundle exec运行任务和同样的问题.

提前感谢任何关于为什么会发生这种情况的提示.

小智 6

我遇到了同样的麻烦.我检查了环境变量,发现捆绑器修改了RUBYOPT:

$ bundle exec env > benv.txt
$ env > env.txt
$ diff -u env.txt benv.txt
--- env.txt 2012-12-05 17:13:10.000000000 +0400
+++ benv.txt  2012-12-05 17:13:07.000000000 +0400
@@ -72,4 +72,8 @@
 CDPATH=.
 install_flag=1
 rvm_hook=
-_=/usr/bin/env
+_=/Users/denis/.rvm/gems/ruby-1.9.3-p327@global/bin/bundle
+_ORIGINAL_GEM_PATH=/Users/denis/.rvm/gems/ruby-1.9.3-p327:/Users/denis/.rvm/gems/ruby-1.9.3-p327@global
+BUNDLE_BIN_PATH=/Users/denis/.rvm/gems/ruby-1.9.3-p327@global/gems/bundler-1.2.3/bin/bundle
+BUNDLE_GEMFILE=/Users/denis/src/my-project/Gemfile
+RUBYOPT=-I/Users/denis/.rvm/gems/ruby-1.9.3-p327@global/gems/bundler-1.2.3/lib -rbundler/setup
Run Code Online (Sandbox Code Playgroud)

所以,解决方法我这样做:

system %Q(/usr/bin/env RUBYOPT= bundle package)
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!


Pan*_*dan 6

人们也可以选择使用

Bundler.with_clean_env do
   sh "bundle update --source .."
end
Run Code Online (Sandbox Code Playgroud)

确实这是丹尼斯提到的RUBYOPT问题