Reputation: 35374
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?
Upvotes: 1
Views: 1676
Reputation: 10930
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
Upvotes: 0
Reputation: 1358
You can use the context.TrackBuildError method to mark the build error.
Upvotes: 0
Reputation: 70923
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.
Upvotes: 3