使用Capistrano 3设置默认阶段 [英] Set default stage with Capistrano 3
问题描述
是否可以在Capistrano 3中设置默认阶段?
Is there a way to set a default stage in Capistrano 3?
我尝试将 set:stage,:production
在deploy.rb中,但是没有用,它给出了错误:
I've tried putting set :stage, :production
inside deploy.rb but that didn't work, it gives the error:
Stage not set, please call something such as `cap production deploy`,
where production is a stage you have defined
我现在只有一个阶段,所以我希望能够运行 cap deploy
并使其在默认状态下执行。
I only have one stage right now so I want to be able to just run cap deploy
and have it execute on the default.
推荐答案
Capistrano v3有点像Rake的包装,因此您需要意识到真正发生的是生产
任务首先运行,然后是 deploy
任务。
Capistrano v3 is somewhat of a wrapper around Rake, so you need to realize that what's really happening is that a production
task is getting run first, followed by a deploy
task.
如果稍加调试,您会发现 deploy.rb
不会加载不要在舞台上打字。这是因为阶段的任务是加载 deploy.rb
的位置:查看 lib / setup.rb
,为每个阶段定义一个任务。运行时,阶段的任务设置:stage
,加载capistrano默认值,然后最终加载 deploy.rb
文件。
If you debug it a little, you'll find that deploy.rb
doesn't get loaded when you don't type in a stage. This is because the stage's task is where deploy.rb
gets loaded: Looking at lib/setup.rb
, a task is defined for each stage. When run, the stage's task sets :stage
, loads up the capistrano defaults, and then finally loads your deploy.rb
file.
因此,一个简单的技巧是告诉Capistrano每次运行 cap
都要调用阶段任务通过将其添加到 Capfile
(不是您的 deploy.rb
)的末尾:
So, an easy trick would be to tell Capistrano to invoke the stage task every time you run cap
by adding this to the end of your Capfile
(not your deploy.rb
):
Rake::Task[:production].invoke
或使用Capistrano DSL中的调用
方法:
or, using the invoke
method from Capistrano's DSL:
invoke :production
如果您这样做,可能会产生意想不到的后果实际上确实使用了多个阶段,但是如果您只使用 production
阶段,它应该可以正常工作。
This may have some unintended consequences if you actually do use multiple stages, but if you only ever use the production
stage, it should work fine.
另一个简便的解决方案可以是简单的shell别名,例如 alias cap ='cap production'
,但如果您有多个具有不同阶段名称的项目,则可能效果不佳。
Another easy solution could be a simple shell alias, such as alias cap='cap production'
, but it might not work great if you have multiple projects with different stage names.
这篇关于使用Capistrano 3设置默认阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!