Increment variable value in TFS build +1
Asked Answered
D

3

14

I have a Microsoft Visual Studio Team Foundation Server (Version 15.117.26714.0) with predefined variable $(ProjectBuildNumber).

Is there any way to increment, during build process, value of variable with minor build number by +1?

$(ProjectBuildNumber)  =   663

So, that on next build it will be:

$(ProjectBuildNumber)  =   664

enter image description here

enter image description here

Damage answered 14/5, 2018 at 12:31 Comment(0)
C
9

You can't reference variables in the build number of the Build Definition. But what you can do is override the build number in the build itself. You can either use a magic log command or use my VSTS Variables Task to set the Build.BuildNumber in the build itself. The Variables Task does expand variable references. You could probably just set the value to the current value to get it expanded.

enter image description here

To issue the log command yourself use a batch script, PowerShell or bash to output the following specific string to the console:

##vso[build.updatebuildnumber]build number

Update build number for current build. Example:

##vso[build.updatebuildnumber]my-new-build-number

Minimum agent version: 1.88

source: https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md

An alternative option is to use the $(Rev) option:

Build.BuildNumber = 1.1.$(Rev:.r)  

That will automatically increase the variable each time the build runs.

To update a variable in a Build Definition use yet another extension:

enter image description here

These things combined should be able to get what you want.

Ced answered 14/5, 2018 at 12:46 Comment(3)
So, if I understand you correctly, I cannot change variable value permanently, using build tasks? Just in project build context? The $(Rev) is not a variant, because there are multiple projects in VSTS, and every time I start project, $(Rev) value increasing.Damage
No Rev just generates a unique version number. If you pre-pend you version number with the name of the project it will generate unique versions.Ced
To change the variable permanently, use the extension here: marketplace.visualstudio.com/…, but you'll also need to run the Set Variable task to make the build itself take on the correct value.Ced
H
5

In the variable section,

set the value of ProjectBuildNumber to $[counter('', 663)].

This will queue build starting with 663 as ProjectBuildNumber and increments by 1 for the subsequent queue of builds.

Halfway answered 5/6, 2019 at 8:9 Comment(2)
This also works for releases and re-deploys of releases!Ogpu
See the Microsoft documentation for counter.Iapetus
M
0

Unfortunately counter function (Expressions) is not available in TFS 2018. In this old version the best solution for me is to use a PowerShell script as the first Task of the build. You can than have your parameter

$(ProjectBuildNumber)

as an input argument, and place this inline script:

$ProjectBuildNumber=$args[0]
$ProjectBuildNumber++
Write-Host "##vso[task.setvariable variable=ProjectBuildNumber;]$ProjectBuildNumber"

After this Task you can use your incremented ProjectBuildNumber variable in all subsequent Tasks.

Modernism answered 28/11, 2022 at 13:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.