Team Foundation Build environment variables

You can use the TF_BUILD environment variables to get key bits of data that you need for your build process logic. For example, you can get the path to the source folder or the path to the folder that contains the outputs you want to drop.

  • TF_BUILD environment variables

  • Use environment variables in MSBuild

  • Use environment variables in programs or scripts

  • Use environment variables in a custom build process

TF_BUILD environment variables

  • TF_BUILD Set by TFBuild to True so that you can make parts of your script conditional, based on whether or not your script is being run by TFBuild.

  • TF_BUILD_BINARIESDIRECTORY The directory from which the build process copies output files to the staging location. This directory is a sub-directory of the build agent working directory. For example: C:\Build\BuildBot3\CoolApp\CIBuild\bin.

    TFBuild automatically creates this directory and puts the build outputs in it unless you set Output location to AsConfigured. In this case, you can use your scripts to copy the files you want to drop to this directory.

    Note

    Your script should also be able to handle creating or cleaning this directory as needed. For an example, see Run a script in your build process.

  • TF_BUILD_BUILDDEFINITIONNAME the name of the build definition.

  • 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_BUILDREASON The build reason for the build. For example: IndividualCI.

  • TF_BUILD_BUILDURI The URI to the build. For example: vstfs:///Build/Build/35.

  • TF_BUILD_COLLECTIONURI The URI to the team project collection. For example: http://fabrikam-2:8080/tfs/DefaultCollection (You must have installed Visual Studio 2012 Update 2 or a subsequent version to use this environment variable.)

  • TF_BUILD_DROPLOCATION The location of the drop. An example of a drop to a UNC path: \\fabrikam-1\Drops\CIBuild\CIBuild_20130613.6. An example of a drop to the Team Foundation Server: #/11/drop.

  • TF_BUILD_SOURCEGETVERSION The version (TFVC changeset or Git commit) being built.

  • 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.

    For an example, see Run a script in your build process.

  • TF_BUILD_TESTRESULTSDIRECTORY The test results sub-directory of the build agent working directory. For example: C:\Build\BuildBot3\CoolApp\CIBuild\tst.

Use environment variable data in MSBuild

When you use MSBuild to compile your code (either via the default template or in a custom build process using the MSBuild activity), you can pass in arguments to control how it works. Also, some code projects include special targets you can invoke using these arguments. You can use TF_BUILD environment variables to enable these options without having to edit the XML in the code project file.

For example, you are using the Default Template to build an ASP.Net web application project, and you want to drop only the web package to your staging location. To do this, you edit the build definition and:

  • Set the Output location process parameter to AsConfigured.

  • Add something like the following options to the MSBuild arguments:

    /p:DeployOnBuild=true;DeployMethod=Package /p:DefaultPackageOutputDir=”$(TF_BUILD_BINARIESDIRECTORY)”\WebPackage
    

Use environment data in programs or scripts

You can also pass TF_BUILD environment variables to programs or scripts you don’t own. You don’t have to pass them to your own scripts so long as your script accesses them using the names given above. For some examples, see Run a script in your build process.

Use environment data from a custom build process

If you need to use an environment variable in your custom build process template, you can use the GetEnvironmentVariable activity to get the data. You can get data from any of the WellKnownEnvironmentVariables.

For example, to get the path to the binaries directory, set the Name property of the GetEnvironmentVariable activity to Microsoft.TeamFoundation.Build.Activities.Extensions.WellKnownEnvironmentVariables.BinariesDirectory