0

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.
Ben_G
  • 770
  • 2
  • 8
  • 30

2 Answers2

0

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.
Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
  • Thanks Daniel, but as I just updated the question above I tried TF_BUILD_BUILDDIRECTORY and that appears to be blank too (unless I'm referencing it wrong. I'm using $(TF_BUILD_BUILDDIRECTORY) - right? – Ben_G Sep 29 '17 at 20:33
0

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?

starian chen-MSFT
  • 33,174
  • 2
  • 29
  • 53
  • Thanks, but that doesn't work. I set /p:SourceDir="$(TF_BUILD_BUILDDIRECTORY)" in the build definition and then in the .proj file when I the values I get: InitVariables: SourceDir: $(TF_BUILD_BUILDDIRECTORY). So the TF_BUILD_BUILDDIRECTORY isn't being translated - it's being taken as a literal. – Ben_G Oct 02 '17 at 18:55
  • @Ben_G You need to use SourceDir in MSBuild directly, for example: ``. (The $(TF_BUILD_BUILDDIRECTORY) is used in TFS build activity) – starian chen-MSFT Oct 03 '17 at 03:01
  • @Ben_G What's the result after trying it? – starian chen-MSFT Oct 09 '17 at 09:26