Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitHub WebHooks triggered globally instead of per branch

Our product creates WebHooks at GitHub. One for each customer project.

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.

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.

The problem is that when a push is performed to one of the branches, GitHub triggers all repository related WebHooks.

We would expect that when a push is made to a certain branch, only a single corresponding WebHook would be triggered.

I found two posts (one of them is from 2012) that seem to refer to this problem:

  • Web Hooks - execute only for specified branches
  • Ability to select a specific branch in webhook

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). But that will be "too late" - cause all WebHooks will have been triggered by then...

But seems unreasonable that GitHub wouldn't have a way to configure this behavior somehow.

Help would be appreciated.

like image 329
ilans Avatar asked Sep 10 '17 11:09

ilans


People also ask

How does GitHub Webhook work?

GitHub Webhooks allow you to create or set up integrations on GitHub.com, such as GitHub Apps or OAuth Apps, that subscribe to specific events. They will send an HTTP POST payload to the Webhook's defined URL when one of those events occurs.

What is secret in GitHub Webhook?

Secret. Setting a webhook secret allows you to ensure that POST requests sent to the payload URL are from GitHub. When you set a secret, you'll receive the X-Hub-Signature and X-Hub-Signature-256 headers in the webhook POST request.

What is GitHub Hook trigger for GITScm polling?

The Github Integration plugin will add the “Github hook trigger for GITScm polling” option for you. Which means that every time Jenkins receives a PUSH GitHub hook (from the repository you defined in the Source Code Management section) it will trigger the polling login you previously defined.


1 Answers

I've reached out GitHub support.

I hope this post would help others, who misunderstand the relation between WebHooks and repositories / branches.

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:

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

There are no per-branch webhooks.

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).

This answer made us realize our conception was wrong.

Branches are not mapped to 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:

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

Another thing to note is that GitHub limits the number of WebHooks to be created per Repository-Event:

You can create up to 20 webhooks for each event on each installation target (specific organization or specific repository).

It was taken from here:

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.

Keeping it at one WebHook per repository would eliminate that problem.

like image 56
ilans Avatar answered Oct 12 '22 05:10

ilans