Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
106 views
in Technique[技术] by (71.8m points)

How to fail the build from a PowerShell task in TFS 2015

I am trying to make a certain result in a PowerShell script fail the build process, but its not working for me. I am using the new build actions from TFS 2015 and tried the following options:

I do get red text in the log window of the step as well as marked 'Issues' in the build overview, but the build result is still Green: 'Build succeeded'

I want to use the failure in the script to fail the build and thus send an email using an alert on failed builds.

Edit: including the PS script:

Param(
  [string]$url
)

if ($url -eq '')
{
    #use default value
    $url = 'https://myurl.com'
}

$req = [system.Net.WebRequest]::Create($url)
$req.Timeout = 60000 * 5 # = 5 minutes
try
{
    Write-Host "Try to get response from url:" $url
    $res = $req.GetResponse()
    Write-Host "Closing the connection"
    $req.Close() # close the connection
} 
catch [System.Net.WebException] 
{
    Write-Host "Got an exception"    
    Write-Host "##vso[task.logissue type=error;]Exception: " $_.Exception

    if ($_.response) # try to close the connection
    {
        $_.response.Close();    
    }
    $res = $_.Exception.Response
}
$printCode=[int]$res.StatusCode

Write-Host "Result StatusCode:" $res.StatusCode "(" $printCode ")"
If ([int]$res.StatusCode -eq 200)
{
    Write-Host "##vso[task.complete result=Succeeded;]Done"
}
Else
{
    Write-Host "##vso[task.logissue type=error;]Test error: " $res
    Write-Host "##vso[task.complete result=Failed;]Error testing if demo site is up"
    exit 1
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've created a very simple script:

 Write-Error ("Some error")
 exit 1

Save this as a PowerShell script. Create a new Empty Build definition and only add one PowerShell task that points to your script. When I do this I get a failed build with the following error:

2015-12-30T10:27:29.4872452Z . 'C:a1sscript.ps1' 
2015-12-30T10:27:29.6780242Z Executing the following powershell script. (workingFolder = C:a1s)
2015-12-30T10:27:29.6790500Z C:a1sscript.ps1 
2015-12-30T10:27:33.8017820Z ##[error]C:a1sscript.ps1 : Some error
2015-12-30T10:27:33.8027833Z ##[error]At line:1 char:1
2015-12-30T10:27:33.8037819Z ##[error]+ . 'C:a1sscript.ps1'
2015-12-30T10:27:33.8037819Z ##[error]+ ~~~~~~~~~~~~~~~~~~~~~~~
2015-12-30T10:27:33.8047816Z ##[error]    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
2015-12-30T10:27:33.8047816Z ##[error]    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,script.ps1
2015-12-30T10:27:33.8057887Z ##[error] 
2015-12-30T10:27:33.8057887Z ##[error]Process completed with exit code 1 and had 1 error(s) written to the error stream.

The main difference with your script is the usage of Write-Error in combination with an exit 1.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...