How to add an environment variable to an specific environment using bitbucket api ?

Marcelo_Anabuki October 11, 2019

Screen Shot 2019-10-11 at 20.43.38.png

This image is what I want to do in the api.

I want to add a variable to an already existent environment on a repository using the api. This is not a repository "global" variable. This is a specific environment variable.(On this environment I am using staging)

From the documentation I found this url that should do the trick, but I have no idea how to use it to achieve what I want.

/2.0/repositories/{workspace}/{repo_slug}/environments/{environment_uuid}/changes/

 

4 answers

2 accepted

0 votes
Answer accepted
Philip Hodder
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 11, 2019

Hello,

We've added endpoints for interacting with deployment variables to our public API.

You can find the documentation for them here: https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/deployments_config/environments/%7Benvironment_uuid%7D/variables

Thanks,

Phil

0 votes
Answer accepted
Peter Plewa
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
October 13, 2019

Hi @Marcelo_Anabuki,

You can add Deployment Variables through API as follows with an OAuth2 access token. 

curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer {access_token}" \
-d '{"key": "key", "value": "value", "secured": false }' \
https://api.bitbucket.org/internal/repositories/{repo_slug}/deployments_config/environments/{environment_uuid}/variables/

This is internal API and thus not documented, out of curiosity what is the use case for adding variables this way over the UI?

Thanks,

Peter

Marcelo_Anabuki October 14, 2019

Hi @Peter Plewa , 

Thanks for the help. I will try it out.

Our use case is that we are automating a few repository configurations. 

We are using bitbucket pipelines and we need to set some secrets to make our deploy work, adding/updating it manually takes a lot of time and is prone to error.

Marcelo_Anabuki October 14, 2019

Hey @Peter Plewa 

I was getting some 409 but is working fine now. Thanks.

 

Peter Plewa
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
October 14, 2019

409 can happen if you are trying to submit the same variable name to the same environment, which needs to be unique.

Thomas Barrett October 31, 2019

I would very much like to see this in v2.

Our different environments deploy to different hosts. We have been manually configuring this with key-value pairs in the environment variables.

We use the v2 API to instantiate new repositories, set up the branches, the branch permissions, turn on pipelines etc - so being able to set the environment variables would be useful.

Philip Hodder
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 11, 2019

These endpoints have been added to the V2 API: https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/deployments_config/environments/%7Benvironment_uuid%7D/variables

As our internal APIs do not have a deprecation policy. I suggest you migrate to the 2.0 API as soon as you're able, so you don't run into any issues in the future.

Thanks,

Phil

1 vote
mark-studer
Contributor
October 26, 2019

I've been working on a PowerShell module to front the Bitbucket API for automation.  I've added cmdlets for working with the deployment variables in the latest version.  

 

https://www.powershellgallery.com/packages/Atlassian.Bitbucket/0.15.1

0 votes
Deleted user October 23, 2019

@Peter Plewa , I'm trying to do the same thing as Marcelo, but am unable to get this working.

I keep getting a Resource not found error, and the detail is "There is no API hosted at this URL.\n\nFor information about our API's, please refer to the documentation at: https://developer.atlassian.com/bitbucket/api/2/reference/".

I'm trying to hit this through Postman, but curl isn't working for me either. Any suggestions?

Marcelo_Anabuki October 23, 2019

Hey Allen,

 

Send us the curl command line that you are using.

I spent some time trying to make it work so I can probably figure it out if I see the command.

Deleted user October 23, 2019

Hey Marcelo, 

It looks pretty much exactly like what Peter had, I've changed the data in here (for obvious reasons) but this is effectively what the command looks like:

curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer REDACTED" \
-d '{"key": "test", "value": "value", "secured": false }' \
https://api.bitbucket.org/internal/repositories/my-service/deployments_config/environments/{123456g8-99k5-j98k-82h4-b6fb67e9eeb1/variables/

mark-studer
Contributor
October 26, 2019

@[deleted] - It looks like you are missing the team portion from the path before the repo name...

You can also use my PowerShell module which I've added support for variables.

https://www.powershellgallery.com/packages/Atlassian.Bitbucket/0.15.1

Deleted user November 12, 2019

Thank you @mark-studer that got me through my issue

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events