I am using Azure DevOps for some application deployment. I need to have a saved variable build number that would update every time I do a build successfully and send the apk/ipa to the store.
Right now, from what I read in the Azure documentation and other post on StackOverflow about this, I setup my scripts this way.
So, it seems to update my local variable but not my pipeline variable. I am unsure why since this is the example provided EVERYWHERE.
Sources:
- https://learn.microsoft.com/en-us/azure/devops/pipelines/process/set-variables-scripts?view=azure-devops&tabs=bash
- how to increment and save variable in azure devops pipeline
Thank you for the help!
Edit 1: Ok, so it seems that there is a variable/function called counter
. I haven’t figured out how to use it yet, but looking into it.
Edit 2:
Updated my azure-pipelines.yml
variables:
major: 1
minor: 0
patch: 0
build: $[counter(variables['patch'], 1)]
On my pipeline it looks like this
and my fastlane (ruby script ) lane looks like this
lane :tf do
`echo $major`
`echo $minor`
`echo $patch`
`echo $build` # Nothing
`echo $MAJOR`
`echo $MINOR`
`echo $PATCH`
`echo $BUILD` # Nothing
`echo $(major)` # fails
end
those show nothing.
This azure DevOps is very depressing. It says here I can do a bash call to this variable.
4
Answers
I eventually used this formula that works without having to hack the NSA and FBI to get a build number updated. It's not exactly what I wanted, but whatever, I will deal with this atm.
then using the
ENV['BUILDNUMBER']
in my ruby script, and it reads the env variable with the counter. It is the type of solution I needed, although it doesn't do exactly what I want.Variables used with macro syntax are expanded in runtime before task is executed, that is why there is value ‘1’ in last log, even when value previously was set in previous step to ‘2’.
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#runtime-expression-syntax
Try to use runtime expression instead, it is expanded in runtime
Two solutions for you.
1. Use counter expression.
counter(<prefix>, <feed>)
counter expression is for pipeline, and it has two parameters, prefix and seed. Seed is based on the prefix.
When the prefix is been set and run for the first time, the counter result will start from the feed value. But for the later run based on the same prefix, the counter result will ignore the feed value, it will be ‘last time counter result + 1’
2, Change the pipeline definition directly.
For example, I can use the below python code to get and change the variable of classic pipeline:
Write a JSON demo for you, you can import it in your DevOps and design yourself classic pipeline based on this:
Refer to this doc: Set variables in scripts
The method you used to update the Pipeline Variable value is correct.
It will not work on the current task, but the updated value can be used in the next tasks.
For example:
For the requirement about using counter expression, you can refer to my another ticket: Azure DevOps: release version