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

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?


Answers:
author: Theodora Boudale
body: Hi [@Lars_langhans](/t5/user/viewprofilepage/user-id/3814226),

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:

* <https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-repositories-workspace-repo-slug-pipelines-config-variables-post>
* <https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-repositories-workspace-repo-slug-pipelines-config-variables-variable-uuid-put>

<br />

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:

* <https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-workspaces-workspace-pipelines-config-variables-post>
* <https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-workspaces-workspace-pipelines-config-variables-variable-uuid-put>

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

* <https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-repositories-workspace-repo-slug-deployments-config-environments-environment-uuid-variables-post>
* <https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-repositories-workspace-repo-slug-deployments-config-environments-environment-uuid-variables-variable-uuid-put>

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

Kind regards,  
Theodora

comments: author: Lars_langhans
body: 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.


author: Lars_langhans
body: 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)"'
```


author: Theodora Boudale
body: 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


Tags: bitbucket-cloud

cloud