角色过滤在 Capistrano 中不起作用
role filtering not working in capistrano
我正在使用 capistrano v3
我有 2 台服务器,其中一台有两个要部署的存储库,另一台只有一个。所以我通过过滤角色
设置capistrano进行部署
我的 staging.rb 文件有这个配置
# server-based syntax
# ======================
server '172.28.128.3', user: 'vagrant', roles: %w{api}
server '172.28.128.3', user: 'vagrant', roles: %w{admin}
server '172.28.128.4', user: 'vagrant', roles: %w{apg}
# role-based syntax
# ==================
role :api, %w{vagrant@172.28.128.3}
role :admin, %w{vagrant@172.28.128.3}
role :apg, %w{vagrant@172.28.128.4}
# Custom SSH Options
# ==================
# You may pass any option but keep in mind that net/ssh understands a
# limited set of options, consult the Net::SSH documentation.
# http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start
#
# Global options
# --------------
set :ssh_options, {
keys: %w(/home/anyname/.ssh/id_rsa),
forward_agent: true,
auth_methods: %w(publickey),
user: 'vagrant'
}
我在deploy.rb
中添加了这个任务
# config valid only for current version of Capistrano
lock '3.4.0'
set :branch, 'master'
# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
# Default deploy_to directory is /var/www/my_app_name
#set :deploy_to, '/var/www/{fetch(:application)}'
#ser :repo_path, ':Q'
# Default value for :scm is :git
set :scm, :git
# Default value for :format is :pretty
set :format, :pretty
# Default value for :log_level is :debug
# set :log_level, :debug
# Default value for :pty is false
set :pty, true
# Default value for :linked_files is []
#set :linked_files, fetch(:linked_files, []).push('composer.lock')
# Default value for linked_dirs is []
#set :linked_dirs, fetch(:linked_dirs, []).push('vendor')
# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }
# Default value for keep_releases is 5
set :keep_releases, 5
namespace :deploy do
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
desc "select git url"
task :select_repo do
on roles(:all) do |host|
if host.roles.include?(:apg)
set :repo_url, 'git@bitbucket.org:something/repo1.git'
set :application, 'webartists'
elsif host.roles.include?(:api)
set :repo_url, 'git@bitbucket.org:something/repo2.git'
set :application, 'webapi'
set :linked_files, fetch(:linked_files, []).push('composer.lock')
set :linked_dirs, fetch(:linked_dirs, []).push('vendor')
elsif host.roles.include?(:admin)
set :repo_url, 'git@bitbucket.org:something/repo3.git'
set :application, 'webadmin'
end
end
end
before :deploy, "deploy:select_repo"
end
当我运行
$cap --roles=api staging deploy
$cap --roles=apg staging deploy
它在两种情况下都成功部署了 repo
但是当我运行
$cap --roles=admin staging deploy
它开始 运行 api 角色并开始部署它而不是管理员。
我认为为每个 repo_url/application
组合创建一个单独的阶段会更好。 Capistrano 的设计假定它们只有一个值。角色过滤并不意味着按照您尝试的方式工作。
也就是说,创建三个阶段:
deploy/staging_webartists.rb
deploy/staging_webapi.rb
deploy/staging_webadmin.rb
并在每个阶段定义合适的:repo_url
和:application
。例如:
# deploy/staging_webartists.rb
set :repo_url, 'git@bitbucket.org:something/repo1.git'
set :application, 'webartists'
然后不进行角色过滤,而是通过指定所需的阶段进行部署:
cap staging_webartists deploy
我正在使用 capistrano v3
我有 2 台服务器,其中一台有两个要部署的存储库,另一台只有一个。所以我通过过滤角色
设置capistrano进行部署我的 staging.rb 文件有这个配置
# server-based syntax
# ======================
server '172.28.128.3', user: 'vagrant', roles: %w{api}
server '172.28.128.3', user: 'vagrant', roles: %w{admin}
server '172.28.128.4', user: 'vagrant', roles: %w{apg}
# role-based syntax
# ==================
role :api, %w{vagrant@172.28.128.3}
role :admin, %w{vagrant@172.28.128.3}
role :apg, %w{vagrant@172.28.128.4}
# Custom SSH Options
# ==================
# You may pass any option but keep in mind that net/ssh understands a
# limited set of options, consult the Net::SSH documentation.
# http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start
#
# Global options
# --------------
set :ssh_options, {
keys: %w(/home/anyname/.ssh/id_rsa),
forward_agent: true,
auth_methods: %w(publickey),
user: 'vagrant'
}
我在deploy.rb
中添加了这个任务# config valid only for current version of Capistrano
lock '3.4.0'
set :branch, 'master'
# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
# Default deploy_to directory is /var/www/my_app_name
#set :deploy_to, '/var/www/{fetch(:application)}'
#ser :repo_path, ':Q'
# Default value for :scm is :git
set :scm, :git
# Default value for :format is :pretty
set :format, :pretty
# Default value for :log_level is :debug
# set :log_level, :debug
# Default value for :pty is false
set :pty, true
# Default value for :linked_files is []
#set :linked_files, fetch(:linked_files, []).push('composer.lock')
# Default value for linked_dirs is []
#set :linked_dirs, fetch(:linked_dirs, []).push('vendor')
# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }
# Default value for keep_releases is 5
set :keep_releases, 5
namespace :deploy do
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
desc "select git url"
task :select_repo do
on roles(:all) do |host|
if host.roles.include?(:apg)
set :repo_url, 'git@bitbucket.org:something/repo1.git'
set :application, 'webartists'
elsif host.roles.include?(:api)
set :repo_url, 'git@bitbucket.org:something/repo2.git'
set :application, 'webapi'
set :linked_files, fetch(:linked_files, []).push('composer.lock')
set :linked_dirs, fetch(:linked_dirs, []).push('vendor')
elsif host.roles.include?(:admin)
set :repo_url, 'git@bitbucket.org:something/repo3.git'
set :application, 'webadmin'
end
end
end
before :deploy, "deploy:select_repo"
end
当我运行
$cap --roles=api staging deploy
$cap --roles=apg staging deploy
它在两种情况下都成功部署了 repo
但是当我运行
$cap --roles=admin staging deploy
它开始 运行 api 角色并开始部署它而不是管理员。
我认为为每个 repo_url/application
组合创建一个单独的阶段会更好。 Capistrano 的设计假定它们只有一个值。角色过滤并不意味着按照您尝试的方式工作。
也就是说,创建三个阶段:
deploy/staging_webartists.rb
deploy/staging_webapi.rb
deploy/staging_webadmin.rb
并在每个阶段定义合适的:repo_url
和:application
。例如:
# deploy/staging_webartists.rb
set :repo_url, 'git@bitbucket.org:something/repo1.git'
set :application, 'webartists'
然后不进行角色过滤,而是通过指定所需的阶段进行部署:
cap staging_webartists deploy