29

我正在使用 capistrano、capistrano/rbenv、capistrano/bundler 和 capistrano/rails。我在 capistrano 编译资产的步骤中收到此错误:

DEBUG [49a50df6]    /usr/bin/env:
DEBUG [49a50df6]    ruby
DEBUG [49a50df6]    : No such file or directory
DEBUG [49a50df6]

在生产服务器/usr/bin/env ruby -v上是正确的。我知道这一点:why-does-something-work-in-my-ssh-session-but-not-in-capistrano 但我无法让它工作。

这是我的Capfile

# Load DSL and Setup Up Stages
require 'capistrano/setup'

# Includes default deployment tasks
require 'capistrano/deploy'

# Includes tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
#   https://github.com/capistrano/rvm
#   https://github.com/capistrano/rbenv
#   https://github.com/capistrano/chruby
#   https://github.com/capistrano/bundler
#   https://github.com/capistrano/rails/tree/master/assets
#   https://github.com/capistrano/rails/tree/master/migrations
#
# require 'capistrano/rvm'
require 'capistrano/rbenv'
# require 'capistrano/chruby'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'

# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }

这是我的deploy.rb

# rbenv
set :rbenv_type, :user
set :rbenv_ruby, '2.0.0-p247'

# bundler
set :bundle_gemfile, -> { release_path.join('Gemfile') }
set :bundle_dir, -> { shared_path.join('bundle') }
set :bundle_flags, '--deployment --quiet'
set :bundle_without, %w{development test}.join(' ')
set :bundle_binstubs, -> { shared_path.join('bin') }
set :bundle_roles, :all

# rails
set :rails_env, 'production'

set :application, 'MY APP'
set :repo_url, 'MY_REPO.git'

# ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }

# set :deploy_to, '/var/www/my_app'
# set :scm, :git

# set :format, :pretty
# set :log_level, :debug
# set :pty, true

set :linked_files, %w{.env config/database.yml}
# set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}

# set :default_env, { path: "/opt/ruby/bin:$PATH" }
set :keep_releases, 5

namespace :deploy do

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      # Your restart mechanism here, for example:
      # execute :touch, release_path.join('tmp/restart.txt')
    end
  end

  after :restart, :clear_cache do
    on roles(:web), in: :groups, limit: 3, wait: 10 do
      # Here we can do anything such as:
      # within release_path do
      #   execute :rake, 'cache:clear'
      # end
    end
  end

  after :finishing, 'deploy:cleanup'

end

这是我的staging.rb

set :stage, :staging

# Simple Role Syntax
# ==================
# Supports bulk-adding hosts to roles, the primary
# server in each group is considered to be the first
# unless any hosts have the primary property set.
#role :app, %w{deploy@example.com}
#role :web, %w{deploy@example.com}
#role :db,  %w{deploy@example.com}

# Extended Server Syntax
# ======================
# This can be used to drop a more detailed server
# definition into the server list. The second argument
# something that quacks like a has can be used to set
# extended properties on the server.
#server 'example.com', user: 'deploy', roles: %w{web app}, my_property: :my_value
server 'my_server', user: 'my_user', roles: %w{web app db}

# you can set custom ssh options
# it's possible to pass any option but you need to keep in mind that net/ssh understand limited list of options
# you can see them in [net/ssh documentation](http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start)
# set it globally
#  set :ssh_options, {
#    keys: %w(/home/rlisowski/.ssh/id_rsa),
#    forward_agent: false,
#    auth_methods: %w(password)
#  }
# and/or per server
# server 'example.com',
#   user: 'user_name',
# roles: %w{web app},
#   ssh_options: {
#     user: 'user_name', # overrides user setting above
#     keys: %w(/home/user_name/.ssh/id_rsa),
#     forward_agent: false,
#     auth_methods: %w(publickey password)
#     # password: 'please use keys'
#   }
# setting per server overrides global ssh_options

# fetch(:default_env).merge!(rails_env: :staging)

set :deploy_to, '/home/my_user'

这是完整的跟踪:

 INFO [c24b8f94] Running RBENV_ROOT=~/.rbenv RBENV_VERSION=2.0.0-p247 ~/.rbenv/bin/rbenv exec bundle exec rake assets:precompile on 162.243.16.201
DEBUG [c24b8f94] Command: cd /home/my_app/releases/20131101193513 && ( RAILS_ENV=production RBENV_ROOT=~/.rbenv RBENV_VERSION=2.0.0-p247 ~/.rbenv/bin/rbenv exec bundle exec rake assets:precompile )
DEBUG [c24b8f94]    /usr/bin/env:
DEBUG [c24b8f94]    ruby
DEBUG [c24b8f94]    : No such file or directory
DEBUG [c24b8f94]
cap aborted!
rake stdout: Nothing written
rake stderr: Nothing written
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sshkit-1.1.0/lib/sshkit/command.rb:94:in `exit_status='
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sshkit-1.1.0/lib/sshkit/backends/netssh.rb:125:in `block (4 levels) in _execute'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/channel.rb:551:in `call'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/channel.rb:551:in `do_request'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:561:in `channel_request'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:465:in `dispatch_incoming_packets'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:221:in `preprocess'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:205:in `process'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:169:in `block in loop'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/channel.rb:269:in `wait'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sshkit-1.1.0/lib/sshkit/backends/netssh.rb:147:in `block (2 levels) in _execute'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/channel.rb:514:in `call'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/channel.rb:514:in `do_open_confirmation'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:545:in `channel_open_confirmation'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:465:in `dispatch_incoming_packets'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:221:in `preprocess'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:205:in `process'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:169:in `block in loop'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/net-ssh-2.7.0/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sshkit-1.1.0/lib/sshkit/backends/netssh.rb:149:in `block in _execute'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sshkit-1.1.0/lib/sshkit/backends/netssh.rb:106:in `tap'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sshkit-1.1.0/lib/sshkit/backends/netssh.rb:106:in `_execute'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sshkit-1.1.0/lib/sshkit/backends/netssh.rb:54:in `execute'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/capistrano-rails-1.0.0/lib/capistrano/tasks/assets.rake:61:in `block (6 levels) in <top (required)>'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sshkit-1.1.0/lib/sshkit/backends/abstract.rb:89:in `with'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/capistrano-rails-1.0.0/lib/capistrano/tasks/assets.rake:60:in `block (5 levels) in <top (required)>'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sshkit-1.1.0/lib/sshkit/backends/abstract.rb:81:in `within'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/capistrano-rails-1.0.0/lib/capistrano/tasks/assets.rake:59:in `block (4 levels) in <top (required)>'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sshkit-1.1.0/lib/sshkit/backends/netssh.rb:42:in `instance_exec'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sshkit-1.1.0/lib/sshkit/backends/netssh.rb:42:in `run'
/Users/patricio/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/sshkit-1.1.0/lib/sshkit/runners/parallel.rb:12:in `block (2 levels) in execute'
Tasks: TOP => deploy:assets:precompile

顺便说一句,我认为它与 无关$PATH,因为bundle install运行得很好。

有谁知道发生了什么?

谢谢!

4

9 回答 9

51

好的,因为我遇到了同样的问题,我花了很长时间才解决它:

您可能想知道为什么当您使用 ssh 使用 shell 登录时,此命令无法正常工作。

我不是专家(所以当我错了时请纠正我):问题是使用 ssh 在远程服务器上登录有几个不同之处。引用 capistrano 的文档:

这实际上是 Capistrano 使用哪种 shell 的问题,它是关于登录、非登录、交互或非交互的可能性矩阵。

他们在此处(底部)有一个精美的图形,其中说明了在登录过程中执行的文件的路径。-如果您想继续阅读该主题,给定的来源也很有趣。

出了什么问题?

当您使用 ssh 登录时,您(在登录时)正在执行多个文件(例如,当您使用 bash 时是 .bash_profile)。通常,您的 $PATH 变量会扩展为您的环境中重要功能的多个路径。但是不同的登录“样式”包括不同的文件。

正如文档再次所说:

默认情况下,Capistrano 始终分配一个非登录、非交互式 shell。

在我的情况下,这会导致我的登录文件 .bash_profile 不包括在内,但它包括我的 rvm 的设置。capistrano 登录会话不知道我的 rvm 路径的结果。

我怎样才能解决这个问题?

对我来说,结果只是取消注释Capfile中的require 'capistrano/rvm' 因为我正在使用 RVM)行(位于您的 rails-project 的根目录中)。

希望能帮助到某人。

编辑:

并且肯定包含gem 'capistrano-rvm'在 Gemfile 中!

于 2014-02-24T12:17:15.067 回答
19

好的,修好了。

我添加了这个:

set :default_env, { path: "~/.rbenv/shims:~/.rbenv/bin:$PATH" }

到我的deploy.rb.

我不确定是不是最好的解决方案,但它确实有效。

于 2013-11-01T20:55:03.387 回答
18

我认为你应该执行

apt-get install git-core

在您的服务器上

于 2015-02-11T06:47:07.850 回答
9

我在使用 rbenv 时遇到了同样的问题。为我解决的问题是在 bundler 之前包含 rbenv 库——而不是在它之后。事实证明,订单很重要。这是我的 Capfile 中的内容:

require 'capistrano/rbenv'
require 'capistrano/bundler'

如果你做对了,你应该会看到使用 rbenv 前缀执行的 bundle 命令,如下所示:

XX RBENV_ROOT=$HOME/.rbenv RBENV_VERSION=2.3.0 $HOME/.rbenv/bin/rbenv exec bundle install ...
于 2016-09-12T21:43:04.840 回答
3

确保在 Capfile 中需要所有这些 gem。

于 2013-11-01T11:26:45.667 回答
2

我的问题是:

DEBUG [3132b2c2]    /usr/bin/env: ‘bundle’: No such file or directory
DEBUG [3132b2c2] 

我是这样解决的:

在 Debian 中,您的 ~/.bashrc 中有一段代码表示:

# If not running interactively, don't do anything

case $- in
   *i*) ;;
     *) return;;
esac

当你运行 Capistrano 时,它实际上会使用你的 ~/.bashrc 但它不会运行该代码块下面的任何命令。

因此,我修复了在该代码块上方添加加载 ruby​​ 环境所需的代码行的问题,因此 Capistrano 可以加载捆绑包。

source /usr/local/share/chruby/chruby.sh
source /usr/local/share/chruby/auto.sh

PS:我的服务器使用的是chruby。

于 2019-08-07T08:45:28.423 回答
1

错误的解决方法:

/usr/bin/env git 没有这样的文件或目录 capistrano

将 git 安装在远程服务器上,即要进行部署的机器。

apt-get install git
于 2015-09-11T12:50:48.083 回答
0

当我bundle install在终端上以交互方式输入时,它起作用了。现在,当我通过 Capistrano 使用它时,它不起作用。

我是这样解决的:

在 Ubuntu 中,您的代码中有一段代码~/.bashrc

# If not running interactively, don't do anything
case $- in
   *i*) ;;
     *) return;;
esac

当你运行 Capistrano 时,它实际上会使用你的~/.bashrc ,但它不会运行该代码块下面的任何命令。

bundle install 因此,我修复了移动该代码块上方所需的代码行的问题,以便 Capistrano 可以加载它们。

您可以通过运行来提示要移动哪些行which bundle

于 2018-04-27T08:46:23.270 回答
0

我遇到了同样的错误。在努力解决它之后,我记得我已经从我正在部署代码的服务器上随机删除了几个包。

所以我也意外删除了 git sudo apt-get install git。运行修复了它。

于 2019-02-06T01:21:36.200 回答