22

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:

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.

ilans
  • 2,537
  • 1
  • 28
  • 29
  • Parse a single webhooks for push events and filter by branch. Alternatively, you could fork each customer project where each repo has separate webhooks. – osowskit Sep 13 '17 at 15:47
  • @osowskit how would that be implemented in a Jenkins job? Would you just need to specify the branch in the refspec field for the repo? If not I'm not aware of a build trigger plugin for parsing a payload and matching refs to determine whether or not to build (but maybe one exists?) – Stew C Jul 19 '19 at 00:17

2 Answers2

34

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.

ilans
  • 2,537
  • 1
  • 28
  • 29
  • any resolution to your problem? I'm facing the same issue. *Any* push to the repo triggers both *dev* and *stage* builds. Highly annoying. – rivanov Feb 27 '19 at 18:06
  • Did you follow my answer regarding the "ref" attribute? – ilans Feb 27 '19 at 19:26
  • yes I did. But the problem seems to be with github, and when the hook is triggered. – rivanov Feb 28 '19 at 15:36
  • 1
    Carefully check your code and how you read the "ref". I have a strong feeling that it's your implementation rather than a problem with GitHub... – ilans Feb 28 '19 at 19:01
  • 11
    push to certain branch triggers something is a common scenario. GitHub's design choice (not support it) produces a lot of unnecessary network requests, and, **more importantly**, forces any webhook receiver who requires this feature to implement the parsing ref logic (it could have been implemented just once at GitHub). – weakish Mar 01 '19 at 06:03
  • For implementing in Jenkins, would you just need to specify the branch in the refspec field in the job and the job will automatically verify it with the ref in the payload? – Stew C Jul 19 '19 at 00:13
0

I am using Jenkins to use CI/CD and i realised that it doesnt build for each push event by 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

enter image description here

If you are trying to parse in your own endpoint without using tools like Jenkins etc, you should parse the incoming response by branches

Furkan ozturk
  • 654
  • 8
  • 26