8

我的环境

Vanilla 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

我的问题

我有一个 rake 任务来打包我的 gem 并将它们上传到我的开发和生产服务器。问题是当 Gemfile 包含 git 或路径 gem 时 rake 任务失败。Bundler 已经支持打包这些类型的 gem,它在我的终端上运行良好,但是在运行 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

我的尝试

在终端中运行bundle 包可以:

> 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

使用 irb运行bundle 包也可以:

> 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

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

> 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)

我找不到这可能失败的原因。我一直在同一个环境中运行。我已经检查了这三种情况下的 bundle exec 文件是否相同(/usr/local/bin/bundle)。我没有痕迹或 rvm 或 renv 或类似的东西。还尝试在没有 bundle exec 的情况下运行任务和同样的问题。

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

4

2 回答 2

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

因此,要解决此问题,我会这样做:

system %Q(/usr/bin/env RUBYOPT= bundle package)

希望这可以帮助!

于 2012-12-05T13:30:32.417 回答
6

也可以更喜欢使用

Bundler.with_clean_env do
   sh "bundle update --source .."
end

确实这是 Denis 提到的一个 RUBYOPT 问题

于 2014-04-01T21:23:00.103 回答