We use TFS 2010.
There are a couple of projects with deployment steps which must know whether they are running on a dev machine or on the TFS build agent.
Right
You have a few options:
'$(BuildingInsideVisualStudio)' != ''
'$(TeamBuildConstants)' != ''
(supported in team Build 2008)'$(IsDesktopBuild)' == 'false'
'$(tf_build)' != ''
(supported in recent versions of Azure Pipelines)You can check either one to detect the context the task has been executed in. If both don't evaluate then MsBuild has been called from the commandline or some other process.
When calling MSBuild from the command line, you can pass/overwrite properties like this:
# Simulate Visual Studio build
. msbuild.exe Project.csproj /p:BuildingInsideVisualStudio=true [...]
# Custom property
. msbuild.exe Project.csproj /p:MyCustomProperty=true [...]
Is use these to check them in my post/afterbuild events.
I thought to provide my own answer updated to 2020.
The way we do it is by checking an environment variable, because TFS (now Azure DevOps) exposes build variables through the environment. When tested in msbuild code they look the same build variables passed on the msbuild command line, but we also employ powershell scripts, which rely only on the environment.
Also, I would sometimes like to distinguish between the Build and the Release pipeline, because in the classic Release pipeline (as opposed to yaml, which we do not have on prem) some things do not work the same (like vsts logging commands)
So, here is what we are using:
BUILD_BUILDNUMBER
- exists in both build and release, because all of our releases include a build artifactRELEASE_ENVIRONMENTNAME
- exists only in the classic release pipelineTF_BUILD
- not sure if exists in release, definitely in buildI would not bother with BuildingInsideVisualStudio
, because it is false when build with msbuild on the command line, yet it is not a build on the CI server.
I have TFS2012 and use this:
<IsTfsServerBuild Condition=" '$(IsTfsServerBuild)' == '' ">false</IsTfsServerBuild>
<IsTfsServerBuild Condition=" '$(BuildingInsideVisualStudio)' != 'true' AND '$(BuildUri)' != '' ">true</IsTfsServerBuild>