2
votes

Please help me to get auto incremented revision number for the pipeline build task in Azure.

Let's say there is major.minor.patch.revision number. major.minor.patch is set in the version file that is stored in the source control. This allows to define software version by the source code maintainer and do not change the build task definition (or its parameter). I'd like to avoid version parameter value update before new version is going to be built.

However I'd like to get revision increased automatically each time when version builds. To make it possible to have major.minor.patch the same.

Version file is a plain text file with major.minor.patch content. I have powershell script task in the build pipeline. It read version file and store as build variable like below:

$fv = Get-Content versionFile

       $buildNumber = $env:BUILD_BUILDNUMBER
       if ($buildNumber -eq $null)
       {
           $buildIncrementalNumber = 0
       }
       else
       {
           $splitted = $buildNumber.Split('.')
           $buildIncrementalNumber = $splitted[$splitted.Length - 1]
       }

   Write-Host ("##vso[task.setvariable variable=versionFromFile]$fv.$buildIncrementalNumber")
   Write-Host ("from file $fv incremental .$buildIncrementalNumber")
   Write-Host ("##vso[task.setvariable variable=build.updatebuildnumber]$fv.$buildIncrementalNumber")

As you can see it read from versionFile and takes .N part from the BUILDNUMBER variable to get revision value. And it stores result value to the versionFromFile variable.

And I'd like to get latest version to be updated for each build automatically for the certain project. Just not to update version source file for each build that is started for each commit to the master branch.

I've tried to use build number format however the problem is that $(Rev:.r) is reseted if main part of the Build number format was changed. This means that for number format $(BuildDefinitionName)_$(SourceBranchName)_$(Date:yyyyMMdd)$(Rev:.r) Rev is started from zero each day.

$(BuildId) is an unique value that can not be cleared (started from zero)

I've tried to use the versionFromFile variable in the build number format string but seems that build number is calculated when pipeline is started and there is no way to use custom (script) variable value in the build number.

I think to create one more file, like revisionFile and store revision value in this file. However then I will need to update revisionFile on each build and commit.

Is there any other option to get build auto incremented count for the project? I've met a sample how to update build pipeline variable from the powershell script but it is linked with pipeline so having two build pipelines will produce the same value for (probably) different version.

PS There is a possibility to update build number by outputing "##vso[build.updatebuildnumber]1.2.3.4" to the log during the build but Rev value is calculated before any task have a chance to update build number.

There are Git and Tfs projects so it would be nice to have the same solution for both types of the source control.

1
You can store in a file the whole version, and in the pwoershell script increment each build only the last number (the "revision").Shayki Abramczyk
@ShaykiAbramczyk yes, but it will produce a long history for versionFile (each build will update this file) so it will be hard to distinguish between the version number changes and the ordinary build. That is why I suggested to use dedicated filesoleksa

1 Answers

1
votes

seems that there is an answer already: https://developercommunity.visualstudio.com/content/problem/240373/vsts-build-revision-does-not-increment-after-calli.html

Create a new variable: patch: $[counter('versioncounter',100)] Then, instead of using $(Rev:rrr), you would use $(patch).

I've defined two test variables:

test1: $[counter('versioncounter',1)]

test2: $[counter('versioncounter2',100)]

in the build pipeline

and here are logs:

build 1 agent1 log

[section]Starting: Prepare job Job_1 Variables:
test1: Parsing expression: Evaluating: counter('versioncounter', 1) Expanded: 1 Result: '1'
test2: Parsing expression: Evaluating: counter('versioncounter2', 100) Expanded: 100 Result: '100'

The same values for test1 and test2 are generated for another agent in this build pipeline

Then I've executed build pipeline one more time and here is the log

build 2 agent1 log

[section]Starting: Prepare job Job_1 Variables:
test1: Parsing expression: Evaluating: counter('versioncounter', 1) Expanded: 2 Result: '2'
test2: Parsing expression: Evaluating: counter('versioncounter2', 100) Expanded: 101 Result: '101'

I assume that the counter value is generated for the key from the first argument

[del]This is exactly I've asked for[/del]

upd1 I've tried to run this in the real life

And the problem is that $[counter('versioncounter',1)] is working for the build variable initialization only. I've tried to use it in the PS script as below

- powershell: |
   $fv = Get-Content versionFile
   $buildIncrementalNumber = $[counter($fv,1)]

but failed:

$[counter : The term '$[counter' is not recognized as the name of a cmdlet, 
function, script file, or operable program. Check the spelling of the name, or 
if a path was included, verify that the path is correct and try again.
At E:\buildagent\networkProxy\_work\_temp\d22e789f-bed0-465a-b447-60f634d73c38.
ps1:3 char:27
+ $buildIncrementalNumber = $[counter($fv,1)]
+                           ~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: ($[counter:String) [], ParentCon 
   tainsErrorRecordException
    + FullyQualifiedErrorId : CommandNotFoundException

Is it possible to access $counter magic from the build power shell script?

upd2 two jobs have to be used to read version from file and get incremental number using $counter magic. There is moswald answer in the #1802 issue to use two jobs.

So I've implemented it like below:

jobs:
  - job: versionJob #reads version number from the source file
    steps:
      - powershell: |
          $fv = Get-Content versionFile
          Write-Host ("##vso[task.setvariable variable=versionFromFile;isOutput=true]$fv")
        displayName: 'version from file' 
        name: setVersionStep  


  - job: buildJob # consumes version number, calculates incremental number and set version using assemblyinfo.cs
    dependsOn: versionJob
    variables:
      versionFromFile: $[ dependencies.versionJob.outputs['setVersionStep.versionFromFile'] ] # please note that spaces required between $[ and dependencies
      buildIncrementalNumber: $[ counter(dependencies.versionJob.outputs['setVersionStep.versionFromFile'],1) ] #can't use $versionFromFile here


    steps:
      - powershell: |
          Write-Host ($env:versionFromFile)
          Write-Host ($env:versionFromFile + '.' + $env:buildIncrementalNumber)
        displayName: 'version from file output' 

here is yaml variables documentation

PS: There is no need to declare $variableFromFile before jobs section