Make the build fail if there is a powershell error
I am using the Powershell Runner for a build step. One thing I have noticed is that if a powershell error occurs, the build continues on, and even reports success. Is there any way to change this functionality? I have looked around, but I can't figure out how to do it.
Please sign in to leave a comment.
+1 this is driving me crazy. This was supposed to be fixed with 7.1.1 but I have just installed the hotfix and it doesn't seem to have made any difference...
Have you tried returning the last error code in your script or otherwise catching the error in your script and returning a non-zero exit code?
i.e.
or something like:
I believe the executed script generally does not affect the exit code of powershell executable, which is typically 0 in most cases (thus build step does not fail).
Message was edited by: Anthony Carl Changed return to exit.
Thanks Anthony. Yes I have tried that, and that works fine IF your powershell script is not in a file and is not passed parameters. As soon as you want to execute a file and have parameters passed to it then TeamCity ignores the return code and just assumes that everything was ok.
Ok I have managed to work around this.
I worked around the issue by having the build step contain this script:
I set the script type to Source code, then use this as the script source:
& "%teamcity.build.checkoutDir%\path\to\scriptIwantToExecute.ps1" "%teamcity.build.checkoutDir%" "other script parameters"
Write-Host "The result was of the previous script was " $LASTEXITCODE
Exit $LASTEXITCODE
and have the Script ExecutionMode be 'put script into powershell stdin with "-command-" arguments
Which basically tells powershelll to execute the other script passing in the required parameters and then returning the results of that build, which works now, with the build failing immediately if my scriptIwantToExecute.ps1 returns an exit code other than 0
Hope this helps