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

為{幸葍}努か 提交于 2021-02-17 20:26:40

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/25505368/how-to-differentiate-tfs-builds-and-manual-builds-using-macros-in-post-build-eve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!