How do I get errors to propagate in the TeamCity PowerShell runner

后端 未结 10 2186
轻奢々
轻奢々 2020-12-05 23:47

I have a TeamCity 7 Build Configuration which is pretty much only an invocation of a .ps1 script using various TeamCity Parameters.

I was hoping that mig

10条回答
  •  执笔经年
    2020-12-06 00:29

    An alternative to the accepted answer that works for me On TeamCity 9 (I don't like changing the 'Build Failure Conditions' option as it affects all build steps):-

    I wanted PowerShell errors to fail the build step, but with a pretty message. So I wanted to throw an error message AND return an error code.... try / catch / finally to the rescue.

    EDIT for clarity: This script is supposed to fail. It is demonstrating that it is possible to throw an exception AND to return an exit code. So you get both, the exit code for TeamCity to deal with, and, in my case, a nice clear debug message that shows where the issue was.

    My demo script:

    Try {
        Write-Host "Demoing the finally bit"
        # Make sure if anything goes wrong in the script we get an exception
        $ErrorActionPreference = "Stop"
    
        # This will fail and throw an exception (unless you add the file)
        Get-Content IDontExist.txt
    }
    Catch
    {
        # Throwing like this gives a pretty error message in the build log
        throw $_
        # These give less pretty/helpful error messages
        # Write-Host $_
        # Write-Host $_.Exception.Message
    }
    Finally
    {
        # 69 because it's more funny than 1
        exit(69)
    }
    

提交回复
热议问题