简体   繁体   English

GitHub WebHooks 全局触发而不是每个分支

[英]GitHub WebHooks triggered globally instead of per branch

Our product creates WebHooks at GitHub. One for each customer project.我们的产品在 GitHub 创建 WebHooks。每个客户项目一个。

Each such project, is linked to a single branch.每个这样的项目都链接到一个分支。

When a push to GitHub is performed, the corresponding WebHook is triggered, which in its turn, is making a request to an endpoint on our side to perform a certain action.当执行到 GitHub 的push时,会触发相应的 WebHook,这反过来会向我们这边的端点发出请求以执行特定操作。

A common scenario is that a customer would have several projects, connected to several different branches of the same repository.一个常见的场景是客户有多个项目,连接到同一个存储库的几个不同分支。 Hence, several different WebHooks are connected to the same repository.因此,几个不同的 WebHooks 连接到同一个存储库。

The problem is that when a push is performed to one of the branches, GitHub triggers all repository related WebHooks.问题是,当对其中一个分支执行push时,GitHub 会触发所有与存储库相关的 WebHook。

We would expect that when a push is made to a certain branch, only a single corresponding WebHook would be triggered.我们希望当对某个分支进行推送时,只会触发一个相应的 WebHook。

I found two posts (one of them is from 2012) that seem to refer to this problem:我发现有两篇帖子(其中一篇是 2012 年的)似乎提到了这个问题:

A possible solution would be to parse the ref parameter sent inside the webhook request and control when to take action accordingly (haven't checked that direction yet, and hope ref indeed always exists and holds the right branch path/name).一种可能的解决方案是解析在 webhook 请求中发送的ref参数,并控制何时采取相应的操作(尚未检查该方向,希望ref确实始终存在并拥有正确的分支路径/名称)。 But that will be "too late" - cause all WebHooks will have been triggered by then...但这将是“太晚了”——因为那时所有的 WebHooks 都将被触发……

But seems unreasonable that GitHub wouldn't have a way to configure this behavior somehow.但是 GitHub 无法以某种方式配置此行为似乎是不合理的。

Help would be appreciated.帮助将不胜感激。

I've reached out GitHub support. 我已经达成了GitHub的支持。

I hope this post would help others, who misunderstand the relation between WebHooks and repositories / branches. 我希望这篇文章可以帮助其他人,他们误解了WebHooks和知识库/分支机构之间的关系。

Here's their answer: 这是他们的答案:

The behavior you observed is expected and there are no plans to change it in the near future. 您观察到的行为是预期的,并且没有计划在不久的将来更改它。

When you create a webhook on a repository and subscribe it to the push event -- the webhook will trigger when any branch or tag is pushed to, as documented here: 当您在存储库上创建webhook并将其订阅到push事件时 - 在推送任何分支或标记时将触发webhook,如下所述:

https://developer.github.com/v3/activity/events/types/#pushevent https://developer.github.com/v3/activity/events/types/#pushevent

There are no per-branch webhooks. 没有每个分支的webhook。

So, instead of creating multiple webhooks subscribed on the push event on the same repository, you should create only one, and check which branch was pushed to from the payload you receive (as you noticed, the name of the branch is passed via the ref field in the payload). 因此,不是在同一个存储库上的push事件上创建多个webhooks,而是应该只创建一个,并检查从您收到的有效负载推送到哪个分支(正如您所注意到的,分支的名称通过ref传递有效载荷中的字段)。

This answer made us realize our conception was wrong. 这个答案让我们意识到我们的观念是错误的。

Branches are not mapped to webhooks. 分支未映射到webhooks。 Each WebHook is linked to a repository, and when a commit to a branch is made, the branch is stated inside the ref attribute inside the WebHook web request, like this: 每个WebHook都链接到一个存储库,当提交分支时,分支在WebHook Web请求内的ref属性中声明,如下所示:

 {
  "ref": "refs/heads/branch_name",
  ...

Another thing to note is that GitHub limits the number of WebHooks to be created per Repository-Event: 另外需要注意的是GitHub限制了每个Repository-Event创建的WebHook数量:

You can create up to 20 webhooks for each event on each installation target (specific organization or specific repository). 您可以为每个安装目标(特定组织或特定存储库)上的每个事件创建最多20个webhook。

It was taken from here: 它取自这里:

https://developer.github.com/webhooks/ https://developer.github.com/webhooks/

That's important in this context, cause the creation of a WebHook per branch for the push event, made us reach that limit of 20 WebHooks, thus causing errors when trying to create additional WebHooks. 这在此上下文中很重要,导致为push事件创建每个分支的WebHook,使我们达到20个WebHook的限制,从而在尝试创建其他WebHook时导致错误。

Keeping it at one WebHook per repository would eliminate that problem. 将它保存在每个存储库的一个WebHook将消除该问题。

I am using Jenkins to use CI/CD and i realised that it doesnt build for each push event by github我正在使用 Jenkins 来使用 CI/CD,我意识到它不会通过 github 为每个推送事件构建

because it says "Specify the branches if you'd like to track a specific branch in a repository. If left blank, all branches will be examined for changes and built" according to Jenkins in the image below因为它说“如果您想跟踪存储库中的特定分支,请指定分支。如果留空,将检查所有分支的更改并构建”根据下图中的 Jenkins

在此处输入图像描述

If you are trying to parse in your own endpoint without using tools like Jenkins etc, you should parse the incoming response by branches如果您尝试在不使用 Jenkins 等工具的情况下在自己的端点中进行解析,则应该通过分支解析传入的响应

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

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