0

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.

  • Are you looking for [Build variables](https://learn.microsoft.com/en-us/vsts/build-release/concepts/definitions/build/variables?view=vsts&tabs=batch)? – Camilo Terevinto May 01 '18 at 02:37
  • I think you are looking for something along these lines? The article is a bit dated, but this should help with you narrow down your google search keywords https://www.richard-banks.org/2007/07/versioning-builds-with-tfs-and-msbuild.html – Svek May 01 '18 at 03:38
  • We use the AssemblyInfo.cs to put those values in before compilation. There should be multiple ways do do just that (simple replacement, there are build steps in the store). I can elaborate - if required – Dennis Kuypers May 01 '18 at 04:18
  • Maybe even better add the sha (if using git) so that you can refer to the actual commit that produced this. Comes in handy sometimes – Dennis Kuypers May 01 '18 at 04:20
  • There's a build task that we use for updating AssemblyInfo that can also add in any custom attributes (which would be written out as `[assembly: YourAttribute(...)]`). It can use build variables, including your build number. You would need to define a attribute type to hold your build number and then use reflection to query that value within your application. https://github.com/BoolBySigma/UpdateAssemblyInfo – pinkfloydx33 May 01 '18 at 08:44
  • Which version of TFS are you using? – PatrickLu-MSFT May 02 '18 at 07:39
  • @xrstokes, any update on this issue, have you figured it out? – PatrickLu-MSFT May 18 '18 at 01:53
  • Giulio Vian gave the right answer. – xrstokes May 19 '18 at 04:47
  • @xrstokes Glad to hear the issue solved, appreciate for a [marking his reply as an answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) which will also helps others in the community. – PatrickLu-MSFT May 24 '18 at 01:52

2 Answers2

1

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
Giulio Vian
  • 8,248
  • 2
  • 33
  • 41
0

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

PatrickLu-MSFT
  • 49,478
  • 5
  • 35
  • 62