Custom TFS Enviroment Variable doesn't read $(

2020-02-07 03:52发布

I want to use a custom tfs variable like this:

MergedVersion: $(BuildVersion.Major).$(BuildVersion.Minor).$(Date:yy)$(DayOfYear)$(Rev:.r)

My problem is that $(Date), $(Rev:r) and $(DateOfYear) don't work outside the BuildNumberFormat-Settings.

My result is:

invalid version string: '1.0.$(Date:yy)$(DayOfYear)$(Rev:.r)'.

While with the buildnumberformat like shown here - works correctly:

enter image description here

Result $(Build.BuildNumber) is MyBuildName_1.0.18004.15

3条回答
聊天终结者
2楼-- · 2020-02-07 04:27

Some tokens are only available in the Build number format section, such as $(Date), $(Rev:r) and $(DateOfYear) you mentioned here. See Build definition options

As a workaround, to use $(Rev:r)you can set the build number format as $(Rev:r), then use the $(Build.BuildNumber) variable in your tasks.

To use $(Date:yy)$(DayOfYear), you can set the variables via PowerShell task as ChamindaC mentioned above.

  1. Add a PowserShell task in you build definition
  2. Copy and paste below script and save it as *.ps1 file
  3. Check in the PS file, then run the PS file in PowerShell task

    $time=$(Get-Date -Format 'yy') # you can set the date format based on your requirement $doy = (Get-Date).DayofYear Write-Host "##vso[task.setvariable variable=Date]$time" Write-Host "##vso[task.setvariable variable=DayOfYear]$doy"

Then you can use the variables $(Date) and $(DayOfYear) in other build tasks.

enter image description here

查看更多
Animai°情兽
3楼-- · 2020-02-07 04:27

Use following script in a PowerShell Task in your build definition

$date=$(Get-Date -Format 'yy');
Write-Host "##vso[task.setvariable variable=Today]$date"

Then you can use $(Today) in your subsequent build tasks. However, usage like $(Today:yy) with format is not possible as it is supported only in build number format.

查看更多
smile是对你的礼貌
4楼-- · 2020-02-07 04:43

Building on top of @ChamindaC and @Andy Li-MSFT's answers, and using Peter Groenewegen's Xpirit Run Inline Powershell and Azure Powershell build extension, I was able to retrieve the $(rev:r) from the build number:

How to extract $Rev from Build.BuildNumber

In the following MSBuild task I reference that as $(Revision).

$(Revision) available in the next task

查看更多
登录 后发表回答