Capistrano 3 pulling command line arguments

前端 未结 3 1825
醉话见心
醉话见心 2020-12-24 08:29

I\'m in the process of upgrading from Capistrano 2 to Capistrano 3. In Cap 2 I was using the following to take a command line argument as the branch name (otherwise default

相关标签:
3条回答
  • 2020-12-24 09:07

    Rake tasks (which cap is using) are supporting arguments.

    namespace :test do
      desc "Test task"
      task :test, :arg1 do |t, args|
        arg1 = args[:arg1]
        puts arg1
      end
    end
    

    cap -T outputs:

    cap yiic:test[arg1] # Test task

    Example of invocation:

    cap production yiic:test[test1]

    Also, here is a helpful post

    P.S.: you should use env vars for "global" settings. Like common values for multiple tasks.

    0 讨论(0)
  • 2020-12-24 09:11

    To give an updated and working solution for Capistrano 3 (as it took me a while to find and too many tests to make it works).

    My files are like:

    config/
        deploy/
            staging.rb
            production.rb
        deploy.rb
    ...
    Capfile
    

    In staging.rb I have:

    server 'staging', roles: %w(db)
    
    set :branch,    ENV.fetch('REVISION', 'master')
    set :use_sudo,  false
    set :user,      'toto'
    set :deploy_to, '/var/www'
    

    (In server 'staging', staging is a SSH connexion, defined in my .ssh/config)

    Then, to deploy a specific revision, I just need to call:

    cap staging deploy REVISION=3b2d9ab
    

    Where 3b2d9ab is the Git commit hash (short or long version).

    0 讨论(0)
  • 2020-12-24 09:13

    What I ended up doing was setting an ENV variable.

    So now I can call

    cap production deploy branch=mybranch
    

    And it will deploy mybranch. If I run a simple cap production deploy it will deploy the default branch (master if you don't set one, but I've changed mine below to default to demonstrate)

    This is the code I put in my deploy.rb file:

    set :branch, "default"
    if ENV['branch']
            set :branch, ENV['branch']
    end
    
    0 讨论(0)
提交回复
热议问题