只构建已经推送到的 Git 分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27403576/
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
Build only the Git branch that has been pushed to
提问by Lóránt Pintér
I have a Jenkins job to build the master
branch of my GitHub repo. It triggers nicely when I push a commit to master
, and I'm very happy about it.
我有一份 Jenkins 工作来构建master
我的 GitHub 存储库的分支。当我向 推送提交时它会很好地触发master
,我对此感到非常高兴。
I would also like to have a single Jenkins job that would build any other branch in this repository. I.e. whenever I push a commit to a branch (that is not master
), I'd like to have this Jenkins job to build that branch, and only that branch.
我还希望有一个 Jenkins 作业可以在此存储库中构建任何其他分支。即,每当我将提交推送到一个分支(不是master
)时,我都希望有这个 Jenkins 工作来构建那个分支,并且只构建那个分支。
Using the GitHub pull-request plugin requires me to create a pull-request, and merges my commit with master
. I'd like to do this without having to create a pull-request, and to only build my commit without merging it with master
.
使用 GitHub pull-request 插件需要我创建一个 pull-request,并将我的提交与master
. 我想这样做而不必创建拉取请求,并且只构建我的提交而不将它与master
.
Setting up the Git plugin to build all branches means that whenever I push to master, all branches are built. I'd like to only build the one branch that has been modified.
设置 Git 插件来构建所有分支意味着每当我推送到 master 时,都会构建所有分支。我只想建立一个已经修改过的分支。
Is this possible to do in Jenkins?
这可以在詹金斯做吗?
回答by Mig82
I was having the same issue so I set up a dummy project to experiment and found the solution. And found that yes, you canbuild only the branch that's been pushed into.
我遇到了同样的问题,所以我建立了一个虚拟项目进行实验并找到了解决方案。并发现是的,您只能构建已推入的分支。
The short answer is to use a "Branch Specifier". For instance origin/feature**
.
简短的回答是使用“分支说明符”。例如origin/feature**
。
If you're using the GitFlowand all your feature branches are named with a feature/
prefix, then this is the solution.
如果您使用的是GitFlow并且您的所有功能分支都使用feature/
前缀命名,那么这就是解决方案。
And if you're curious here's how I figured it out.
如果你好奇这就是我是怎么想出来的。
From the repository's settings I set the service “Github plugin”with the Jenkins hook https://<jenkin server>/github-webhook/
.
Note that, at least for me what happened was that for some reason after pressing "test", the message of the payload being sent never changed to confirm it was received/acknowledged or anything. Maybe there's no reply. It's confusing, but anyway...
从存储库的设置中,我使用 Jenkins hook设置了服务“Github 插件”https://<jenkin server>/github-webhook/
。请注意,至少对我来说发生的事情是,由于某种原因,在按下"test" 后,发送的有效负载的消息从未改变以确认它已被接收/确认或任何其他内容。也许没有回复。这很令人困惑,但无论如何......
I created a new Jenkins job and set the branch specifierto blank-which Jenkins then automatically sets to **
.
我创建了一个新的 Jenkins 作业并将分支说明符设置为空白- 然后 Jenkins 自动设置为**
.
I created a feature branch feature/foo
and pushed into it.
我创建了一个功能分支feature/foo
并将其推入其中。
- Build 1 was fired, but on the
master
branch. - Build 2 was also fired on the
feature/foo
branch.
- Build 1 被解雇了,但在
master
分支上。 - Build 2 也在
feature/foo
分支上被触发。
So it seems with the **
or blank specifier the plugin will fire builds on all branches of a repo whenever there's a push to anyof them.
因此,似乎使用**
or 空白说明符,只要对其中任何一个进行推送,插件就会在存储库的所有分支上触发构建。
Then I tried with pattern refs/heads/feature/foo
and pushed another change to the feature/foo
branch.
然后我尝试使用模式refs/heads/feature/foo
并将另一个更改推送到feature/foo
分支。
- Build 3 was fired on the
feature/foo
branch. - No other builds were fired.
- Build 3 在
feature/foo
分支上被触发。 - 没有其他构建被解雇。
Sort of ok, but this is too rigid. It would force us to have one build job for each feature branch. I wanted one build job for allfeature branches.
还可以,但这太死板了。这将迫使我们为每个功能分支进行一项构建工作。我想要所有功能分支的构建工作。
Then I tried a wildcard with pattern refs/heads/feature\*\*
and pushed changes to feature/foo
.
然后我尝试了一个带有模式的通配符refs/heads/feature\*\*
并将更改推送到feature/foo
.
- No build was fired.
- 没有构建被解雇。
Then I tried branch specifier refs/heads/feature/\*\*
and pushed.
然后我尝试了分支说明符refs/heads/feature/\*\*
并推送。
- No build was fired
- 没有构建被解雇
Then I saw the help of the "Branch Specifier"field reads this under the "Wildcards" section:
然后我看到“Branch Specifier”字段的帮助在“Wildcards”部分下读取:
The syntax is of the form:
REPOSITORYNAME/BRANCH
. In addition,BRANCH
is recognized as a shorthand of*/BRANCH
,*
is recognized as a wildcard, and**
is recognized as wildcard that includes the separator/
. Therefore,origin/branches*
would matchorigin/branches-foo
but notorigin/branches/foo
, whileorigin/branches**
would match bothorigin/branches-foo
andorigin/branches/foo
.
语法格式如下:
REPOSITORYNAME/BRANCH
. 此外,BRANCH
被识别为 的简写*/BRANCH
,*
被识别为通配符,**
被识别为包含分隔符的通配符/
。因此,origin/branches*
将匹配origin/branches-foo
但不匹配origin/branches/foo
,而同时origin/branches**
匹配origin/branches-foo
和origin/branches/foo
。
So I tried origin/feature**
所以我试过了 origin/feature**
- Build 4 was fired on
origin/feature/foo
.
- 构建 4 被解雇了
origin/feature/foo
。
Eureka! Seems you can't use wildcards with the references (starting with refs/
), but you canwith the names of the actual branches (starting with origin/
).
尤里卡!似乎您不能在引用中使用通配符(以 开头refs/
),但您可以使用实际分支的名称(以 开头origin/
)。
Then I created branch feature/bar
and pushed to it. To verify that only this one would be built and not other branches also starting with feature/
.
然后我创建了分支feature/bar
并推送到它。为了验证仅构建这个分支而不构建其他分支也以feature/
.
- Build 5 was fired on
origin/feature/bar
.
- 构建 5 被解雇了
origin/feature/bar
。
Looked almost there. Just needed a few more tests.
看的差不多了。只需要再做几次测试。
Then I pushed another change to origin/master
然后我将另一个更改推到 origin/master
- No build fired. Great!
- 没有构建被解雇。伟大的!
Then I pushed another change to feature/bar
, to test that only this branch would be built. Despite the fact that origin/master
had also been pushed into.
然后我将另一个更改推送到feature/bar
,以测试仅会构建此分支。尽管事实上origin/master
也被推入了。
- Build 6 was fired on
feature/bar
. - No other builds fired.
- 构建 6 被解雇了
feature/bar
。 - 没有其他构建被触发。
Looks good to me.
在我看来很好。
回答by thatway_3
You can see there plugin of multiple branch configuring with Jenkins. Getting feedback for any branch from Jenkins is possible with that method.
您可以看到使用 Jenkins 配置的多个分支的插件。使用该方法可以从 Jenkins 获取任何分支的反馈。
https://wiki.jenkins-ci.org/display/JENKINS/Multi-Branch+Project+Plugin
https://wiki.jenkins-ci.org/display/JENKINS/Multi-Branch+Project+Plugin
I hope it may help.
我希望它会有所帮助。
回答by Robert
回答by Sam
As @thatway_3 mentioned, this is possible with a plugin. The Multi-Branch Project Plugin that they linked to is deprecated, but you can use the Pipeline Multibranch Plugininstead.
正如@thatway_3提到的,这可以通过插件实现。他们链接到的多分支项目插件已被弃用,但您可以改用管道多分支插件。
If you install that plugin and then create a new job of type "Pipeline Multibranch", you can configure that to track a particular repository. It will then (correctly!) build all branches from there.
如果您安装该插件,然后创建一个“Pipeline Multibranch”类型的新作业,您可以将其配置为跟踪特定存储库。然后它将(正确!)从那里构建所有分支。
See also: https://jenkins.io/blog/2015/12/03/pipeline-as-code-with-multibranch-workflows-in-jenkins/
另见:https: //jenkins.io/blog/2015/12/03/pipeline-as-code-with-multibranch-workflows-in-jenkins/
Hope this helps.
希望这可以帮助。
回答by LeGEC
Actually, there seems to be an answer to precisely this question on the Jenkins GitHub plugin page :
实际上,Jenkins GitHub 插件页面上似乎有这个问题的答案:
回答by user1808671
you can easily specify branch in job configuration:
您可以轻松地在作业配置中指定分支:
in section Source Code Managementwhere you have already specified your git repository URL you have also an option to specify branches to build. Simply please a name of your branch instead of master (which is default value).
在源代码管理部分,您已经指定了 git 存储库 URL,您还可以选择指定要构建的分支。只需取一个您的分支名称而不是 master 名称(这是默认值)。