BitBucket+Jenkins: Trigger build only when specifi

2020-06-23 08:52发布

问题:

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.

回答1:

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.



回答2:

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



回答3:

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.



回答4:

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



回答5:

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.