Rails 使用和不使用 rbenv 的 Capistrano 部署
Rails Capistrano deploy with and without rbenv
生产中的旧 rails 项目没有 rbenv,使用 ruby 手动安装。我用 rbenv 为它设置了一个阶段,添加了 capistrano-rbenv gem 和 capistrano 阶段部署配置参数。现在即使在部署到生产环境时它也会继续搜索 rbenv 文件夹,当然会失败。
有没有一种方法可以配置 capistrano 在部署到生产环境时不使用 rbenv?
这是我当前的配置文件:
deploy.rb
# config valid only for Capistrano 3.1
lock '3.4.1'
# Git repo URL
set :repo_url, '...'
# Default value for :pty is false
set :pty, true
# Don't use sudo
set :use_sudo, false
# Default value for :linked_files is []
set :linked_files, %w{config/database.yml config/secrets.yml}
# Default value for linked_dirs is []
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}
# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }
namespace :deploy do
...
end
deploy/staging.rb
# Application name
set :application, '...'
# Use a specific tmp dir
set :tmp_dir, "/home/user_name/apps/#{fetch(:application)}/tmp"
# Deploy to this location
set :deploy_to, "/home/user_name/apps/#{fetch(:application)}"
# Branch to deploy from
set :branch, 'staging'
# rbenv stuff
set :rbenv_type, :user
set :rbenv_ruby, '2.2.5'
set :rbenv_prefix, "RBENV_ROOT=#{fetch(:rbenv_path)} RBENV_VERSION=#{fetch(:rbenv_ruby)} #{fetch(:rbenv_path)}/bin/rbenv exec"
set :rbenv_map_bins, %w{rake gem bundle ruby rails}
set :rbenv_roles, :all # default value
# General
server '...', user: 'user_name', roles: %w{app web db}
deploy/production.rb
# Application name
set :application, 'production'
# Use a specific tmp dir
set :tmp_dir, "/home/user_name/apps/#{fetch(:application)}/tmp"
# Deploy to this location
set :deploy_to, "/home/user_name/apps/#{fetch(:application)}"
# Branch to deploy from
set :branch, 'master'
# General
server '...', user: 'user_name', roles: %w{app web db}
制作ruby
production@server_name:~$ ruby -v
ruby 2.2.10p489 (2018-03-28 revision 63023) [x86_64-linux]
production@server_name:~$ which ruby
/usr/bin/ruby
为避免 gem 在 production
环境中加载,
尝试将 capistrano-rbenv
移动到 Gemfile 中的 staging
组(如果您还没有)。
此外,您必须在 Capfile 中有一个条目
require 'capistrano/rbenv'
您需要使其成为有条件的,这样您就不会最终在生产环境中需要它
有个老问题解决了类似的问题,请看一下
https://github.com/capistrano/rbenv/issues/31
在 Capfile 中添加这个
if Rake.application.top_level_tasks.first == "staging"
require 'capistrano/rbenv'
end
生产中的旧 rails 项目没有 rbenv,使用 ruby 手动安装。我用 rbenv 为它设置了一个阶段,添加了 capistrano-rbenv gem 和 capistrano 阶段部署配置参数。现在即使在部署到生产环境时它也会继续搜索 rbenv 文件夹,当然会失败。
有没有一种方法可以配置 capistrano 在部署到生产环境时不使用 rbenv?
这是我当前的配置文件:
deploy.rb
# config valid only for Capistrano 3.1
lock '3.4.1'
# Git repo URL
set :repo_url, '...'
# Default value for :pty is false
set :pty, true
# Don't use sudo
set :use_sudo, false
# Default value for :linked_files is []
set :linked_files, %w{config/database.yml config/secrets.yml}
# Default value for linked_dirs is []
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}
# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }
namespace :deploy do
...
end
deploy/staging.rb
# Application name
set :application, '...'
# Use a specific tmp dir
set :tmp_dir, "/home/user_name/apps/#{fetch(:application)}/tmp"
# Deploy to this location
set :deploy_to, "/home/user_name/apps/#{fetch(:application)}"
# Branch to deploy from
set :branch, 'staging'
# rbenv stuff
set :rbenv_type, :user
set :rbenv_ruby, '2.2.5'
set :rbenv_prefix, "RBENV_ROOT=#{fetch(:rbenv_path)} RBENV_VERSION=#{fetch(:rbenv_ruby)} #{fetch(:rbenv_path)}/bin/rbenv exec"
set :rbenv_map_bins, %w{rake gem bundle ruby rails}
set :rbenv_roles, :all # default value
# General
server '...', user: 'user_name', roles: %w{app web db}
deploy/production.rb
# Application name
set :application, 'production'
# Use a specific tmp dir
set :tmp_dir, "/home/user_name/apps/#{fetch(:application)}/tmp"
# Deploy to this location
set :deploy_to, "/home/user_name/apps/#{fetch(:application)}"
# Branch to deploy from
set :branch, 'master'
# General
server '...', user: 'user_name', roles: %w{app web db}
制作ruby
production@server_name:~$ ruby -v
ruby 2.2.10p489 (2018-03-28 revision 63023) [x86_64-linux]
production@server_name:~$ which ruby
/usr/bin/ruby
为避免 gem 在 production
环境中加载,
尝试将 capistrano-rbenv
移动到 Gemfile 中的 staging
组(如果您还没有)。
此外,您必须在 Capfile 中有一个条目
require 'capistrano/rbenv'
您需要使其成为有条件的,这样您就不会最终在生产环境中需要它
有个老问题解决了类似的问题,请看一下
https://github.com/capistrano/rbenv/issues/31
在 Capfile 中添加这个
if Rake.application.top_level_tasks.first == "staging"
require 'capistrano/rbenv'
end