How to fail a executed TFS build when an invoked batch script fails?

1.6k views Asked by At

I'm running a .bat file via the InvokeProcess activity in a build process template.

When the script fails, the build still continues and succeeds. How can we make the build fails at the time?

3

There are 3 answers

3
MC ND On

This article shows how to fail depending of the exit code of a console application.

Once the build activity is configured, from your batch file, use the exit command.

Use exit /b 0 to signal everything goes ok, or exit /b 1 to signal there is an error. exit command ends the execution of the batch file, setting the errorlevel/exitcode to the value after the '/b' parameter.

0
Jayendran On

You can use MSBUILD to call the bat file.Using the Exit code we can fail the build when bat file failes.

MSBuild file wrapper.proj

<Project DefaultTargets="Demo" ToolsVersion="3.5"
         xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup>
    <BatchFile>test.bat</BatchFile>
    <FromMSBuild>FromMSBuild</FromMSBuild>
    <build_configurations></build_configurations>
  </PropertyGroup>

  <Target Name="Demo">

    <Message Text="Executing batch file $(BatchFile)" Importance="high"/>

    <PropertyGroup>
      <_Command>$(BatchFile) $(build_configurations) </_Command>
    </PropertyGroup>
    <Exec Command="$(_Command)">
      <Output PropertyName="CommandExitCode" TaskParameter="ExitCode"/>
    </Exec>

    <Message Text="CommandExitCode: $(CommandExitCode)"/>

  </Target>
</Project>

test.bat

ECHO OFF

IF (%1)==() goto Start
SET fromMSBuild=1

:Start

ECHO fromMSBuild:%fromMSBuild%


REM ***** Perform your actions here *****

set theExitCode=101
GOTO End



:End
IF %fromMSBuild%==1 exit %theExitCode%


REM **** Not from MSBuild ****

ECHO Exiting with exit code %theExitCode%
exit /b %theExitCode%

Thanks to @Sayed Ibrahim Hashimi for his Post

1
Hoang Nguyen On

You can use the context.TrackBuildError method to mark the build error.