We're using TeamCity's command line build runner to call a bat-file. The bat-file builds our solution by calling the Visual Studio 2008's "devenv.exe" and then it executes the unit tests and creates the correct folder structure.
What we would like to do is to stop executing the bat-file if the call to devenv fails and make the TeamCity to realize that the build failed. We can catch the failed devenv call by checking the ErrorLevel (which is 1 if the build failed) and we can exit our bat-file at that point. But how can we tell to the TeamCity that the build failed?
This is what we've tried:
call "build.bat"
IF ERRORLEVEL 1 EXIT /B 1
But TeamCity doesn't recognize our exit code. Instead the build log looks like this:
[08:52:12]: ========== Build: 28 succeeded or up-to-date, 1 failed, 0 skipped ==========
[08:52:13]: C:\_work\BuildAgent\work\bcd14331c8d63b39\Build>IF ERRORLEVEL 1 EXIT /B 1
[08:52:13]: Process exited with code 0
[08:52:13]: Publishing artifacts
[08:52:13]: [Publishing artifacts] Paths to publish: [build/install, teamcity-info.xml]
[08:52:13]: [Publishing artifacts] Artifacts path build/install not found
[08:52:13]: [Publishing artifacts] Publishing files
[08:52:13]: Build finished
So TeamCity will report that the build was successful. How can we fix this?
Solution:
TeamCity provides a mechanism called Service Messages which can be used to handle situations like this. I've updated my build script to look like the following:
IF %ERRORLEVEL% == 0 GOTO OK
echo ##teamcity[buildStatus status='FAILURE' text='{build.status.text} in compilation']
EXIT /B 1
:OK
As a result TeamCity will report my build as failed because of a "Failure in compilation".