Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

teamcity - Why are my PowerShell exit codes always "0"?

I've got a PowerShell script as follows

##teamcity[progressMessage 'Beginning build']
# If the build computer is not running the appropriate version of .NET, then the build will not run. Throw an error immediately.
if( (ls "$env:windirMicrosoft.NETFrameworkv4.0*") -eq $null ) {
    throw "This project requires .NET 4.0 to compile. Unfortunately .NET 4.0 doesn't appear to be installed on this machine."
    ##teamcity[buildStatus status='FAILURE' ]
}

##teamcity[progressMessage 'Setting up variables']
# Set up variables for the build script
$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path
$v4_net_version = (ls "$env:windirMicrosoft.NETFrameworkv4.0*").Name
$nl = [Environment]::NewLine

Copy-Item -LiteralPath "$directorypathpackagesNUnit.2.6.2lib
unit.framework.dll" "$directorypathPandell.Testsindebug" -Force

##teamcity[progressMessage 'Using msbuild.exe to build the project']
# Build the project using msbuild.exe.
# Note we've already determined that .NET is already installed on this computer.
cmd /c C:WindowsMicrosoft.NETFramework$v4_net_versionmsbuild.exe "$directorypathPandell.sln" /p:Configuration=Release
cmd /c C:WindowsMicrosoft.NETFramework$v4_net_versionmsbuild.exe "$directorypathPandell.sln" /p:Configuration=Debug

# Break if the build throws an error.
if(! $?) {
    throw "Fatal error, project build failed"
    ##teamcity[buildStatus status='FAILURE' ]
}

##teamcity[progressMessage 'Build Passed']
# Good, the build passed
Write-Host "$nl project build passed."  -ForegroundColor Green


##teamcity[progressMessage 'running tests']
# Run the tests.
cmd /c $directorypathuild_tools
unit
unit-console.exe $directorypathPandell.TestsindebugPandell.Tests.dll

# Break if the tests throw an error.
if(! $?) {
    throw "Test run failed."
    ##teamcity[buildStatus status='FAILURE' ]
}

##teamcity[progressMessage 'Tests passed']

From what I'm lead to believe, an uncaught Throw will result in an exit code of 1, but unfortunately TeamCity is saying otherwise.

[19:32:20]Test run failed.
[19:32:20]At C:BuildAgentworke903de7564e599c8uild.ps1:44 char:2
[19:32:20]+     throw "Test run failed."
[19:32:20]+     ~~~~~~~~~~~~~~~~~~~~~~~~
[19:32:20]    + CategoryInfo          : OperationStopped: (Test run failed.:String) [],
[19:32:20]   RuntimeException
[19:32:20]    + FullyQualifiedErrorId : Test run failed.
[19:32:20]
[19:32:20]Process exited with code 0
[19:32:20]Publishing internal artifacts
[19:32:20][Publishing internal artifacts] Sending build.finish.properties.gz file
[19:32:20]Build finished

It might also be important to note that my Execution Mode is set to Execute .ps1 script with "-File" argument.

I tried changing it to Put script into PowerShell stdin with "-Command -" arguments, but then it failed with an exit code of 1 even with passing tests. I'm sure that running it as -File is going to be the right way.

If I open up the script located at C:BuildAgentworke903de7564e599c8uild.ps1 and run it manually in CMD, it does the same thing... I.e., the failing tests fail, and the %errorlevel% is still 0.

Yet, if I run it in PowerShell and call $LASTEXITCODE, it returns the right code every time.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

This is a known issue with PowerShell. Executing a script with -file returns an exit code of 0 when it shouldn't.

(Update: The links below no longer work. Please look for, or report, this problem on PowerShell: Hot (1454 ideas) – Windows Server)

Since using -command wasn't working for you, you could try adding a trap at the top of the script:

trap
{
    write-output $_
    ##teamcity[buildStatus status='FAILURE' ]
    exit 1
}

The above should result in a proper exit code when an exception is thrown.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...