Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

run bitbucket pipeline with python

AXA
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 18, 2022

I am trying to run my bitbucket pipeline ```branches/main``` with this script. Currently, when I run it, I see no output. No error/success message. When I check on bitbucket, the pipeline has not run. How can I further debug this?
```
import requests
headers = {
'Content-type': 'application/json',
}

data = """
{"target": {
"ref_type": "branch",
"type": "pipeline_ref_target",
"ref_name": "main",
"selector": {
"type": "branches",
"pattern" : "main"
}
},
"variables": [
{
"key" : "build_number",
"value" : "3"
},
{
"key" : "workspace",
"value" : "My Name"
}]
}
"""

response = requests.post('https://api.bitbucket.org/2.0/repositories/workspaceidmyname/{f567890098765456789234560}/pipelines/', headers=headers, data=data, auth=(username, password))
print(response.text)
```

Am I using the wrong selector or name? This is how my ```bitbucket-pipelines.yml```file looks like:
```
image: atlassian/default-image:3

pipelines:
default:
- step:
name: 'default'
script:
- echo "Your security scan goes here..."
branches:
main:
- step:
name: 'Validate'
script:
- echo "main branch's pipeline"
```

For the authentication, I used an "app password".

1 answer

0 votes
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 20, 2022

Hi @Azmah Aaban,

You don't need to use a selector, since there is a definition for the branch main in your bitbucket-pipelines.yml file. If you run this call with target main, the pipeline for the main branch will run.

The "variables" part in the data is to be used only with custom pipelines that have variables defined in the bitbucket-pipelines.yml file.

Can you try running the call with the following data?

{
"target": {
"ref_type": "branch",
"type": "pipeline_ref_target",
"ref_name": "main"
}
}

I'm not very familiar with Python so I'm not sure if the rest of the details are correct, but an example with curl which should work is the following:


curl -X POST -is -u BitbucketUsername:AppPassword \
-H 'Content-Type: application/json' \
https://api.bitbucket.org/2.0/repositories/workspace-id/repo-slug/pipelines/ \
-d '
{
"target": {
"ref_type": "branch",
"type": "pipeline_ref_target",
"ref_name": "main"
}
}'

The values in bold are the ones that need to be replaced with the respective ones for your account, workspace, and repo.

Kind regards,
Theodora

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events