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".
Another build failure condition causes TeamCity to mark build as failed when a certain text is present in the build log. To add such failure condition, click Add build failure condition and select from the list: Fail build on metric change.
You can disable a build step temporarily or permanently, even if it is inherited from a build configuration template, using the corresponding option in the last column of the Build Steps list.
General SettingsThe option is available if "Executable with parameters" is selected in the Run drop-down menu. Specify the path to an executable to be started. The option is available if "Executable with parameters" is selected in the Run drop-down menu. Specify space-separated parameters to pass to the executable.
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>']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With