LoginSignup
10
6

More than 5 years have passed since last update.

bundle exec xxxのスクリプト中でbundle installする

Posted at

capistranoを使って、ローカルにrailsアプリをclone/assetes precompileまでやってからrsyncでデプロイしようとしたらハマった。
capistranoに限らずbundle exec scriptして、その中でbundle installすると発生するようなのでメモしておく。

現象

bundle exec scriptした中でbundle installすると、scriptを実行した時点の設定が引き継がれていて、bundle installが期待通りに走らない。

sctiptを実行するディレクトリのGemfileがこうなっていて

$ cat Gemfile
# A sample Gemfile
source "https://rubygems.org"

gem 'capistrano'
gem 'capistrano_colors'
gem 'capistrano-bundler'
gem 'capistrano-rails'

以下のようなテストコードを走らせてみると

test.rb
Dir.chdir("tmp/deploy") do
  puts "--- current dir ----"
  system "pwd"
  puts "--- current bundle config ---"
  system "bundle config"
  puts "--- bundle install ---"
  system "bundle install --path=vendor/bundle --without development test"
end

bundle execしたところの情報が引き継がれていて、bundle installが期待通りに走らない

$ bundle exec ruby test.rb
--- current dir ----
/home/paty/work/git/capistrano3/tmp/deploy
--- current bundle config ---
Settings are listed in order of priority. The top value will be used.

path
Set for your local app (/home/paty/work/git/capistrano3/.bundle/config): "vendor/bundle"

gemfile
Set via BUNDLE_GEMFILE: "/home/paty/work/git/capistrano3/Gemfile"
...

--- bundle install ---
Using rake (10.3.2)
Using json (1.8.1)
...
(tmp/deployのGemfileは参照されない)

対応方法

こちらのサイトを参考にさせていただいた http://blog.kyanny.me/entry/2012/04/25/074256
どうやらbundlerの環境変数を引き継いだままになっているのが問題とのこと

今回はBundler.with_clean_envを使うことで期待通りの挙動にできた。

test.rb
Dir.chdir("tmp/deploy") do
  Bundler.with_clean_env do
    ...
  end
end
10
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
6