---
title: "Trigger a BitBucket pipeline"
canonical: "https://docs.getxray.app/space/XRAYCLOUD/44565651/Trigger%20a%20BitBucket%20pipeline"
format: markdown
---
> Macro (toc)


# Bitbucket Cloud/Pipelines

## Trigger a Bitbucket pipeline build from an issue

<span style="color: #000000">In this very simple scenario, we'll implement a rule, triggered manually, that will trigger a Bitbucket pipeline build. The action will be available from the "Automation" panel, in all issues of the selected project.</span>

<span style="color: #000000">We're assuming that:</span>

- <span style="color: #000000">you just want to trigger a CI job, period; this job may be totally unrelated to the issue from where you triggered it</span>
- what the CI job will do, including if it will report the results back to Xray or not, is not relevant


### Bitbucket configuration

In Bitbucket we need to configure the pipeline as usual; no special configuration is needed.  Some [variables can be used and defined at multiple layers](https://support.atlassian.com/bitbucket-cloud/docs/variables-in-pipelines/).

![image](media://b11e6db4-6a8e-4272-a899-b8f31957928e)

<details>
<summary>bitbucket-pipelines.yml</summary>

```yml
# Use Maven 3.5 and JDK8
image: maven:3.5-jdk-8


pipelines:
  default:
    - step:
        caches:
          - maven
        script:
          - |
              echo "building my amazing repo..."
              mvn test
              export token=$(curl -H "Content-Type: application/json" -X POST --data "{ \"client_id\": \"$client_id\",\"client_secret\": \"$client_secret\" }" https://xray.cloud.xpand-it.com/api/v1/authenticate| tr -d '"')  
              echo $token
              curl -H "Content-Type: text/xml" -H "Authorization: Bearer $token" --data @target/surefire-reports/TEST-com.xpand.java.CalcTest.xml  "https://xray.cloud.xpand-it.com/api/v1/import/execution/junit?projectKey=CALC&testPlanKey=$testplan"
              echo "done
```
</details>



### Automation configuration

1. create a new rule and define the "When" (i.e. when it to should be triggered), to be "Manually triggered"

![image](media://8f086454-af88-4bf8-87ac-f0e5b8a7b15c)


2. define an action (i.e. the "Then") as "Send web request" and configure it as follows

![image](media://494dcfec-debb-49cd-ba53-c6bc98e73e98)

<details>
<summary>custom data (i.e. HTTP body content)</summary>

```javascript
{
    "target": {
      "ref_type": "branch",
      "type": "pipeline_ref_target",
      "ref_name": "master"
    }
}
```
</details>

- the Webhook URL provided above follows the [Bitbucket pipelines REST API syntax ](https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/pipelines/)
  - https://api.bitbucket.org/2.0/repositories/<workspace>/<repository>/pipelines/
- the Webhook HTTP POST body content, defined in the "Custom data" field, will be used to identify the target branch and also a variable containing the Test Plan issue key that will be accessible to the pipeline build
  **custom data (i.e. HTTP body content)**
- besides the "Content-Type" header that should be "application/json", define also an "Authorization" header having the value "Basic <auth>", where  the base64 encoded <auth> can be [generated](https://www.blitter.se/utils/basic-authentication-header-generator/) using your Bitbucket credentials


After publishing the rule, you can go to the screen of an issue and trigger the Bitbucket pipeline build.

![image](media://1d99ba25-956b-45b2-8e40-7bd562bedec9)



In this case, a new Test Execution would be created in Jira/Xray.

![image](media://51a397c3-3f8e-486b-bb2f-824a80a860ef)

## Trigger a Bitbucket pipeline build from a Test Plan and report the results back to it

<span style="color: #000000">In this simple scenario, we'll implement a rule, triggered manually, that will trigger a Bitbucket pipeline build. The action will be available from the "Automation" panel, for all Test Plan issues of the selected project.</span>

<span style="color: #000000">We're assuming that:</span>

- <span style="color: #000000">you just want to trigger a CI job, period; this job may be totally unrelated to the issue from where you triggered it</span>
- <span style="color: #172b4d">the results will be submitted back to Xray, if the pipeline is configured to do so</span>


### Bitbucket configuration

In Bitbucket pipeline definition we will make use of a variable named "testplan" (referenced by $testplan). This variable does not need to be defined explicitly as such in Bitbucket; Bitbucket will pass to the pipeline all "variables" identified in the REST API request, if so, whenever triggering the pipeline. 

![image](media://b11e6db4-6a8e-4272-a899-b8f31957928e)

<details>
<summary>bitbucket-pipelines.yml</summary>

```yml
# Use Maven 3.5 and JDK8
image: maven:3.5-jdk-8


pipelines:
  default:
    - step:
        caches:
          - maven
        script:
          - |
              echo "building my amazing repo..."
              mvn test
              export token=$(curl -H "Content-Type: application/json" -X POST --data "{ \"client_id\": \"$client_id\",\"client_secret\": \"$client_secret\" }" https://xray.cloud.xpand-it.com/api/v1/authenticate| tr -d '"')  
              echo $token
              curl -H "Content-Type: text/xml" -H "Authorization: Bearer $token" --data @target/surefire-reports/TEST-com.xpand.java.CalcTest.xml  "https://xray.cloud.xpand-it.com/api/v1/import/execution/junit?projectKey=CALC&testPlanKey=$testplan"
              echo "done
```
</details>


### Automation configuration

1. create a new rule and define the "When" (i.e. when it to should be triggered ), to be "Manually triggered"

![image](media://a4c94a97-ed88-447a-9b1e-538878cf6426)


2. define the condition so that this rule can only be executed from Test Plan issue; we could also define this on the previous step

![image](media://081e19f8-047c-44ac-a9b2-0a0e6e4e28ae)

3. define an action (i.e. the "Then") as "Send web request" and configure it as follows

![image](media://ea31f5d8-871a-445b-9f7c-9d934e1d9d52)


- the Webhook URL provided above follows the [Bitbucket pipelines REST API syntax](https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/pipelines/)
  - https://api.bitbucket.org/2.0/repositories/<workspace>/<repository>/pipelines/
- the Webhook HTTP POST body content, defined in the "Custom data" field, will be used to identify the target branch and also a variable containing the Test Plan issue key that will be accessible to the pipeline build
  **custom data (i.e. HTTP body content)**
- besides the "Content-Type" header that should be "application/json", define also an "Authorization" header having the value "Basic <auth>", where  the base64 encoded <auth> can be [generated](https://www.blitter.se/utils/basic-authentication-header-generator/) using your Bitbucket credentials

After publishing the rule, you can go to the screen of an issue and trigger the Bitbucket pipeline.

![image](media://c224c28a-21f0-4fcf-91ce-40fa71a7309a)


In this case, a new Test Execution would be created and linked back to the source Test Plan where the automation was triggered from

![image](media://80b23571-9d5d-4d6a-861a-c72d7c50c4ea)

![image](media://cea949f7-fdf2-494e-ad92-d391b294100e)

# References

- [GraphQL to JSON Body Converter](https://datafetcher.com/graphql-json-body-converter)
- [Jira Automation in Jira Cloud](https://support.atlassian.com/cloud-automation/docs/jira-cloud-automation/)
  - [Jira smart values - issues](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-issues/)
  - [Jira smart values - lists](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-lists/)
  - [Jira smart values - text fields](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-text-fields/)
  - [Jira smart values - users](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-users/)
  - [Jira smart values - conditional logic](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-conditional-logic/)
  - [Jira smart values - JSON functions](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-json-functions/)
- Bitbucket Cloud
  - [REST API for pipelines ](https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/pipelines/)
  - [using variables in pipelines](https://support.atlassian.com/bitbucket-cloud/docs/variables-in-pipelines/)
  - [integrating Jira Cloud and Bitbucket Cloud](https://support.atlassian.com/bitbucket-cloud/docs/integrate-jira-and-pipelines/)