Getting the compile and test status as environment variables when extending TF Build using scripts

The Goal

When running custom batch or PowerShell scripts as part of TF Build, you want to access the compilation and test result using an environment variable.

The Solution

When you use the new TfvcTemplate.12.xaml build process template, it’s easy to run additional scripts or tools as part of the build process.

There is a tool available (TfbEnv.exe) that will create additional environment variables with the compilation/test results. It is free and can be downloaded here.

The tool will always set the following two environment variables:

  • TFB_COMPILATIONSTATUS
  • TFB_TESTSTATUS

The value corresponds to the current result (Succeeded, Failed, Unknown).

Additional environment variables:

  • If compilation succeeds, TFB_COMPILATION_SUCCESS is set to 1.
  • If compilation fails, TFB_COMPILATION_FAILED  is set to 1.
  • If tests succeed, TFB_TEST_SUCCESS is set to 1.
  • If tests fail, TFB_TEST_FAILED is set to 1.

How to use it

There are two ways to use it:

Option 1: Use TfbEnv.exe to generate a batch file which you can call from your scripts to set up the environment variables

  1. Create a batch file that invokes TfbEnv.exe and then calls “SetVars.bat” (which is created by TfbEnv.exe).

    Here’s a very simple example:

    TfbEnv-Batch-File
    (Note: In this example TfbEnv.exe is stored in a “build” folder in Version Control)
  2. In your build definition, set “Post-test script path” to run your batch file:
    TfbEnv-Batch-Parameters
  3. Done. Queue a new build!

Option 2: Use TfbEnv.exe to invoke your batch oder PowerShell script file with the additional environment variables available

  1. In your build definition, set “Post-test script path” to run TfbEnv.exe.
  2. Under Post-test script arguments use the “run” argument to specify the batch file or PowerShell script that you want to run.

    Here’s an example:

    TfbEnv-PowerShell-Parameters
    Figure: In this example, TfbEnv will set up the environment variables
    and then execute the PowerShell script “PostTest.ps1”
  3. Done. Queue a new build!

Note: TFS 2013 Update 2 (2013.2) is required. If you are not on Update 2, you can explicitly use the /collection (or just "/c") parameter to specify your TFS Collection URL.

Enjoy!

–Neno

 

Updated (May 21, 2014): Added a note that TFS 2013 Update 2 is required (+ workaround).

Updates (May 25, 2014): Updated this blog article to explain how to use the tool (2 options).

One thought on “Getting the compile and test status as environment variables when extending TF Build using scripts”

  1. Thank you for this post. I was looking for it but no good tutorial was available. Getting the compile and test status as environment variables when extending TF Build using scripts is a complex task for newbies like me. Hhaha

    Thanks again.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>