Waiting for API-triggered pipelines
Asked by plussier:

Hi,

I'm attempting to trigger a BB pipeline from Jenkins using the REST API (please tell me if there's a better way!) and wondering if I need to tell Jenkins to wait for it or not?

I'm a little unclear on how to use the REST API short of \`curl\` commands, and while this page <https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/> is a great resource on WHAT the APIs are, it's not overly helpful in getting started using them.

Would appreciate any help/pointers in how to have a Jenkins "stage" be a BB pipeline, and how to trigger and wait for that as well.

--

Paul


Answers:
author: Theodora Boudale
body: Hi Paul,

The following page has some examples of how to trigger a Bitbucket Pipeline via API:

* <https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-repositories-workspace-repo-slug-pipelines-post>

After a Bitbucket Pipelines build gets triggered, you can check its state with an API call like the following:

```
curl -u BitbucketUsername:AppPassword -X GET https://api.bitbucket.org/2.0/repositories/workspace-id/repo_slug/pipelines/{pipeline_uuid}?fields=state.name
```

When the pipeline is completed, the name of the state will be COMPLETED as follows:

```
{"state": {"name": "COMPLETED"}}
```

You can use that call every few seconds to check if the pipeline you have triggered has been completed or not.

If you want to get the status of the Pipelines build after it gets completed you can use

```
curl -u BitbucketUsername:AppPassword -X GET https://api.bitbucket.org/2.0/repositories/workspace-id/repo_slug/pipelines/{pipeline_uuid}?fields=state.result.name
```

which will return {"state": {"result": {"name": "FAILED"}}} for a failed build and {"state": {"result": {"name": "SUCCESSFUL"}}} if the build succeeded.

That being said, you will need to check Jenkins documentation or contact their support team to ask whether it is possible for a Jenkins build to wait until a certain condition is met (this is something we have no control over) and also how to execute REST API calls from a Jenkins build.

Kind regards,  
Theodora

comments: author: plussier
body: Hi Theorodora,

Thanks for the quick response! I greatly appreciate it, your answer is exactly what I was looking for!

For others, in the example above, {pipeline_uuid} is really {build_number}, which is returned in the initial response to the pipeline triggering.

If you pipe the response to the curl command through jq, you can retrieve it like this:

```
URL="https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/pipelines/"
curl -X POST \
     -is \
     -u ${username}:${password} \
     -H 'Content-Type: application/json' \
    ${URL} \
    -d '{
          "target": {
            "ref_type": "branch",
            "type": "pipeline_ref_target",
            "ref_name": "refname"
          }
        }' | jq -r '.["build_number"]'
```

Or if using python requests:

```
# Trigger the pipeline
response = requests.post(URL,
                         auth=HTTPBasicAuth(username, password),
                         headers=myheaders,
                         json=body)
build_num = response.json()['build_number']
```


Tags: bitbucket-cloud

bitbucket-pipelines

cloud

jenkins

rest-api

trigger