Search code examples
ruby-on-railsrubydeploymentcapistranocapistrano3

role filtering not working in capistrano


I'm using capistrano v3

I have 2 servers, one of them has two repositories to deploy and other has only one. So I set up capistrano for deployment by filtering roles

My staging.rb file has this configuration

# 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'
  }

I have added this task in 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

when I run

$cap --roles=api staging deploy

$cap --roles=apg staging deploy

it successfully deploys repo in both cases

but when i run

$cap --roles=admin staging deploy

it starts to run api role's and start to deploy that instead of admin's.


Solution

  • I think you would be better served by creating a separate stage for each repo_url/application combination. Capistrano is designed assuming these have only a single value. Roles filtering isn't meant to work the way you are attempting.

    In other words, create three stages:

    deploy/staging_webartists.rb
    deploy/staging_webapi.rb
    deploy/staging_webadmin.rb
    

    And in each stage define the :repo_url and :application that are appropriate. E.g.:

    # deploy/staging_webartists.rb
    set :repo_url, '[email protected]:something/repo1.git'
    set :application, 'webartists'
    

    Then instead of roles filtering, deploy by specifying the desired stage:

    cap staging_webartists deploy