Trigger Jenkins Pipeline on Bitbucket Pull Request

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.

Step 1

First login to jenkins and create new jenkins pipeline / job.

Step 2

Now provide an appropriate name for your job, here we create a pipeline as “pipeline-test”.

Step 3

Select “Multibranch Pipeline” as project and click “OK”.

Step 4

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”

Step 5

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.

Step 6

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”.

Step 7

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.

Step 8

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.

Step 9

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.

Step 10

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.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Powered by

Up ↑

%d bloggers like this: