无法使用 capistrano 部署 rails 应用程序,找不到包

Unable to deploy rails application using capistrano, bundle not found

我正在尝试使用 capistrano 部署我的 rails 应用程序,但它给我 "bundle stdout: /home/deploy/.rvm/scripts/set: line 19: exec: bundle: not found" 错误。我已经在服务器上安装了 bundler gem,但 capistrano 仍然无法找到 bundle。

Capfile 内容:

require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/bundler'
require 'capistrano/rvm'
require 'capistrano/rails/assets' # for asset handling add
require 'capistrano/rails/migrations' # for running migrations
require 'capistrano/puma'
require 'capistrano/rake'

install_plugin Capistrano::Puma
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

Deploy.rb 内容:

lock '3.10.0'

set :application, 'app_name'
set :repo_url, 'my-repo-url' # Edit this to match your repository
set :branch, :branch_name
set :deploy_to, '/home/deploy/app_name'
set :pty, true
set :linked_files, %w{config/database.yml config/application.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}
set :keep_releases, 3
set :rvm_type, :user
set :rvm_ruby_version, 'ruby-2.3.1' # Edit this if you are using MRI Ruby

set :puma_rackup, -> { File.join(current_path, 'config.ru') }
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/puma.sock"    #accept array for multi-bind
set :puma_conf, "#{shared_path}/puma.rb"
set :puma_access_log, "#{shared_path}/log/puma_error.log"
set :puma_error_log, "#{shared_path}/log/puma_access.log"
set :puma_role, :app
set :puma_threads, [0, 16]
set :puma_workers, 0
set :puma_worker_timeout, nil
set :puma_init_active_record, true
set :puma_preload_app, false
set :puma_prune_bundler, true

namespace :deploy do
    desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      invoke 'puma:restart'
    end
  end

  after  :finishing,    :compile_assets
  after  :finishing,    :cleanup
  after  :finishing,    :restart
end

Production.rb 内容:

server 'My-IP', user: 'deploy', roles: %w{web app db}
set :puma_env, fetch(:rack_env, fetch(:rails_env, 'production'))

当使用 capistranorvm 部署 Rails 应用程序时,您必须注意 capistrano 会切换 ruby 版本,并且可能不一样ruby 如您所见的版本。

你可以ssh到远程服务器自己查看

ssh deploy@My-IP
ruby -v        # It's probably not 2.3.1
which bundle
rvm use 2.3.1  # This is the actual version that will be used by capistrano
which bundle

如果您在 ruby-2.3.1 中看不到安装的包,您可以通过

手动安装它
rvm use 2.3.1
gem install bundler

之后,capistrano 部署应该会按预期工作。