Reputation: 7059
I have a working yaml build pipeline where I maintain the major, minor and patch versions of my app. The revision is calculated by the pipeline with this expression:
revision: $[counter(variables['squilversionkey'], 0)]
(The key is calculated from the other variables to give something like "1.1.20-dev".)
In the same pipeline, I pass this to a script that sets the revision and combined versions (such as "1.1.20.11-dev") in some text files so that the app knows its version. So far, that works.
Ideally though, I don't want to maintain the major, minor and patch versions in the same yaml file. It's a complicated file and I don't want to see it as changed in the commit history just because the versions are bumped when the logic isn't changed.
In order to have those in a different file, I need to read those values into the pipeline though (they are no longer in the yaml). That works in principle:
Write-Output "##vso[task.setvariable variable=squilversionkey]$baseVersion"
This should give me the variable to use in the counter expression, right?
Alas, the counter expression needs to be "dynamic" (in square brackets), and those can only appear in variable definitions and conditions - not in the env sections of tasks.
Tasks can also not have variable definitions of their own, only jobs. But a new job will have forgotten the variable and it also seems a bit overkill to bother a second job.
What's a good way to do this?
Upvotes: 1
Views: 525
Reputation: 7251
I don't want to see it as changed in the commit history just because the versions are bumped when the logic isn't changed.
Since you have this requirement, then obviously you shouldn't make any changes in the repository.
The following introduces two methods of setting persistent variables for pipeline run (neither will generate commit history). You can choose one of them.
1, A suggestion is store the data in other places, for example, variable group:
And then get it from the variable group:
Also you can simply using YAML to get the variable like this:
trigger:
- none
pool:
vmImage: ubuntu-latest
variables:
- name: squilversionkey
value: 1
- name: revision
value: $[counter(variables['squilversionkey'], 0)]
- group: MyGroup
steps:
- bash: |
echo $(revision)
echo $(versionxxx)
- bash: |
echo $abc
env: #get the env of the task from variable group. Directly using the variable name in variable group is ok.
abc: $(versionxxx)
This is my variable group:
I can successfully get the value from it via the above YAML(using env section of the task can easily process the value during runtime):
2, Another suggestion is to update the variable of your pipeline(Not from YAML), and then use the variable from the pipeline.
I mean the variable in this place:
The variable in this place will not affect the content of the repository, so it will also not generate unrelated commit history in repository.
For example, you made a active variable:
Using this REST API to get the variable of the pipeline definition:
Build Pipeline Definitions - Get
Result on my side:
And use this REST API to update the variables of the pipeline.
Build Pipeline Definitions - Update
Upvotes: 1