Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,457,764
Community Members
 
Community Events
176
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

Atlassian Community Events