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?
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?
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
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
exitcommand.Use
exit /b 0to signal everything goes ok, orexit /b 1to signal there is an error.exitcommand ends the execution of the batch file, setting the errorlevel/exitcode to the value after the '/b' parameter.