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

Is there a way to set a secret variable for pipeline via curl from outside without UI?

Lars_langhans April 28, 2022

I have a lot of lookups in my Ansible playbook. This works like a charm on a shell script form my local computer but inside a bitbucket pipeline I have not the rights to access aws secret manager so the idea was to put all lookups in a local script and set it via curl command in the bitbucket pipeline as new variables. So bitbucket pipelines use only secret variables to run with Ansible. If there are changes in the variables I can set it from outside without click through the error-prone process to set the variables new. We talk about over 50 variables which will change regulary.

Is there a simple way with curl to set such pipeline variables?

 

1 answer

1 accepted

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

Hi @Lars_langhans,

There are API endpoints that you can use to either create a new variable or update the value of an existing variable (also secured ones).

If these are Repository variables, the endpoints are the following:


An example with curl for each of the endpoints:

curl -u BitbucketUsername:AppPassword -X POST https://api.bitbucket.org/2.0/repositories/WORKSPACE-ID/REPO-SLUG/pipelines_config/variables/ --header 'Accept: application/json' --header 'Content-Type: application/json' --data '{"key": "mySecuredVariable", "value": "123456","secured": true}'
curl -u BitbucketUsername:AppPassword -X PUT https://api.bitbucket.org/2.0/repositories/WORKSPACE-ID/REPO-SLUG/pipelines_config/variables/%7BVARIABLE-UUID%7D --header 'Accept: application/json' --header 'Content-Type: application/json' --data '{"value": "987654"}'

For the second example with curl (updating an existing variable's value), you will need to specify the variable UUID in the API URL. You can use the following endpoint to get all variables and find the UUID of the one you want to update

https://api.bitbucket.org/2.0/repositories/WORKSPACE-ID/REPO-SLUG/pipelines_config/variables

 

If you are using workspace variables, the endpoints are the following:

 

If you are using deployment variables, the endpoints are the following:

If you have any questions, please feel free to let me know!

Kind regards,
Theodora

Lars_langhans May 4, 2022

Hi,

thank you very much. Create a new variable works like a charm. List variables works also.

After some work I decided myself to use deployment environments instead of normal variables. For this I need a environment_uuid and within the UI I can't see this uuid.

Maybe within the API:

I found "List environments"

And this offers me the environment_uuid.

Lars_langhans May 6, 2022

To list all environment uuids in a simple online list.

AUTHENTICATION=<username>:<app password>
curl -u ${AUTHENTICATION} --request GET \
--url "https://api.bitbucket.org/2.0/repositories/${WORKSPACE_ID}/${REPO_SLUG}/environments/" \
--header 'Accept: application/json' | jq '.values[] | "\(.category.name)=\(.uuid)"'
Like Theodora Boudale likes this
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 6, 2022

Hi Lars,

Thank you for the updates and you are very welcome.

You are also right about the API for listing the environments, the endpoint is this one

https://developer.atlassian.com/cloud/bitbucket/rest/api-group-deployments/#api-repositories-workspace-repo-slug-environments-get

If you need anything further, please feel free to reach out!

Kind regards,
Theodora

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events