These instructions are for users using the Jenkins Git Plugin 4.0 and higher. For Git Plugin versions older than that, see here.
When a pull request is created in Bitbucket, Webhook to Jenkins will send pull request references(**/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 **/from. 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 **/from branch spec is used then build can't be started properly. Because **/from 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 the 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