1

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?

Community
  • 1
  • 1
Nam G VU
  • 33,193
  • 69
  • 233
  • 372

3 Answers3

3

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.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • I did put the line `exit /b 1` to signal my .bat file to fails but the build is still succeeded. The article you gave out is somehow about customized process activity. I just use the native `InvokeProcess` one. – Nam G VU Dec 17 '13 at 18:22
  • 1
    @NamG.VU, `InvokeProcess` just starts a program and returns the `Result` (the exit code/errorlevel). But it is necessary to indicate the condition on the result value to fail the build. That is the reason for the variable to hold the value of the return and the `if` over the variable to throw the error. – MC ND Dec 18 '13 at 07:11
  • Please update your answer to include `Result` value and how to use it in the `if` over that variable to fail the build. Thank you! – Nam G VU Dec 27 '13 at 09:21
0

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

Hoang Nguyen
  • 1,348
  • 1
  • 10
  • 17
0

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

Jayendran
  • 9,638
  • 8
  • 60
  • 103