git 如果从 bitbucket 触发时发生更改,我如何让 Jenkins 仅构建功能分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19003098/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 09:06:37  来源:igfitidea点击:

How can I make Jenkins only build feature branches if they have changed when triggering from bitbucket

gitjenkinscontinuous-integrationbranchbitbucket

提问by YakobeYak

I have a Jenkins job that builds branches that match "origin/feature/**".

我有一个 Jenkins 工作,它构建与“origin/feature/**”匹配的分支。

Bitbucket triggers the job to build whenever there are changes to the repository via a repository hook (https://confluence.atlassian.com/display/BITBUCKET/Jenkins+hook+management).

只要通过存储库挂钩 ( https://confluence.atlassian.com/display/BITBUCKET/Jenkins+hook+management)对存储库进行更改,Bitbucket 就会触发作业构建。

This works fine whenever there is a change on a feature branch, the job recognizes that the branch has changed and builds it.

每当功能分支发生更改时,这都可以正常工作,该作业会识别该分支已更改并构建它。

However, when there is a commit to a non-feature branch (eg. a merge to develop or master) bitbucket still triggers the job. There are no changes to any of the feature branches so it just builds the last built feature branch again.

但是,当提交到非功能分支(例如合并到 develop 或 master)时,bitbucket 仍然会触发作业。任何功能分支都没有更改,因此它只会再次构建最后构建的功能分支。

Is there a way to control this?

有没有办法控制这个?

采纳答案by salcosand

After some research I found a good solution finally:

经过一番研究,我终于找到了一个很好的解决方案:

  • Install Bitbucket Pluginat your Jenkins
  • Add a normal Post as Hook to your Bitbucket repository (Settings -> Hooks) and use following url:
  • 在您的 Jenkins 上安装Bitbucket 插件
  • 将一个普通的 Post 作为 Hook 添加到您的 Bitbucket 存储库(Settings -> Hooks)并使用以下 URL:

https://YOUR.JENKINS.SERVER:PORT/bitbucket-hook/

https: //YOUR.JENKINS.SERVER:PORT/bitbucket-hook/

  • Configure your Jenkins project as follows:
    • under build triggerenable Build when a change is pushed to BitBucket
    • under Source Code Managementselect GIT; enter your credentials and define Branches to build(like **feature/*)
  • 按如下方式配置您的 Jenkins 项目:
    • 将更改推送到 BitBucket 时,构建触发器下启用构建
    • 源代码管理下选择GIT;输入您的凭据并定义要构建的分支(如**feature/*

By this way I have three build projects, one for all features, one for develop and one for release branch.

通过这种方式,我有三个构建项目,一个用于所有功能,一个用于开发,一个用于发布分支。

回答by huebym

Since i can not answer the comment top (whyever)

由于我无法回答评论顶部(为什么)

"you are correct but I want to do in jenkins pipeline job"

“你是对的,但我想做詹金斯管道的工作”

To let a pipeline-job build automatically after a push (per hook) i created another job that is configured just to be triggered by the bitbucket/git and has a follow-up job that points to the pipeline-job in the post-build-action. That was the only way i could manage this

为了在推送(每个钩子)后自动构建管道作业,我创建了另一个作业,该作业配置为仅由 bitbucket/git 触发,并有一个后续作业指向构建后的管道作业-行动。那是我能做到的唯一方法

回答by Tomas Bjerre

You can do that with Generic Webhook Trigger Plugin.

您可以使用Generic Webhook Trigger Plugin做到这一点

  • Resolve the branch name, from the JSON payload, into a variable like reference.
  • The optional filter text can be set to $referenceand the filter regexp set to ^(refs/heads/feature/.+)$
  • 将分支名称从 JSON 负载解析为类似reference.
  • 可选的过滤器文本可以设置为$reference,过滤器正则表达式设置为^(refs/heads/feature/.+)$

回答by Electrawn

The answer was in the comments section you linked to. Bitbucket will always call the hook to your job.

答案在您链接的评论部分。Bitbucket 将始终调用您的工作的钩子。

You need a proxy in front of Jenkins to segregate out posts.

您需要在 Jenkins 面前使用代理来隔离帖子。

https://github.com/oveddan/gitBuildForwarder

https://github.com/oveddan/gitBuildForwarder

If this was SVN, I would tell you how to alter the SVN hook to only selectively trigger the hook based on Regex filtering.

如果这是 SVN,我会告诉您如何更改 SVN 钩子以仅根据正则表达式过滤选择性地触发钩子。