TeamCity command line build runner: How to make the build fail?

10.5k views Asked by At

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".

1

There are 1 answers

2
Sergey Mirvoda On BEST ANSWER

See Build Script Interaction with TeamCity topic.

You can report messages for build log in the following way:

##teamcity[message text='<message text>' errorDetails='<error details>' status='<status value>']

where:

  • The status attribute may take following values: NORMAL, WARNING, FAILURE, ERROR. The default value is NORMAL.
  • The errorDetails attribute is used only if status is ERROR, in other cases it is ignored.

This message fails the build in case its status is ERROR and "Fail build if an error message is logged by build runner" checkbox is checked on build configuration general settings page. For example:

##teamcity[message text='Exception text' errorDetails='stack trace' status='ERROR']

Update 2013-08-30:

As of TeamCity 7.1 build failures should be reported using the buildProblem service message instead:

##teamcity[buildProblem description='<description>' identity='<identity>']