Vasyl Stepulo
Vasyl Stepulo

Reputation: 1603

Increment variable value in TFS build +1

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

Upvotes: 14

Views: 6975

Answers (3)

Stefano Maioli
Stefano Maioli

Reputation: 86

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.

Upvotes: 0

Jabez
Jabez

Reputation: 905

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.

Upvotes: 5

jessehouwing
jessehouwing

Reputation: 114927

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.

Upvotes: 9

Related Questions