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

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

Louis Willcock May 6, 2018

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

3 votes
Seth Miller December 14, 2020

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

3 votes
davina
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 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

Danny Sievers September 23, 2018

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
Atlassian Team members are employees working across the company in a wide variety of roles.
September 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"
}
}
}
Danny Sievers September 24, 2018

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 January 3, 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 January 8, 2019

solved

URL must contain {UUID}

Matjaz Jurecic April 9, 2019

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
AUG Leaders

Atlassian Community Events