Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to update a repository variable in my pipeline

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.

# 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 '<username>/<reposlug>/pipelines_config/variables/%7B<uuid>%7D' | jq -r .value )
- echo $VERSION_NUM_NEW # Value is 2
- echo ${VERSION_NUM} # Value is 2
- 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 '<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?

1 answer

0 votes

@Daeruinwhen 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

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.

@DaeruinI 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

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Bitbucket

📣 Calling Bitbucket Data Center customers to participate in research

Hi everyone, Are you Bitbucket DC customer? If so, we'd love to talk to you! Our team wants to dive deep to understand your long-term plans regarding Bitbucket DC and Atlassian Cloud. Do you plan...

123 views 2 4
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you