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

后端 未结 10 2187
轻奢々
轻奢々 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:13

    If newer TeamCity versions are within your reach then it is worth checking out some of PowerShell build runner improvements.

    In particular, changing Error Output from default warning to error may interest you.

    0 讨论(0)
  • 2020-12-06 00:13

    This has been superseded by options afforded by 9.x, but I'll leave it here as it definitely was bullet proof at the time and I couldn't find any other solution I liked better.


    You could just do something that works. The following has been tested with

    • errors in the script bit
    • missing files
    • script exiting with non-0 ERRORLEVEL

    In the TeamCity Powershell runner options, set it as follows:

    • Script File

      Source code

    • Script source

      $ErrorActionPreference='Stop'
      powershell -NoProfile "Invoke-Command -ScriptBlock { `$errorActionPreference='Stop'; %system.teamcity.build.workingDir%/Script.ps1 %system.teamcity.build.workingDir% --OptionB %BuildConfigArgument% %BuildConfigArg2%; exit `$LastExitCode }"

      (unwrapped version: powershell -NoProfile "Invoke-Command -ScriptBlock { $errorActionPreference='Stop'; %system.teamcity.build.workingDir%/Script.ps1 %system.teamcity.build.workingDir% --OptionB %BuildConfigArgument% %BuildConfigArg2%; exit$LastExitCode }"

    • Script execution mode

      Put script into PowerShell stdin with "-Command -" arguments

    • Additional command line parameters

      -NoProfile

    I'm hoping against hope this isn't the best answer!

    0 讨论(0)
  • 2020-12-06 00:14

    Powershell v2 atleast had an issue with the -File option messing up error codes. There is some details about how it messes up and stuff.

    The answer is either switch to -Command option and/or wrap it with a bat file that checks the LastErrorCode and returns 1+ if its supposed to fail.

    http://zduck.com/2012/powershell-batch-files-exit-codes/

    0 讨论(0)
  • 2020-12-06 00:15

    This checks the last error code and exits if is not 0.

        If ($LASTEXITCODE -ne 0) {
            Write-Host "The result was of the previous script was " $LASTEXITCODE
            Exit $LASTEXITCODE
        }
    
    0 讨论(0)
  • 2020-12-06 00:22

    You're over-thinking things. Try this:

    • Script

        File
      
    • Script File

        Script.ps1
      

      You don't need to give this a path - by default, it's relative to the checkout directory.

    • Script execution mode

        Put script into PowerShell stdin with "-Command -" arguments
      

    This is exactly what I use to run a bunch of powershell scripts within Teamcity.

    Update

    I missed the bit in the original post about having failures in the powershell script fail the build. Apologies!

    I've solved that part of the puzzle in two different ways.

    For regular powershell scripts

    Wrap the main code block in a try...catch; if an exception occurs, return a non-zero integer value. For successful execution, return 0.

    This convention of returning zero for success dates back a very long way in history - it's used by Unix/Linux systems, DOS, CP/M and more.

    For PSake build scripts

    Use a wrapper powershell script to invoke psake and directly set the result of the teamcity build by writing a response message to stdout.

    At the start of the script, define a status message that represents failure:

    $global:buildResult = "#teamcity[buildStatus status='FAILURE' text='It died.']
    

    Within the psake script, update $global:buildResult to indicate success in a dedicated task that's run last of all.

    $global:buildResult = "#teamcity[buildStatus status='SUCCESS' text='It lives.']
    

    At the end of the wrapper script, output the status message

    write-host $global:buildResult
    

    If anything in the build script fails, that last task won't run, and the default message (indicating failure) will be output.

    Either way, Teamcity will pick up on the message and set the build status appropriately.

    0 讨论(0)
  • 2020-12-06 00:25

    Use something like this:

     echo "##teamcity[buildStatus status='FAILURE' text='Something went wrong while executing the script...']";
    

    For reference, take a look on Reporting Build Status from Teamcity

    0 讨论(0)
提交回复
热议问题