捆绑安装不适用于 capistrano

Posted

技术标签:

【中文标题】捆绑安装不适用于 capistrano【英文标题】:bundle install doesn't work from capistrano 【发布时间】:2013-10-22 04:28:21 【问题描述】:

我想通过capistrano 3.0 部署我的简单rails 4.0 应用程序。

我使用 bundler 1.3.5,所以我添加了 capistrano-bundler gem 以将捆绑器与 capistrano 集成。

我的配置非常简单(几乎是默认配置):

set :bundle_gemfile, ->  release_path.join('Gemfile') 
set :bundle_dir, ->  shared_path.join('bundle') 
set :bundle_flags, ''
set :bundle_without, %wtest development.join(' ')
set :bundle_binstubs, ->  shared_path.join('bin') 
set :bundle_roles, :all

当我运行 cap staging deploy --trace 时它失败了:

[50b524bc] Running /usr/bin/env bundle --gemfile /home/webmaster/www/api/releases/20131014144650/Gemfile --path /home/webmaster/www/api/shared/bundle  --binstubs /home/webmaster/www/api/shared/bin --without test development on 125.51.3.1
DEBUG [50b144bc] Command: cd /home/webmaster/www/api/releases/20131014144650 && /usr/bin/env bundle --gemfile /home/webmaster/www/api/releases/20131014144650/Gemfile --path /home/webmaster/www/api/shared/bundle  --binstubs /home/webmaster/www/api/shared/bin --without test development
cap aborted!
bundle stdout: Nothing written
bundle stderr: Nothing written
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/sshkit-1.0.0/lib/sshkit/command.rb:94:in `exit_status='
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/sshkit-1.0.0/lib/sshkit/backends/netssh.rb:125:in `block (4 levels) in _execute'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh/connection/channel.rb:551:in `call'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh/connection/channel.rb:551:in `do_request'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:561:in `channel_request'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:465:in `dispatch_incoming_packets'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:221:in `preprocess'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:205:in `process'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:169:in `block in loop'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:169:in `loop'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:169:in `loop'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh/connection/channel.rb:269:in `wait'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/sshkit-1.0.0/lib/sshkit/backends/netssh.rb:147:in `block (2 levels) in _execute'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh/connection/channel.rb:514:in `call'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh/connection/channel.rb:514:in `do_open_confirmation'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:545:in `channel_open_confirmation'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:465:in `dispatch_incoming_packets'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:221:in `preprocess'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:205:in `process'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:169:in `block in loop'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:169:in `loop'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:169:in `loop'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/sshkit-1.0.0/lib/sshkit/backends/netssh.rb:149:in `block in _execute'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/sshkit-1.0.0/lib/sshkit/backends/netssh.rb:106:in `tap'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/sshkit-1.0.0/lib/sshkit/backends/netssh.rb:106:in `_execute'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/sshkit-1.0.0/lib/sshkit/backends/netssh.rb:54:in `execute'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/capistrano-bundler-1.0.0/lib/capistrano/tasks/bundler.cap:20:in `block (4 levels) in <top (required)>'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/sshkit-1.0.0/lib/sshkit/backends/abstract.rb:81:in `within'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/capistrano-bundler-1.0.0/lib/capistrano/tasks/bundler.cap:19:in `block (3 levels) in <top (required)>'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/sshkit-1.0.0/lib/sshkit/backends/netssh.rb:42:in `instance_exec'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/sshkit-1.0.0/lib/sshkit/backends/netssh.rb:42:in `run'
/home/omnomnom/.rvm/gems/ruby-2.0.0-p247/gems/sshkit-1.0.0/lib/sshkit/runners/parallel.rb:12:in `block (2 levels) in execute'
Tasks: TOP => deploy:updated => bundler:install

但是如果我复制此命令并运行 ssh 一切正常(所有 gem 安装成功)我该如何修复/调试它?

【问题讨论】:

Rubygems 今天有问题。请稍后再试... @spickermann 但如果我通过 ssh 运行此命令,gems 下载效果很好...... 发布您的 capfile 和 deploy.rb,可能存在一些配置问题。另外,请确保您的部署用户对 gem 的目录具有写入权限 【参考方案1】:
gem install bundler 

为了 Capistrano,我需要在我的部署用户上运行:

/usr/local/rvm/bin/rvm 2.4.0 do bundle install --path /var/www/hivebench-api/shared/bundle --without development test --deployment --quiet

通过!

【讨论】:

【参考方案2】:

在您的 config/deploy/production.rb 文件中放置

set :rvm_ruby_version, '2.0.0-p247'  

见https://github.com/capistrano/rvm

【讨论】:

【参考方案3】:

简单的解决方案,适用于所有 Capistrano 3 版本:

gem 'rvm1-capistrano3', require: false

在 Capfile 中添加

require 'rvm1/capistrano3'

https://github.com/rvm/rvm1-capistrano3

【讨论】:

【参考方案4】:

我有:

set :bundle_flags, '--system --quiet'

改为:

set :bundle_flags, '--deployment --quiet'

这解决了一个问题。

【讨论】:

【参考方案5】:

这适用于 capistrano 3:

  task :bundle_list do
    on roles(:app) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :bundle, "list"
        end
      end
    end
  end

将此用于 rake 任务:

execute :rake, 'some_rake_task'

【讨论】:

【参考方案6】:

就我而言,我将 config/deploy.rb 的 set :log_level, :info 更改为 set :log_level, :debug,这显示“找不到 GEMFILE”。这表明 bundle 使用错误的工作目录运行,所以我更改了

before "deploy:assets:precompile", "deploy:bundle_install"
desc "Bundle install for RVMs sake"
task :bundle_install do
  on roles(:app) do
    execute "/u0/jrepenni/.rvm/bin/rvm 2.1.0@akiary do /u0/jrepenni/.rvm/gems/ruby-2.1.0@global/bin/bundle install"
  end
end

before "deploy:assets:precompile", "deploy:bundle_install"
desc "Bundle install for RVMs sake"
task :bundle_install do
  on roles(:app) do
    execute "cd #release_path && /u0/jrepenni/.rvm/bin/rvm 2.1.0@akiary do /u0/jrepenni/.rvm/gems/ruby-2.1.0@global/bin/bundle install"
  end
end

(注意添加的“cd”)

【讨论】:

【参考方案7】:

该错误听起来像是在您的PATH 中找不到bundle。当您手动 SSH 时,它可能会在您的 ~/.profile~/.bash_profile 中运行某些内容,将其添加到您的路径中。

通过登录并运行which bundle 找到bundle 的路径。然后,尝试查找该路径是如何添加到您的 PATH 环境变量中的。如果你的 ~/.bash_profile, try moving it to~/.bashrc` 中有东西。

您也可以尝试Command Mapping 指定准确路径。

http://www.capistranorb.com/documentation/faq/why-does-something-work-in-my-ssh-session-but-not-in-capistrano/ 有更多故障排除提示

【讨论】:

【参考方案8】:

刚刚遇到同样的问题。对我有用的是:

1) 安装capistrano-rvm gem 并添加

require 'capistrano/rvm'

Capfile

2) 将我的部署用户添加到服务器上的rvm 组:

# usermod deploy -a -G rvm

3) 在我的部署用户的主文件夹中创建两个目录:.rvm.rvm/bin

4) 将此行添加到我的deploy.rb 文件中:

set :default_env,  rvm_bin_path: '~/.rvm/bin' 

呸!这花了几个小时。

【讨论】:

找到问题的根源了吗? 不是真的(时间紧迫,不得不让它快速运行)。我怀疑这与 Capistrano 登录时未加载 rvm 设置有关(与常规 shell 登录相反,请参阅 here)。我开始研究它,但后来决定用 rvm 包装器走这条路,它奏效了。 它对我有用,只有一个更正:我必须更改以将 require 'capistrano/rvm' 更改为 require 'rvm/capistrano' 并且一切正常 啊!我的印象是 default_environment 被删除了!它只是被重命名。好的!谢谢。【参考方案9】:

要调试它,请尝试删除 --quiet 标志:

set :bundle_flags, '--deployment'

您使用的是 rbenv、rvm 还是类似的东西?当bundle 运行时,Ruby 版本可能尚未设置。通过删除--quite 标志,您可能会获得一些调试信息。

【讨论】:

如您所见,该命令中没有--quiet 标志,因为我已经将其删除了。如果已经设置了 ruby​​,我使用 rvm 如何调试?

以上是关于捆绑安装不适用于 capistrano的主要内容,如果未能解决你的问题,请参考以下文章

XDebug 不适用于 xampp

Magento - 以编程方式添加的捆绑产品不适用于购物篮/前端

Azure 中的捆绑不适用于 bootstrap 和 jquery、ASP.NET MVC 5

gem install mysql2 v '0.3.11' 不适用于优胜美地

源地图不适用于 Webpack

iOS:Firebase 通知不适用于用户段