How do I keep Build parameters alive in TFS Build (TFS server 2017 and build agent 2013)

287 views Asked by At

I'm trying to figure out why my builds are failing, so in a .proj that the build calls I added:

<Message Text="System Drive: $(SystemDrive)"/>
<Message Text="BuildAgtentId: $(BuildAgentId)" />
<Message Text="BuildDefinitionPath: $(BuildDefinitionPath)" />
<Message Text="SourceDir: $(SourceDir)" />
<Message Text="SrcDir: $(SrcDir)" />
<Message Text="BuildDefinitionPath: $(BuildDefinitionPath)." />
<Message Text="BuildDefinitionId: $(BuildDefinitionId)." />
<Message Text="TF_BUILD_BUILDDIRECTORY: $(TF_BUILD_BUILDDIRECTORY)" />

Now in doing this I assumed that $(SourceDir) would resolve to C:\Builds\$(BuildDefinitionId) since that's what I have in my Working Directory setting for my agent. I also set a variable in the MSBuild Parameters:

/p:SrcDir=$(SourceDir)  

But when the .proj runs here's my output:

System Drive: C:
BuildAgtentId: 
BuildDefinitionPath: 
SourceDir: 
SrcDir: $(SourceDir)
BuildDefinitionPath: .
BuildDefinitionId: .
TF_BUILD_BUILDDIRECTORY: .

So it seems as only $(SystemDrive) persists during the build. And my parameter set in the MSBuild args doesn't even seem to be set correctly (SrcDir). How can I access these variables during the build so I know where my files will be? I need to call some other programs (i.e. attrib) and pass in the location of my files and nothing seems to persist.

Based on this article I thought for sure that TF_BUILDDIRECTORY would work but it doesn't.

I then tried using this article that says that this should work:

<GetBuildProperties TeamFoundationServerUrl="$(TFSTeamFoundationServerUrl)" BuildUri="$(BuildUri)">
  <Output TaskParameter="BuildDirectory" PropertyName ="TFSBuildDirectory" />
  <Output TaskParameter="TeamProject" PropertyName="TFSTeamProject" />
</GetBuildProperties>

<Message Text="BuildLocation: $(TFSBuildDirectory)." />
<Message Text="TeamProject: $(TFSTeamProject)."/>

And while TeamProject works, BuildDirectory does not. So I get this:

BuildLocation: .
TeamProject: MDRVIDEOTOUCH.
2

There are 2 answers

1
Daniel Mann On

You need to use the XAML build environment variables.

Some of those values aren't available, but you can get some of them:

  • TF_BUILD_BUILDDIRECTORY The build agent working directory. For example: C:\Build\BuildBot3\CoolApp\CIBuild.
  • TF_BUILD_BUILDNUMBER The build number of the build. For example: CIBuild_20130613.6.
  • TF_BUILD_SOURCESDIRECTORY The sources sub-directory of the build agent working directory. This directory contains your source code. For example: C:\Build\BuildBot3\CoolApp\CIBuild\src.
3
starian chen-MSFT On

You need to pass the value to MSBuild task through MSBuild arguments.

  1. Define properties in PropertyGroup section of your project file, for example <PropertyGroup><SourceDir></SourceDir><BuildDefinitionName></BuildDefinitionName></PropertyGroup>
  2. Specify MSBuild arguments: /p:SourceDir="$(TF_BUILD_BUILDDIRECTORY)" /p: BuildDefinitionName="$(TF_BUILD_BUILDDEFINITIONNAME)"

On the other hand, there are some variables in MSBuild, you can check this thread: How can I get current directory in msbuild script?