How to update a repository variable in my pipeline
Asked by Daeruin:

I am using a repository variable to store a patch version number for a certain branch. In my pipeline script, I use the Bitbucket API to get the value of the variable, increment it, and update the value of the variable. However, if I attempt to use the variable after that point in the script, it seems to retain its old value.

\`\`\`  
script:  
# Get value of VERSION_NUM from BitBucket API and assign it to VERSION_NUM_NEW  
- VERSION_NUM_NEW=$( curl -X GET -u "\<username\>":"\<password\>" --url '[https://api.bitbucket.org/2.0/repositories/\<username\>/\<reposlug\>/pipelines_config/variables/%7B\<uuid\>%7D]()' \| jq -r .value )  
- echo $VERSION_NUM_NEW # Value is 2  
- echo ${VERSION_NUM} # Value is 2  
# Increment VERSION_NUM_NEW  
- ((VERSION_NUM++))  
- echo $VERSION_NUM_NEW # Value is 3  
- echo ${VERSION_NUM} # Value is 2  
# Use BitBucket API to update VERSION_NUM_1_12 to new version number  
- curl -vX PUT -u "\<username\>":"\<password\>" --url '[https://api.bitbucket.org/2.0/repositories/\<username\>/\<reposlug\>/pipelines_config/variables/%7B\<uuid\>%7D]()' -H 'Content-Type:application/json' -d "{\\"key\\":\\"VERSION_NUM\\",\\"value\\":\\"$VERSION_NUM_NEW\\"}"  
- echo $VERSION_NUM_NEW # Value is 3  
- echo ${VERSION_NUM} # Value is still 2! What??

\`\`\`

Later, when I log into Bitbucket to check the value of VERSION_NUM, it has indeed been updated as I expect. It just seems that inside the script, it somehow retains its old value.

I have a workaround, but it bothers me that I can't seem to get the correct value of my variable while in the script. Am I doing something wrong?


Answers:
author: Halyna Berezovska
body: [@Daeruin](/t5/user/viewprofilepage/user-id/113074)when you have updated the value - you will have value changed either in PUT response or in response of NEW get request to the variables.

This is the question of bash script you're using - you re not overriding $VERSION_NUM variable in local script section.

So you have to update it somehow. BUT be awarte that if variable is secured , it will not be show in API responses.

Also, we have embedded variable BITBUCKET_BUILD_NUMBER that is global variable that represents your build number. Perhaps, it will be also useful for you to know.

Regards, Galyna

comments: author: Daeruin
body: I am updating ${VERSION_NUM} on the third to last step of my script with a PUT call. I can confirm that the value is in fact getting updated at some point after the script runs. I can see the updated value when I check the value manually in my repository settings. But when I try to get the value after the PUT statement in my script, the value does not seem to be changed.

I was previously using BITBUCKET_BUILD_NUMBER, but this is no longer sufficient. I need to update version numbers independently for different branches of my code.


author: Halyna Berezovska
body: [@Daeruin](/t5/user/viewprofilepage/user-id/113074)I guess, this is due to local cache. If you really need to update VERSION_NUM var in the exact same step - I think, you can parse the response of PUT call.

Or explain us your use case, and we can help constructing well-written pipeline.

Regards, Galyna


Tags: bitbucket-pipelines