Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to differentiate TFS Builds and manual builds using macros in Post build event

In TFS post build script of a .proj file I want to find whether the project build is happening through TFS triggered build or manually triggered build. Can someone suggest me how to do this using macros in Post Build event.

like image 459
Sriks Avatar asked Aug 26 '14 12:08

Sriks


1 Answers

Short answer: you can make use of the IsDesktopBuild MSBUILD property within your csproj file to differentiate between TFS and local build.

Long Answer:

Developer or Team Build?

To differentiate the build environments we have to implement a mechanism that detects in which environment the build is being executed. In other words, we need to know if we running a local build that is executed by the developer or a team build running on the build server. In fact, there are 3 different build environments we need to consider:

· Visual Studio Build – a build executed by a developer, on their own development machine inside the Visual Studio IDE

· Team Build – a build executed by TFS (manually or scheduled), on the build.

· Desktop Build – a build explicitly executed manually, on the development workstation using the command 'msbuild.exe tfsbuild.proj'.

A ‘DesktopBuild’ and a ‘TeamBuild’ are very similar in nature except that ‘DesktopBuild’ does not perform a ‘GetLatest’ function from source repository, will not ‘Label’ the source tree and will not determine the change set.

When using MSBUILD tasks (as we will use primarily in following sections), one common way to achieve this is to use the ‘IsDesktopBuild’ and ‘BuildingSolutionFile’ properties as conditions to test in the tasks.The ‘IsDesktopBuild’ property is declared in the ‘Microsoft.TeamFoundationBuild.targets’. The ‘BuildingSolutionFile’ property is declared and assigned automatically by MSBUILD.

The following table lists the values of each of these properties in each of the build environments.

Environment                    IsDesktopBuild                BuildingSolutionFile

Visual Studio Build               (empty)                         (empty)
Desktop Build                       true                            true
Team Build                         false                            true

One caveat with using the ‘IsDesktopBuild’ property is that it is not defined in many target files by default. This property will have an ‘empty’ value in a Visual Studio build, so we initialize it to a value of ‘true’ as the default value. Therefore we need to be explicitly define it in all MSBUILD target files where it will be tested.

We simply add the following element to all target files where we need to differentiate between a build on the development machine and a build on the build server (within the first section).

<IsDesktopBuild Condition="'$(IsDesktopBuild)' == ''">true</IsDesktopBuild>

Update: thank you @dbardakov. Starting VS 2012 we can use the property to find if the build is happening within Visual Studio:

BuildingInsideVisualStudio

MSDN SOURCE - for BuildingInsideVisualStudio

MSDN SOURCE

like image 109
Isaiah4110 Avatar answered Oct 07 '22 23:10

Isaiah4110