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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

I want to change bitbucket pipelines environmental variables with the API, how do I do this?

So I've been creating an automated pipeline and require the ability to change environmental variables in bitbucket pipelines programatically, I have looked at the various pieces of documentation for this and can't make heads or tails of how to actually go about this, I would consider this to be a fairly reasonable use case, I'm not building an application on top of the API, I just want to POST new variables to my pipeline, can you please give me a detailed guide on how to do this, I've been looking through this stuff for days now and have made barely any progress.

2 answers

This is the worst API I've ever used and oh boy have I used some bad ones.

3 votes
davina Atlassian Team Aug 16, 2018

Hi @Louis Willcock

If you want to create new variables, you can use the API POST call

https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/pipelines_config/variables/#post

An example payload

{
key: "foo",
value: "bar",
secured: false
}

If you want to change an existing environment variable you can use the API PUT call on a specific variable uuid (this does require you to know the variable uuid) you want to update

https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/pipelines_config/variables/%7Bvariable_uuid%7D#put

An example payload

{
key: "foo",
value: "baz",
}

 If you need to set up authentication to make these calls, you can follow the guide here https://developer.atlassian.com/bitbucket/api/2/reference/meta/authentication

 

Hope this helps!

Davina

Hey @davina,

I am trying to update pipeline environment variables via the API PUT schema you have specified above and am getting a 400 - Bad Request error. I am making the request with Python as follows:

url = 'https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/pipelines_config/variables/{variable_uuid}'
headers
= {'Content-Type': 'application/json'}
payload = {
"key": {environment_variable_name},
"value": {updated_value}
}

r = requests.put(url, json=payload, auth=('{USERNAME}', '{PASSWORD}'), headers=headers)

I have previously gotten the variable_uuid via the API. It seems like it does not like the payload I am providing. I have also tried including "uuid": {variable_uuid} and "secured": {boolean} as part of the payload for completeness sake according to the docs stated above. I have also ensured that the environment variable exists prior to update.

Any insight would be much appreciated, thanks!

Like Umar Hussain likes this
davina Atlassian Team Sep 24, 2018

Hmmm that is strange, what you've written looks correct. Is there any other information in the error? for example if your json is malformed you will get a 400 Bad Request with 

{
"error": {
"message": "An error occurred parsing the JSON.",
"data": {
"key": "rest-service.request.invalid-json"
}
}
}

I wish there was, here is the entire response I receive:

{
"error": {
"message": "Bad request",
"data": {
"arguments": {
"spanId": "d3759f902c1d0268",
"parentSpanId": "c58f6fef99732713",
"traceId": "c58f6fef99732713"
},
"key": "Bad Request"
},
"detail": "HTTP 400 Bad Request"
}
}

Thanks again for your help on this! 

Deleted user Jan 03, 2019

Hello,

any news here? I have the same problem:

 

curl -X PUT \
https://api.bitbucket.org/2.0/repositories/XX/YY/pipelines_config/variables/UID \
-H 'Authorization: Basic %code%' \
-H 'Content-Type: application/json' \
-d '{
"value": "xxx",
"key": "%NAME%"
}'

 

response:

{
"error": {
"message": "Bad request",
"data": {
"arguments": {
"spanId": "49a845225e3881ad",
"parentSpanId": "28aa2925157e7c3f",
"traceId": "28aa2925157e7c3f"
},
"key": "Bad Request"
},
"detail": "HTTP 400 Bad Request"
}
}
Deleted user Jan 08, 2019

solved

URL must contain {UUID}

How can it contain  {UUID} if you are creating a new variable?

Like # people like this

Suggest an answer

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

New improvements to user management in Bitbucket Cloud 👥

Hey Community! We’re willing to wager that quite a few of you not only use Bitbucket, but administer it too. Our team is excited to share that we’ll be releasing improvements throughout this month of...

3,709 views 10 16
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