These instructions are for users using the Jenkins Git Plugin 3.0 and lower. For Git Plugin versions higher than that, see here.
When a pull request is created in Bitbucket, Webhook to Jenkins will send pull request references(refs/pull-request/<id>/from) as well as the branch name. If the pull request is from a fork, the app will only send the pull request reference. This should allow users to setup the plugin to just build from pull requests. Note, that cross-repo pull requests with fork repositories are supported only with Bitbucket Branch Source endpoint.
If you want to configure Jenkins to just build pull requests, do the following:
Configure Bitbucket Server
Advanced Configuration > Branch Options > Build All is selected. This will skip the whitelist and blacklist checks so that all new commits and tags send the required Jenkins notification.
Omit SHA1 must be unchecked in this configuration since it triggers polling mode on Jenkins side. And in this mode Jenkins process refs/heads/* only.
Set up a Jenkins Job for Pull Requests
In the job set the branch specifier to **/pull-requests/**. This will configure the job so it only responds to new pull requests.
Configure Pipeline Job for Pull Requests
When Jenkins webhook receives notification it needs to map repository url and branches to particular job. Pipeline job implementation uses information from previous builds.
This information includes:
So it makes configuring pipeline job is a bit tricky. Build needs to be started from Jenkins side:
once when job is configured initially
every time when job is reconfigured (repository url or branch specifier is changed)
And there is another problem: When only **/pull-requests/** branch spec is used then build can't be started properly. Because **/pull-requests/** branches are not fetched.
To solve this issue second branch spec can be added (like for example */master ) and when job is successfully invoked then it can be removed.
Triggering initial build
A good way to trigger build after branch spec change is using 'Trigger Jenkins' button in hook setting. In this case no branch filtering occurs and Jenkins uses exact commit id for checkout