使用 capistrano 从不同的 git 分支部署
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1524204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using capistrano to deploy from different git branches
提问by Toms Mikoss
I am using capistrano to deploy a RoR application. The codebase is in a git repository, and branching is widely used in development. Capistrano uses deploy.rb
file for it's settings, one of them being the branch to deploy from.
我正在使用 capistrano 部署一个 RoR 应用程序。代码库位于 git 存储库中,分支在开发中被广泛使用。Capistrano 使用deploy.rb
文件进行设置,其中之一是部署的分支。
My problem is this: let's say I create a new branch Afrom master. The deploy file will reference masterbranch. I edit that, so Acan be deployed to test environment. I finish working on the feature, and merge branch Ainto master. Since the deploy.rb
file from Ais fresher, it gets merged in and now the deploy.rb
in masterbranch references A. Time to edit again.
我的问题是:假设我从master创建了一个新分支A。部署文件将引用master分支。我编辑它,所以可以将A部署到测试环境。我完成了该功能的工作,并将分支A合并到master 中。由于从文件一个是新鲜的,它被合并了,现在在主分支引用一个。是时候再次编辑了。deploy.rb
deploy.rb
That's a lot of seemingly unnecessary manual editing - the parameter should always match current branch name. On top of that, it is easy to forget to edit the settings each and every time.
这是很多看似不必要的手动编辑 - 参数应始终与当前分支名称匹配。最重要的是,很容易忘记每次编辑设置。
What would be the best way to automate this process?
自动化此过程的最佳方法是什么?
Edit:Turns out someone already had done exactly what I needed:
编辑:原来有人已经完成了我需要的工作:
This morning I had occasion to deploy a branch of a git repository to a staging server but hadn't the foggiest idea how. A quick search through the capistrano source code revealed that I could use set
:branch "branch_name"
in my deploy script. I tried it and it worked. I then figured I would need to make a similar change across all my branches. Of course, I'm a lazy sod and wondered if there wasn't a better way.If you're not familiar with git, the output of the git branch command is a list of branches with an asterisk marking the one currently checked out on your local machine. For example:
> git branch * drupal_authentication fragment_caching master
So, I figured, what if I just parsed the output and searched for the branch marked as current:
set :branch, if `git branch` =~ /\* (\S+)\s/m
Now I'm able to deploy whatever branch is current on my local machine from a single, shared, deploy script.
今天早上,我有机会将 git 存储库的一个分支部署到一个临时服务器,但对如何部署一无所知。快速搜索 capistrano 源代码发现我可以
:branch "branch_name"
在我的部署脚本中使用 set 。我试过了,它奏效了。然后我想我需要对我的所有分支进行类似的更改。当然,我是个懒惰的人,想知道是否有更好的方法。如果您不熟悉 git,git branch 命令的输出是一个带有星号的分支列表,标记当前在本地机器上检出的分支。例如:
> git branch * drupal_authentication fragment_caching master
所以,我想,如果我只是解析输出并搜索标记为当前的分支怎么办:
set :branch, if `git branch` =~ /\* (\S+)\s/m
现在,我可以通过一个共享的部署脚本在我的本地机器上部署当前的任何分支。
回答by wintersolutions
This works with Capistrano >= 3.1:
这适用于 Capistrano >= 3.1:
add this line to config/deploy.rb
:
将此行添加到config/deploy.rb
:
set :branch, ENV['BRANCH'] if ENV['BRANCH']
and then call capistrano with:
然后调用 capistrano :
cap production deploy BRANCH=master
This solution works with Capistrano < 3.1:
此解决方案适用于 Capistrano < 3.1:
# call with cap -s env="<env>" branch="<branchname>" deploy
set :branch, fetch(:branch, "master")
set :env, fetch(:env, "production")
回答by Eric Wanchic
Using Capistrano 3.1.0+, none of these were working for me anymore. Instead, according to their commented instructions:
使用 Capistrano 3.1.0+,这些都不适合我了。相反,根据他们的评论说明:
ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }
But, you don't want to use ask
or it will prompt you. Instead you should use set
. HEAD
is the top most branch; 'edge' as it's called. If you want a different branch, replace HEAD
with your branch name, eg: master
, staging
, etc.
但是,您不想使用,ask
否则它会提示您。相反,您应该使用set
. HEAD
是最顶端的分支;所谓的“边缘”。如果你想要一个不同的分支,替换HEAD
你的支行名称,如:master
,staging
等等。
To conclude with examples, in /config/deploy/production.rb
, you might include this line:
以示例结束,在 中/config/deploy/production.rb
,您可以包含以下行:
set :branch, proc { `git rev-parse --abbrev-ref master`.chomp }
...or
...或者
set :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }
btw, HEAD
is the default setting, so no need to really state that in file. Might be used better in a /config/deploy/edge.rb
.
顺便说一句,HEAD
是默认设置,因此无需在文件中真正说明。可能在/config/deploy/edge.rb
.
In /config/deploy/staging.rb
, you might include this line:
在 中/config/deploy/staging.rb
,您可以包含以下行:
set :branch, proc { `git rev-parse --abbrev-ref staging`.chomp }
...or
...或者
set :branch, proc { `git rev-parse --abbrev-ref test`.chomp }
You get the idea!
你明白了!
I hope these examples help future users of capistrano (^_^)
我希望这些例子能帮助 capistrano 未来的用户 (^_^)
回答by David Hersey
With multistage, it's actually now:
有了多级,现在实际上是:
cap production deploy -s branch=my-branch
The previous post syntax does not work in my environment
上一篇文章的语法在我的环境中不起作用
回答by Paul Odeon
I can confirm that the below still works in Cap 3.11.0 13/10/18 as well as Cap 2:
我可以确认以下内容仍然适用于 Cap 3.11.0 13/10/18 以及 Cap 2:
In deploy.rb / stage.rb:
在 deploy.rb / stage.rb 中:
set :branch, ENV['BRANCH'] || 'develop'
On the command line:
在命令行上:
cap deploy BRANCH=featurex
This gives you a default branch (which could be different for different environments), and the ability to change branches when you want.
这为您提供了一个默认分支(对于不同的环境可能会有所不同),并且可以在需要时更改分支。
回答by naven87
Alternatively you could structure it from the command line where you have a default branch and environment and also you are able to pass parameters to the cap call which could include the environment and the branch to use. This could be a branch that is explicitly passed or you could have a parameter which would indicate current branch as described in the link you listed.
或者,您可以从具有默认分支和环境的命令行构建它,并且您还可以将参数传递给 cap 调用,其中可能包括要使用的环境和分支。这可能是一个明确传递的分支,或者您可以有一个参数来指示您列出的链接中描述的当前分支。
#call with cap -S env="<env>" branch="<branchname>" deploy
...
# Prevents error if not parameter passed, assumes that default 'cap deploy' command
# and should deploy the master branch to the production server
set(:env, ‘production') unless exists?(:env)
set(:branch, ‘master') unless exists?(:branch)
if !env.nil? && env == "production"
role :web, "production_ip_address"
else # add more as needed
role :web, "development_ip_address"
end
if !branch.nil? && branch == "current"
set :branch, if `git branch` =~ /\* (\S+)\s/m
elsif !branch.nil?
set :branch, branch
else # add more as needed
set :branch, "master"
end
...
回答by asymmetric
If you're using capistrano-multistage, you only need to run
如果您使用的是capistrano-multistage,则只需要运行
cap -s branch=$MY_BRANCH deploy
or
或者
cap -s branch=$MY_BRANCH production deploy
without any further edit to your deploy.rb
.
无需对您的deploy.rb
.
回答by SakyHank
This command won't work anymore:
此命令将不再起作用:
cap deploy -s branch=your_branch
Support for -sS
flags was removed in capistrano v3+.
Here you can read more about it: link
It was mentioned in couple of answers, but currently is not correct.
-sS
在 capistrano v3+ 中删除了对标志的支持。
在这里你可以阅读更多关于它的信息:链接
它在几个答案中提到,但目前不正确。
What works for me:
in deploy.rb
file add
什么对我有用:
在deploy.rb
文件中添加
set :branch, ENV['BRANCH'] || :master
then run:
然后运行:
BRANCH=your_branch cap deploy
Also please notice that, in order to successfully run this command, you need to be on master branch.
另请注意,为了成功运行此命令,您需要在 master 分支上。
回答by Pablo Cantero
This solution should work with all versions of Capistrano.
此解决方案适用于所有版本的 Capistrano。
def branch_name(default_branch)
branch = ENV.fetch('BRANCH', default_branch)
if branch == '.'
# current branch
`git rev-parse --abbrev-ref HEAD`.chomp
else
branch
end
end
set :branch, branch_name('master')
Usage:
用法:
BRANCH=. cap [staging] deploy
# => deploy current branch
BRANCH=master cap [staging] deploy
# => deploy master branch
cap [staging] deploy
# => deploy default branch
回答by David Rosa
Im using version 3.3.5and i have this working:
我使用的是3.3.5版,我有这个工作:
set :branch, 'develop'
回答by Shakil
Method 1: Set stage specific branch (e.g. test, production) for deployment
方法 1:为部署设置阶段特定分支(例如测试、生产)
Put branch
configuration in stage files instead of 'deploy.rb' and set the target branch for that stage to deploy from.
将branch
配置放在阶段文件而不是“deploy.rb”中,并设置该阶段的目标分支以进行部署。
For a two stage app with associated branch name test
and production
, the configuration will look like this,
对于具有关联分支名称test
和的两阶段应用程序production
,配置将如下所示,
# app_root/config/deploy/test.rb
...
set :branch, "test"
...
# app_root/config/deploy/production.rb
...
set :branch, "production"
...
This method enables to deploy from stage specific branches. So, only additional step that'll be required is to merge or rebase latest code from base branch.
此方法允许从阶段特定分支进行部署。因此,唯一需要的额外步骤是从基本分支合并或重新设置最新代码。
Method 2: Deploy directly from any branch (using tag)
方法二:直接从任意分支部署(使用tag)
Another approach is to deploy using tag. In order to deploy using tag, set the branch
config. in 'deploy.rb' as follows,
另一种方法是使用标签进行部署。为了使用标签进行部署,请设置branch
配置。在“deploy.rb”中,如下所示,
set :branch, `git describe --tags $(git rev-list --tags --max-count=1)`.chomp
And, configure the CI to conditionally deploy to different stages if the associated tag pattern matches (e.g. /.*-test$/
).
并且,如果关联的标记模式匹配(例如/.*-test$/
),则配置 CI 以有条件地部署到不同的阶段。
Now, a deploy can be made from any branch,
现在,可以从任何分支进行部署,
First, create a tag from any branch,
git tag -a v0.1.0-test -m "Version 0.1.0-test"
And, push
git push origin v0.1.0-test
首先,从任何分支创建一个标签,
git tag -a v0.1.0-test -m "版本 0.1.0-test"
并且,推
git push origin v0.1.0-test
Note: The above methods are based on Capistrano 3.
注:以上方法基于 Capistrano 3。