How to launch a bitbucket pipeline on pull request creation, AKA launch sonar on pull request

I‘m a huge fan of sonar analysis, it may be not perfect but it does provide insights on your code reliablity, security and maintainabily. So I wanted to setup a sonar analysis on every pull requests so the reviewer could use sonar as part of his review.

As my client is using bitbucket pipeline for his CI, I’ve created a custom pipeline to launch sonar scanner. Unfortunatly there is no way in bitbucket to trigger a pipeline for a pull request. But as there is webhooks and api this can be still be done using Zapier. Here is the tutorial :

Bitbucket does not provide a way to execute a pipeline when a pull request is created.

But it does provide two things :
* A webhook when a pull request is created
* A rest API to launch a pipeline

In this tutorial I will show you how to use an IFTTT (Zapier in that case) to connect the webhook and rest API.

First you need to go to zapier and create an account

Then you must create a Zap

Receiving pull request creation notification in Zapier

Choose webhook

We choose “Catch hook” as this is the first step to receive pull request creation notification inside Zapier

The next step allows you to filter the received content, we will continue without making any change

On the “Test This Step” step Zapier will provide a unique Url that will be used to receive webhooks

Let’s copy this url and browse to bitbucket (do not leave zapier). In bitbucket you will create a webhook for your repository

In the webhook creation page you must set the Url field to the url provided by Zapier and specify which trigger should be used for this webhook (pull request created in that case)

To test if it works and get the available variable in Zapier, you must create a pull request on your repository.

Now go back to the Zapier page and click on “Ok I did this!”

If everything went smoothly you should end up with a green “Test successful!”

Click on continue.

Send bitbucket pipeline execution request

For the second part you will create a webhook call :

The bitbucket pipeline API takes a POST call (view API doc)

You must first fill the Url field. As stated by the API documentation, the url to trigger a pipeline looks like that<username>/<repository-name>/pipelines/

You must replace username and repository name with the data sent by the webhook. This is how you do it:

Then you should select a Json payload type and setup the data to transmit the required Json. In our case we want to trigger a custom pipeline for the source branch of the pull request.

See “Trigger a specific pipeline definition for a commit on a branch or tag” on bitbucket API doc

We need to send this JSON to the API

"target": {
"selector": {
"type": "custom",
"pattern": "Deploy to production"
"type": "pipeline_ref_target",
"ref_name": "master",
"ref_type": "branch"

In zapier, to transform flat data to dictionnary, you must use double underscore in the field name. For target > selector > type, this lead to target__selector__type

You should end up with this setup

Where target__selector__pattern is the name of your custom pipeline

Finally you should set your bitbucket username and password in the Basic Auth field

And voila, you have a custom pipeline being triggered for every pull requests created !