Limit webhook trigger on specific branch

I have setup a webhook in Bitbucket to trigger a jenkins build on every push.

The issue is that my jenkins build itself will make create a tag for the release candidate and pushes it to bitbucket.

This creates an infinite loop of builds.

Is there a way to limit the trigger to a specific branch? For example the webhook triggers on a push to master but not when a tag is pushed.

1 answer

This widget could not be displayed.

Hi Cesar,

Is there no way for you to change your build such that it only pushes a tag to Bitbucket for a specific branch? The webhook from Bitbucket has to include the name of the branch.

I'm no PM, but I would imagine that one has more flexibility to write scripts how they please on the Jenkins side and hence perhaps the challenge of filtering branches is better tackled there.

Hi Abhin, thanks for your help.

I'm not sure what you mean by pushing a tag for a specific branch. In Git tags are objects like commits and branches. Pushing can involve any of them.

Jenkins has a REST API that allow you to trigger a build. Once the call to the API is made, it will trigger a build. 

I'm using the cloud version of Bitbucket and I can't specify a branch name for the trigger:

Screen Shot 2016-07-12 at 07.10.21.png

I'll continue looking for a way to prevent Jenkins from building on trigger if the desired branch has not changed.

Hi Cesar,

I totally didn't phrase that right. What I'm getting at is that when Jenkins starts a build (after being triggered by a BB webhook), it must run some script that you wrote that then does the tagging (and pushing). From what I understand, the problem you're facing is that push from the build triggers yet another webhook etc. But I suspect that you can break the loop from your own script using some sort of pre-condition that must be satisfied before your script decides to tag the release candidate and push the tag. 

Does you build script have access to the payload of the webhook? If so, the payload contains what was pushed. You should be able to then notice that the push only contained a tag and that the name of the tag matched the pattern you must use for your release candidates. Based on that pre-condition, you could choose to not tag again and push the tag.

Please let me know if that works for you.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 21, 2018 in Bitbucket

Branch Management with Bitbucket

As a project manager, I have discovered that different developers want to bring their previous branching method with them when they join the team. Some developers are used to performing individual wo...

1,523 views 9 11
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you