Issue
-
I want to configure Jenkins and Bitbucket Server to automatically trigger non-multibranch jobs on changes
Environment
Resolution
The Git Plugin provides support in Jenkins to process webhooks received from Bitbucket Server via the add-on Webhook to Jenkins for Bitbucket. This add-on performs a request to the endpoint /git/notifyCommit
that is the push notification feature of the Git Plugin.
The Bitbucket Plugin provides support in Jenkins to process webhooks received from Bitbucket Server via the add-on Bitbucket Server Web Post Hooks Plugin. This add-on performs a request to the endpoint /bitbucket-hook/
.
The Bitbucket Plugin functionality was broken prior to version 1.1.7 of the plugin. You can find more details in JENKINS-28877. |
Configuration in Jenkins
Freestyle Jobs
1) Configure the Git SCM:

2) Configure the project with "SCM Polling" and ensure "Ignore post-commit hooks" is disabled:

Pipeline Jobs
1) Configure the Pipeline script from SCM
:

Or a Pipeline script
:
node { checkout([$class: 'GitSCM', branches: [[name: '*/develop']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[ credentialsId: 'support-team-bitbucket-up', url: 'https://bitbucket.beescloud.com/scm/ce/ce-424.git' ]]]) }
2) Configure the project to "SCM Polling" and ensure "Ignore post-commit hooks" is disabled:

IMPORTANT: Build the project once to retrieve the Git data.
It is possible to checkout multiple repositories in Pipeline. Be aware that when the Git plugin receives a notification that a certain repository has changes, any Jenkins pipeline that checkouts this repository using the git or the checkout step may be triggered if this step does not have poll: false specified
|
Configuration in Bitbucket Server side
Go to the Settings
of a repository. Then go to the section Hooks
and click on Bitbucket Server Webhook to Jenkins:

Specify the Jenkins URL. Also ensure that the Repo Clone URL
matches the Repository URL in your Jenkins job:

Click on Enable
and ensure that the webhook and enabled:

From now on, Push
events to the repository notifies the Bitbucket plugin and triggers a new build.
(Note: Since version 4.1.1 of the Webhook To Jenkins for Bitbucket, it is now possible to set up a webhook at a Bitbucket Project level. It is also possible to notify multiple Jenkins instances. See the Documentation of the Webhook To Jenkins for Bitbucket for more details.)
Consideration
1) You need to ensure that the Repo Clone URL
configured is exactly the same as in the Jenkins job, if not the trigger will not work correctly.
2) You need to ensure that your workspace is not deleted after every build. The Git plugin needs the workspace in order to compare previous commits to check if there were changes in the repository or not.
Pull Requests
(We strongly encourage to use Pipeline Multibranch for such scenario)
There is currently no simple solution to trigger jobs on Pull Requests for Bitbucket Cloud. There is some work in progress in the Bitbucket Plugin to make this possible. This is tracked by JENKINS-23685.
In the meantime, a workaround is to use polling solution like Stash Pullrequest Builder
Troubleshooting
Please have a look at Bitbucket Webhooks Troubleshooting to troubleshoot issues with these solutions.