Override repository variable in pipeline

Mikhail Sapozhnikov January 15, 2021

 

 

I have repository variable with name SCRATCH_TO_DELETE. How i can override his value during pipeline execution? I need to use updated value in another pipeline like argument. Thank u

2 answers

1 accepted

1 vote
Answer accepted
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 18, 2021

Hi Mikhail and welcome to the community!

You can override the value of a repository variable during the build, if the variable's name is SCRATCH_TO_DELETE you can add a command like the following in your yml file:

SCRATCH_TO_DELETE="another value"

Could you please give some more info on your use case regarding using it in another pipeline? Are you triggering another pipeline from your build with the pipe https://bitbucket.org/atlassian/trigger-pipeline/src/master/ and need to use the updated value there? Or are you doing something different?

It would be useful to have some more info to see if this is going to work for you, or if there is another way of achieving what you want. If you change the value of a repo variable during the build, this modified value will apply only for the step where you are making this change.

Kind regards,
Theodora

Mikhail Sapozhnikov January 18, 2021

 

Thanks for the answer. But this is not what I want. I need that after some command, I see the already changed ("another value 1") value in the repository variables

 

image.pngimage.pngimage.png

Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 19, 2021

Hi Mikhail,

Thank you for your reply.

This is possible with an API call during the build. I'm not sure how familiar you are with our API, so I'll add detailed instructions:


1.
First, generate an app password for your Bitbucket account by going to your avatar (bottom left corner) > Personal settings > App passwords.

For Permissions, you can select only Pipelines Read, Write, Edit variables.


2.
Go to the Repository settings > Repository variables and create two variables for your Bitbucket username and the app password, named e.g. Bitbucket_Username and Bitbucket_App_Password

Make sure that the one for the app password is secured.


3.
Run a curl command like the following from your machine to get the UUID of the variable you want to edit:

curl -X GET -u Bitbucket_Username:Bitbucket_App_Password https://api.bitbucket.org/2.0/repositories/<workspace-id>/<repo-slug>/pipelines_config/variables/

replace Bitbucket_Username, Bitbucket_App_Password, <workspace-id>, <repo-slug> in the call with the appropriate values

The output will give you info for all repository variables, take note in the output the UUID of the specific variable you are interested in, let's say it is

{9tf745d2-9566-23e7-c3b5-u012db07092e}

4. You can then add an API call like the following in your yml file to change the value of this variable during the build:

curl -X PUT -u ${Bitbucket_Username}:${Bitbucket_App_Password} https://api.bitbucket.org/2.0/repositories/<workspace-id>/<repo-slug>/pipelines_config/variables/%7B9tf745d2-9566-23e7-c3b5-u012db07092e%7D --header "Content-Type:application/json" --data '{"value":"another value"}'

Please note that the curly brackets of the UUID are encoded in the URL above in order for this to work, { is replaced with %7B and } with %7D

One more thing:
After you run a build with such an API call, you'll be able to see the updated value in Repository settings > Repository variables.
Please note though that this updated value will be applicable in subsequent steps and subsequent builds.
For the step where you are running this API call, the variable will still have the old value, even after the API call, as this was initialised when the step started.
If you want the updated value during the step where you run the API call as well, you can add an additional command after the API call

SCRATCH_TO_DELETE="another value"

 If you want to check more info on the API endpoints I mentioned, you can find it here:

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

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

I hope this helps, please feel free to let me know if you have any questions.

Kind regards,
Theodora

Mikhail Sapozhnikov January 20, 2021

 

 

Thank you very much, its a good solution!

Like Theodora Boudale likes this
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 20, 2021

Good to hear, you're very welcome!

Like Mikhail Sapozhnikov likes this
0 votes
Hugo Costa April 26, 2021

Capture.PNGHi   @Theodora Boudale,

After doing till the step 3. it doesn't show all the variables (image in attach)

before i used curl -s -X GET -H Bitbucket_Username:Bitbucket_App_Password https://api.bitbucket.org/2.0/repositories/..../...../pipelines_config/variables/

and it worked but now recive an error {"type": "error", "error": {"message": "Resource not found"}}

Can you please help? The idea is to find the COMMIT variable to set with $BITBUCKET_COMMIT so that I can send the delta.

 

Thank you in advance.

Hugo Costa

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events