How to trigger PR pipelines in CircleCI for BitBucket

Hey folks,

I couldn’t find a way to trigger a CircleCI build when a pull request was opened or updated in BitBucket. I only found a thread where people were discussing this issue but with no solution available (https://discuss.circleci.com/t/pull-requests-not-triggering-build/1213/29).

As the thread is already closed, I am posting my solution here! It is quite simple once you come up with the “hack”: Simply use the BitBucket PR pipeline for triggering a new build in CircleCI

Here is my bitbucket-pipelines.yml file:

pipelines:
  pull-requests:
    '**':
      - step:
          name: Trigger PR workflow on CircleCI
          script:
            - echo "Triggering PR build for branch $BITBUCKET_BRANCH"
            - >
              curl -ss -X POST -H "Content-Type: application/json" -d '{"build_parameters":{"BITBUCKET_PR":true}}' "https://circleci.com/api/v1.1/project/bb/:username/:project/tree/$BITBUCKET_BRANCH?circle-token=$CIRCLE_CI_ACCESS_TOKEN"

Note: I could not use a CircleCI 2.1 config as the API does not support “build_parameters” yet (see the API doc: https://circleci.com/docs/api/#trigger-a-new-build-by-project-preview). Therefore, I needed to downgrade to version 2.0 so I could set a “PR” environment variable when triggering the build.

This “hack” only costs you around 4-6 seconds in BitBucket pipelines as the job is triggered and thats it so the pipeline finishes quite quickly.

Hope this helps out some people :slight_smile:

Best regards,

derbenoo

2 Likes

Thanks for your solution. Do you have any idea how to escape a branchname if is has an “/” in it?
ie:
feature/my-feature-branch