4

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?

Is it possible to trigger Jenkins from one specific branch only?

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.

Community
  • 1
  • 1
Matt MacLeod
  • 438
  • 2
  • 5
  • 17

5 Answers5

7

As @Ryan Taylor already said you can use the Bitbucket-Plugin:

  • Install Bitbucket Plugin at your Jenkins
  • Add a normal Post as Hook to your Bitbucket repository (Settings -> Hooks) and use following url:

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

  • Configure your Jenkins project as follows:
    • under build trigger enable Build when a change is pushed to BitBucket
    • under Source Code Management select GIT; enter your credentials and define Branches to build (like **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.

salcosand
  • 2,022
  • 4
  • 24
  • 27
  • Okay! Does this automatically trigger your develop or release branch jobs when you push a change-set to your feature branch? It looks like it occurs to me if I follow this. I'd very much like it if the plugin had a restriction on which branch to trigger the job for. :/ – Nasik Shafeek Jan 24 '20 at 02:06
2

You should be able to use Generic Webhook Trigger Plugin. 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.

It may help to have a look this example where its used with GitLab.

enter image description here

Tomas Bjerre
  • 3,270
  • 22
  • 27
1

The solutions suggested above would be better than my own, but I don't think the Bitbucket Plugin allows branch specific triggers.

From the Bitbucket Plugin documentation,

On each commit, all jobs with "Build when a change is pushed to BitBucket" option enabled will be triggered.

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.

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

0

Look into the BitBucket Plugin

I have not set it up with BitBucket before but I have set up this same functionality with GitHub.

I use the generic Git Plugin to attach the build to my git repository with an ssh key. 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. The BitBucket Plugin should have similar features/hooks.

  • 1
    where in the github plugin do you specify which branch to build? I'm not seeing it in the job config with this bitbucket one. PS thanks for pointing out that plugin, it seems like my solution I just need to figure out the setup – Matt MacLeod Dec 11 '14 at 15:58
  • In the source code management section (this should not be part of the BitBucket plugin) you will attach the job to a repository and Refspec. Then you will just need to make sure the correct build trigger selected. There is also overall jenkins configuration that will need to be set up to tie the BitBucket plugin to your bitbucket server – Ryan Taylor Dec 12 '14 at 02:44
0

For same scenario , I am using Poll SCM option in build triggers

we need to give ref in git area as +refs/heads/develop:refs/remotes/origin/develop.

here develop is branch name

and poll SCM schedule = */1 * * * * for every 1 minute

It will only trigger when develop branch changes

Jameel Grand
  • 2,294
  • 16
  • 32