Create
cancel
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.

```
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?

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
TAGS
Community showcase
Published in Jira Service Management

Security Advisory for Jira Service Management

On October 20, 2021, Atlassian published a security advisory for Jira Service Management. The full advisory is available at this link.  We've seen a number of questions already asking for...

76 views 0 1
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