Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,297,229
Community Members
 
Community Events
165
Community Groups

Waiting for API-triggered pipelines

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

1 answer

1 accepted

0 votes
Answer accepted

Hi Paul,

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

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

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']

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket

Git push size limits are coming to Bitbucket Cloud starting April 4th, 2022

Beginning on April 4th, we will be implementing push limits. This means that your push cannot be completed if it is over 3.5 GB. If you do attempt to complete a push that is over 3.5 GB, it will fail...

2,210 views 2 9
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you