This is a step by step guide on how to trigger jenkins pipeline upon a pull request creation/update to bitbucket repository.
Here we will go through the steps to create a jenkins multibranch pipeline and trigger it upon pull request creation or update then checkout pull request instead of the branch or master.
First login to jenkins and create new jenkins pipeline / job.
Now provide an appropriate name for your job, here we create a pipeline as “pipeline-test”.
Select “Multibranch Pipeline” as project and click “OK”.
Remember to provide pipeline name again as “Display Name” otherwise your pipeline will be displayed as the repository name instead of the name which you selected. Then in “Branch Soruces” selected “Bitbucket”
In bitbucket source select the credentials / owner / repository name. Hope that you’ve already created your credentials which you’ll be used to checkout the repository. If not first please credentials as in here.
Since we just want to checkout pull request we have to change the behaviors as below. Delete the rest of the behaviors and leave only “Discover pull requests from origin” and select strategy as “The current pull request revision”.
In the build configuration provide your pipeline script path in the repository. Here I’ve my pipeline script named as jenkins-ci.groovy and it’s already pushed to the bitbucket repository.
Multibranch pipeline will create a new job for every pull request that we are going to create, so later we’ll have number of jobs under this project. In order to get rid of that it’s better to set some retention period for these pull requests. Here I’ve set 14 PRs/Jobs and delete the rest.
Now this is one of the most important steps, normally if your user (the credentials you provided in Step 5) has the necessary permissions it’ll add a bitbucket webhook to your repository with jenkins pipeline URL. But it’s worth to check it and if it’s not there add it as shown below.
Login to bitbucket cloud -> Go to your repository -> Settings -> Webhooks
You can set any title for your webhook, here we’ve named it as “jenkins-hook-pr-creation” since this webhook will trigger on PR creation or update. Set URL as below and check “Created” / “Updated” under Pull Request.
URL – http://your-jenkins-server/bitbucket-scmsource-hook/notify
If all above steps are done, then you can create a pull request and check whether the multibranch pipeline has triggered accordingly. Upon success you’ll be able to see list of jobs in “Pull requests” in your multibranch project.
Note – If you come up with an issue please check whether bitbucket has access to your jenkins server. You can see such an error in webhook.