-->

Increment variable value in TFS build +1

2020-08-11 10:55发布

问题:

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

回答1:

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.

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:

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



回答2:

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.