角色筛选在卡皮斯特拉诺不起作用



我正在使用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

它在这两种情况下都成功部署了存储库

但是当我跑步时

$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

最新更新