2016-02-04 97 views
1

我正在使用capistrano v3角色過濾不工作在capistrano

我有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{[email protected]} 
role :admin, %w{[email protected]} 
role :apg, %w{[email protected]} 

# 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, '[email protected]:something/repo1.git' 
     set :application, 'webartists' 
     elsif host.roles.include?(:api) 
     set :repo_url, '[email protected]: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, '[email protected]: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作用的,並開始部署,而不是管理的。

回答

1

我想你會更好地爲每個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, '[email protected]:something/repo1.git' 
set :application, 'webartists' 

代替過濾,通過指定所期望的階段部署角色然後:

cap staging_webartists deploy