Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capistrano 3 pulling command line arguments

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 to master)

set :branch, fetch(:branch, "master")

If I called cap deploy it would deploy the master branch. But it also let me do something like this:

cap deploy -S branch=foo

Which would deploy the foo branch.

Now, in Capistrano 3, if I try to run the above I get an error: invalid option: -S.

What's the proper way to pass an argument via the command line now?

like image 549
Brad Dwyer Avatar asked Jan 09 '14 17:01

Brad Dwyer


People also ask

How do I get command line arguments?

Command-line arguments are given after the name of the program in command-line shell of Operating Systems. To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments.

What is the handle command line arguments?

The commandLine property of an Activity specifies what executable should be run on the Design Automation server and what arguments should be passed to it. Some of the command line parameters, such as $(appbundles[]), will be interpreted before calling the executable - see Command Lines.

What is Capistrano gem?

Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.


2 Answers

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
like image 180
Brad Dwyer Avatar answered Sep 22 '22 07:09

Brad Dwyer


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.

like image 26
senz Avatar answered Sep 22 '22 07:09

senz