Capistrano 3拉命令行参数

时间:2022-02-10 23:19:27

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)

我正在从Capistrano 2升级到Capistrano 3.在Cap 2中我使用以下命令行参数作为分支名称(否则默认为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,它将部署master分支。但它也让我做这样的事情:

cap deploy -S branch=foo

Which would deploy the foo branch.

哪个会部署foo分支。

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

现在,在Capistrano 3中,如果我尝试运行以上操作,我会收到一个错误:无效选项:-S。

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

现在通过命令行传递参数的正确方法是什么?

3 个解决方案

#1


10  

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

Rake任务(上限正在使用)是支持参数。

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

cap -T outputs:

上限-T输出:

cap yiic:test[arg1] # Test task

cap yiic:test [arg1] #Test任务

Example of invocation:

调用示例:

cap production yiic:test[test1]

制帽yiic:测试[test1]

Also, here is a helpful post

另外,这是一篇有用的帖子

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

P.S。:你应该使用env vars进行“全局”设置。像多个任务的常见值一样。

#2


37  

What I ended up doing was setting an ENV variable.

我最终做的是设置一个ENV变量。

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)

它将部署mybranch。如果我运行一个简单的cap生产部署,它将部署默认分支(master如果你没有设置一个,但我已经改变了我的下面默认为演示)

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

这是我在deploy.rb文件中放入的代码:

set :branch, "default"
if ENV['branch']
        set :branch, ENV['branch']
end

#3


0  

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).

为Capistrano 3提供更新和有效的解决方案(因为它花了我一段时间才找到它并且有太多的测试使其工作)。

My files are like:

我的文件是这样的:

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

In staging.rb I have:

在staging.rb我有:

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)

(在服务器'staging'中,staging是一个SSH连接,在我的.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).

其中3b2d9ab是Git提交哈希(短版或长版)。

#1


10  

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

Rake任务(上限正在使用)是支持参数。

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

cap -T outputs:

上限-T输出:

cap yiic:test[arg1] # Test task

cap yiic:test [arg1] #Test任务

Example of invocation:

调用示例:

cap production yiic:test[test1]

制帽yiic:测试[test1]

Also, here is a helpful post

另外,这是一篇有用的帖子

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

P.S。:你应该使用env vars进行“全局”设置。像多个任务的常见值一样。

#2


37  

What I ended up doing was setting an ENV variable.

我最终做的是设置一个ENV变量。

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)

它将部署mybranch。如果我运行一个简单的cap生产部署,它将部署默认分支(master如果你没有设置一个,但我已经改变了我的下面默认为演示)

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

这是我在deploy.rb文件中放入的代码:

set :branch, "default"
if ENV['branch']
        set :branch, ENV['branch']
end

#3


0  

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).

为Capistrano 3提供更新和有效的解决方案(因为它花了我一段时间才找到它并且有太多的测试使其工作)。

My files are like:

我的文件是这样的:

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

In staging.rb I have:

在staging.rb我有:

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)

(在服务器'staging'中,staging是一个SSH连接,在我的.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).

其中3b2d9ab是Git提交哈希(短版或长版)。