Search code examples
powershellenvironment-variablestfsbuildtfs-2015

TFS 2015 Build Step using PS environment variables fails


I have a vNext build step that runs a PowerShell script after MSBuild. I'm getting this error:

[error]BUILD_SOURCESDIRECTORY : The term 'BUILD_SOURCESDIRECTORY' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

...

[error]BUILD_BUILDNUMBER : The term 'BUILD_BUILDNUMBER' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Here's the script:

if([string]::IsNullOrEmpty($BuildNumber)){
    $BuildNumber = $Env:TF_BUILD_BUILDNUMBER
}
if([string]::IsNullOrEmpty($SolutionRoot)){
    $SolutionRoot = $Env:TF_BUILD_SOURCESDIRECTORY
}
Write-Output "-BuildName: $BuildNumber"
Write-Output "-BuildRoot: $SolutionRoot"

The script has not changed in weeks. The build definition has not changed in months. TFS was updated last night with updates 1 and 2:

I tried doing another build, but it had the same exact issue, so it's not a first-pancake type of issue. I can't find what the update might have done to the environment variables to make them unrecognizable.

I'd appreciate any ideas, and I know how to mark an answer! =)

Update: I tried removing the TF_, but it failed with identical errors

$BuildNumber = $Env:BUILD_BUILDNUMBER
$SolutionRoot = $Env:BUILD_SOURCESDIRECTORY

Update We did get this working again, but I still have no idea why things went from working to not working overnight. The TFS updates had to play a role.


Solution

  • Ok, my build is working again because I adjusted in the TFS Build definition how arguments get passed into the PS script. I think there were two slightly different issues at play.

    Here's what worked on TFS 2015 Base in the PS Arguments field:

    -SolutionRoot $(BUILD_SOURCESDIRECTORY) -BuildNumber $(BUILD_BUILDNUMBER)
    

    Here's what works on TFS 2015 Update 2 in the PS Arguments field:

    -SolutionRoot $(Build.SourcesDirectory) -BuildNumber $(Build.BuildNumber)
    

    There is relevant documentation here: https://msdn.microsoft.com/en-us/library/vs/alm/build/scripts/variables, which says [sic]

    Any text input can reference a variable by using the $(variable_name) syntax and will be substituted with the actual value at run-time. All variables are also exported to the environment as upppercase and any . are replaced with _. In scripts you can reference variables via the environment, i.e. %VARIABLE_NAME%, $VARIABLE_NAME, $env:VARIABLE_NAME, depeding on the operating system.

    Based on my trial and error, the first sentence of that paragraph appears true and works if I obey the details from the Global Build Variables section of that documentation when defining the parameters to be provided to my script. Maybe my use of all-caps and underscores there was deprecated and finally removed in update 2. I think that is issue #1.

    I have doubts regarding the remaining two sentences about environment variables. When the arguments didn't make it into my script because of issue #1, despite what the documentation says, I saw no evidence of those environment variables. This is issue #2, which is what the error messages I first saw were about. I've changed my script to rely fully on the arguments.