Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 502
Mikael Koskinen Avatar asked Sep 09 '10 06:09

Mikael Koskinen


People also ask

How do you mark as failed in TeamCity?

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.

How do I disable build step in TeamCity?

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.

How do you run a command in TeamCity?

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.


1 Answers

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>'] 
like image 167
Sergey Mirvoda Avatar answered Oct 05 '22 06:10

Sergey Mirvoda