简体   繁体   English

BitBucket + Jenkins:仅在更改特定分支时触发构建

[英]BitBucket+Jenkins: Trigger build only when specific branch is changed

The following is a purported solution to the problem, although it does seem like a workaround rather that a definitive solution. 以下是该问题的声称解决方案,虽然它似乎是一种解决方法,而不是一个明确的解决方案。 Is there a way, through either job config or bitbucket hook config, that I can set my jobs to run a build only when a specific branch is pushed to? 有没有办法,通过作业配置或bitbucket钩子配置,我可以设置我的工作只有在推送特定分支时运行构建?

Is it possible to trigger Jenkins from one specific branch only? 是否有可能仅从一个特定分支触发Jenkins?

It has been about a year since the posting of that question, I'm hoping there's been some plugin updates to allow for this without the extra work required in the above link, or perhaps there's always been an easy way that I'm just not seeing. 发布该问题已经过去了大约一年,我希望有一些插件更新可以在没有上述链接所需的额外工作的情况下实现这一点,或者可能总是有一种简单的方法,我只是没有看到。

As @Ryan Taylor already said you can use the Bitbucket-Plugin: 正如@Ryan泰勒已经说过你可以使用Bitbucket-Plugin:

  • Install Bitbucket Plugin at your Jenkins 在你的Jenkins安装Bitbucket插件
  • Add a normal Post as Hook to your Bitbucket repository ( Settings -> Hooks ) and use following url: 将正常的帖子作为Hook添加到您的Bitbucket存储库( 设置 - >挂钩 )并使用以下URL:

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

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

By this way I have three build projects, one for all features, one for develop and one for release branch. 通过这种方式,我有三个构建项目,一个用于所有功能,一个用于开发,一个用于发布分支。

And best of it, you don't have to ad new hooks for new Jenkins projects. 最好的是,您不必为新的Jenkins项目添加新的钩子。

The solutions suggested above would be better than my own, but I don't think the Bitbucket Plugin allows branch specific triggers. 上面提出的解决方案将比我自己更好,但我不认为Bitbucket插件允许分支特定的触发器。

From the Bitbucket Plugin documentation, 从Bitbucket插件文档中,

On each commit, all jobs with "Build when a change is pushed to BitBucket" option enabled will be triggered. 在每次提交时,将触发所有具有“在将更改推送到BitBucket时生成”选项的作业。

This is a shotgun approach, and obviously wasteful. 这是一种霰弹枪方法,显然是浪费。

I have solved this problem by deploying a small web application to the same application server hosting my jenkins instance which parses the json post content sent by bitbucket and selectively trigger branch specific hooks. 我已经通过将一个小型Web应用程序部署到托管我的jenkins实例的同一个应用程序服务器来解决了这个问题,该实例解析了bitbucket发送的json post内容并选择性地触发了分支特定的钩子。

The code is clean and easy to follow, and would be very easy to modify to meet your own needs; 代码简洁易懂,易于修改以满足您的需求; however, its worth noting I did not set out to create a generic solution for others. 但值得注意的是,我没有着手为其他人创建通用解决方案。 Regardless it could prove useful to you as a starting point. 无论它作为一个起点对您有用。

https://github.com/Hillrunner2008/bitbucket-hook-manager https://github.com/Hillrunner2008/bitbucket-hook-manager

Look into the BitBucket Plugin 查看BitBucket插件

I have not set it up with BitBucket before but I have set up this same functionality with GitHub. 我以前没有用BitBucket设置它,但我已经使用GitHub设置了相同的功能。

I use the generic Git Plugin to attach the build to my git repository with an ssh key. 我使用通用的Git插件通过ssh键将构建附加到我的git存储库。 I then select the "Build when a change is pushed to GitHub" option which I believe is part of the GitHub Plugin which is mostly configured in the Jenkins Configuration not the Project Configuration. 然后,我选择“将更改推送到GitHub时构建”选项,我认为该选项是GitHub插件的一部分,该插件主要在Jenkins配置中配置,而不是项目配置。 The BitBucket Plugin should have similar features/hooks. BitBucket插件应具有类似的功能/钩子。

For same scenario , I am using Poll SCM option in build triggers 对于相同的场景,我在构建触发器中使用Poll SCM选项

we need to give ref in git area as +refs/heads/develop:refs/remotes/origin/develop . 我们需要在git区域给出ref作为+refs/heads/develop:refs/remotes/origin/develop

here develop is branch name 这里开发的是分支名称

and poll SCM schedule = */1 * * * * for every 1 minute 并且每1分钟轮询SCM schedule = */1 * * * *

It will only trigger when develop branch changes 它只会在开发分支更改时触发

You should be able to use Generic Webhook Trigger Plugin . 您应该能够使用Generic Webhook Trigger插件 You can use the "Optional filter" feature to trigger only for the branch you want. 您可以使用“可选过滤器”功能仅触发所需的分支。

You will need to fiddle with JSONPath which requires you to have knowledge about the structure of the JSON sent with the webhook from Bitbucket. 您将需要摆弄JSONPath,这需要您了解使用Bitbucket的webhook发送的JSON结构。

It may help to have a look this example where its used with GitLab. 看看这个与GitLab一起使用的例子可能会有所帮助。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM