Click here to Skip to main content
15,889,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm using TFSBuild/MSBuild for our nightly build process. I want to introduce a task that executes an external batch file, and fails the build if the return code is non-zero. According to the documentation a non-zero return code from an external task should fail the build.

Here's my attempt:
XML
<target name="CheckDBScripts">
  <exec command="$(ExecPath)\CheckScriptsForDBO.bat $(ScriptRootPath)" />
</target>


Here's the batch file contents:
grep --quiet \[dbo\] %1\*.sql
rem swap the result code so that no matches = 0, to suit MSBuild conventions
if %errorlevel%==1 exit /B 0
if %errorlevel%==0 exit /B 1


The target runs and the batch file executes, but the return code is ignored - the build always passes regardless.

Am I missing something out?

Neil
Posted
Updated 6-Aug-10 0:12am
v2

Try this without the exit call:

VB
grep --quiet \[dbo\] %1\*.sql
if ERRORLEVEL 1 EXIT 0
if ERRORLEVEL 0 EXIT 1


Good luck!

ps. it could also be that you need to lose the /B in the exit call...
 
Share this answer
 
v4
Thanks for the response. Do you mean

if %errorlevel%==0 SET ERRORLEVEL=1 ?

I guess you do.
 
Share this answer
 
Comments
E.F. Nijboer 6-Aug-10 6:28am    
No I did not ;-) the dos command script language is somewhat strange. I also copied your if construction in my first answer but updated it. Have a look here for more info on it:
http://www.robvanderwoude.com/errorlevel.php
E.F. Nijboer 6-Aug-10 6:33am    
ps. this site also is a very good reference when using batch scripts:
http://www.computerhope.com/msdos.htm
http://www.computerhope.com/batch.htm
Thanks - I'll try that and report the result.
 
Share this answer
 
It appears the TFSBuild is still not getting the errorcode back. If I amend my build script as

XML
<Target Name="CheckDBScripts">
  <Exec Command ="$(ExecPath)\CheckScriptsForDBO.bat $(ScriptRootPath)\common"/>
  <Message Text="Exitcode=$(ExitCode)"/>
</Target>



I get

Exitcode=

in the log. And the build still does not fail when it should.

ExitCode is documented as an MSBuild Exec task property.
 
Share this answer
 
The following script is working properly, you can test both results. You first need to open the command window and then execute the script. I added the /b again so the command window won't close after execution.

The script below is tested by copying a file. When c:\temp\test.txt exists and can be copied, you will see that both if %errorlevel% lines visible in the command window. This means that the first if was false and the second did execute the exit. When c:\temp\test.txt doesn't exist you will see only the first if and falls true. This means that the script works effectively with the copy process. Hopefully it will work just as well with your command.

VB
copy "c:\temp\test.txt" copy.txt
rem swap the result code so that no matches = 0, to suit MSBuild conventions
if %errorlevel%==1 EXIT /b 0
if %errorlevel%==0 EXIT /b 1


Good luck!
 
Share this answer
 
I think the problem is not so much the script (my original script works in the same way as yours) but getting the errorcode back to the ExitCode property in MSBuild.
 
Share this answer
 
I think I have it working now. Thanks for the help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900