Matt Slagle
Matt Slagle

Reputation: 1085

How to tell if TFS vNext build failed via variable?

I am trying to perform different actions (such as copying to a different directory on publish) if a build failed or not. I am unable to find any documentation about any variables that let me know if it failed or not. Can anyone let me know how to tell if the build failed or not?

Upvotes: 1

Views: 402

Answers (1)

Eddie Chen - MSFT
Eddie Chen - MSFT

Reputation: 29966

You can create a PowerShell script to call Rest API to get the build information (You need to enable the alternative credential):

[String]$buildID = "$env:BUILD_BUILDID"
[String]$project = "$env:SYSTEM_TEAMPROJECT"
[String]$projecturi = "$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI"

$username="alternativeusername"
$password="alternativepassword"

$basicAuth= ("{0}:{1}"-f $username,$password)
$basicAuth=[System.Text.Encoding]::UTF8.GetBytes($basicAuth)
$basicAuth=[System.Convert]::ToBase64String($basicAuth)
$headers= @{Authorization=("Basic {0}"-f $basicAuth)}

$url= $projecturi + $project + "/_apis/build/builds/" + $buildID + "/timeline?api-version=2.0"
Write-Host $url

$responseBuild = Invoke-RestMethod -Uri $url -headers $headers -Method Get | select records

And then you can check the result in the information to see if there is any failed steps and then perform the actions you want:

foreach ($record in $responseBuild.records)
{
    $result = $record.result
    ##xxxxxxxxxxxxxxxxxxxx
}

Upvotes: 3

Related Questions