Owen Martin
Owen Martin

Reputation: 77

Get Build Step exit code in TeamCity

I have a build with a number of build steps that compile, run tests and package the application using msdeploy via an MSBuild script. Currently, if any of my tests fail, the package still gets built, however I would want the build to either stop at the point of failure, or to be able to pass a variable into the MSBuild script that is the exit code of the test/compile stage, and create the package based on the value of that variable. Currently, I haven't been able to find any variables that contain this information..

Upvotes: 0

Views: 2594

Answers (2)

wonderwall
wonderwall

Reputation: 142

This is easy to do in teamcity if you have a separate step and a separate build script for each and every step. But if you are using one single script to do each and every thing (build, tests, package and deploy) and only one teamcity step to call the script then it becomes quite difficult as you will have to add a lots of try and catch which will not work on non catch able errors.

I suggest to use psake. To use psake you will need to have 2 scripts. The first one is the one which is ran by teamcity. In the first script you import psake module and call the main script which does everything for you. If the second script fails anywhere it's gonna stop the script (provided you have stoponfailure powershell policy on your agents). The second script will stop and will return to the first script. But the first script will not consider it as failure and will show the build as success. To overcome this you add a piece of code in your first script.

Import-Module .\psake\psake.psm1

Invoke-Psake .\build-steps.ps1 @args

if($psake.build_success -eq $false){
    write-host "There was an error running psake script"
    exit 1
}
Remove-Module psake

Powershell exit code is always 0 in teamcity build step

Upvotes: 1

KMoraz
KMoraz

Reputation: 14164

Set StopOnFirstFailure=true

If true, when one of the projects fails to build, no more projects will be built.

Additionally, define build parameters and conditionally set the TeamCitySetStatus.

Upvotes: 1

Related Questions