xrstokes
xrstokes

Reputation: 1

Setting a C# variable during a TFS build phase

I'm really new to C# and TFS, I've setup a basic WPF app that uses continuous integration. The problem is that some users are not updating when they should, and we have no versioning control so to speak of. What I want to do is have a variable in the code like this.

public static string BuildNumber = "";

And have it populated during the online build process. That way we can show it in the title bar and when they call, we know instantly what we are dealing with. I assume this can be done in the “process variables” section. But really need step by step help here. Just want it to have the same format as the build that is “$(date:yyyyMMdd)$(rev:.r)_$(SourceBranchName)” We are really new to this and know that we should be using proper build versions like 1.0.0.1 type thing. But are just really looking for a quick fix. So unless setting up proper versioning is really easy. Please don’t try and steer me down that path, already on a steep learning curve here.

Thanks.

Upvotes: 0

Views: 364

Answers (2)

Giulio Vian
Giulio Vian

Reputation: 8343

Caveat: I assume that you are using TFS 2015 or later and Build vNext (i.e. not using a Xaml Build).

Your question is easily solved using a Powershell Task as the first step of your build. At that point the build number is know and available in the ${env:BUILD_BUILDNUMBER} variable.

Say that the file containing the public static string BuildNumber = ""; line is in the root of your source directory and named BuildNumber.cs. Then the Powershell code is similar to the following:

$pathToBuildNumber = "${env:BUILD_SOURCESDIRECTORY}\BuildNumber.cs"
$source = Get-Content $pathToBuildNumber
$source -replace 'public static string BuildNumber = "(.*)";',$env:BUILD_BUILDNUMBER | Out-File $pathToBuildNumber

Upvotes: 1

PatrickLu-MSFT
PatrickLu-MSFT

Reputation: 51143

According to my understanding, seems you want to overwrite your Build number with variables, you could do this through powershell script by using build variables:

1) First create a Powershell script like this

$FinalVersion=Some-Function-To-Calculate-Version
$BuildDefName = $Env:BUILD_DEFINITIONNAME
Write-Host "##vso[build.updatebuildnumber]$($BuildDefName)-$($FinalVersion)"

2) In your vNext build definition, for "Build number format" set it to anything. It doesn't matter because the Build Number will be overwritten.

3) In the same vNext build definition steps, add the first step as a Powershell step, and set your script from step 1 to be executed. You can later customize if you want to pass variables in order to calculate your build number.

4) Queue your build and see the results.

Another way please take a look at this similar question: Accessing the build revision number in Powershell script during TFS build

Upvotes: 0

Related Questions