torsdag den 16. oktober 2014

How to prevent NAnt task returning code 3 to fail full buildprocess

Issue

The issue is that my buildscript fails because a task returns a code different from 0 even though the executable succeeds.

The buildscript contains a task that copies my website to a development server.
I use RoboCopy.exe to do this.

The task looks like this

  <target name="upload" depends="release"> 
   <exec 
    program="${robocopy}" 
    commandline=".\Website ${devroot}\www ${robocopyUploadArgs}" 
           /> 
  </target> 

However RoboCopy has a set of codes it can return depending on how many files were copied. See http://ss64.com/nt/robocopy-exit.html for more info.
Here it tells that a return value below 9 is a success.

Solution

By specifying to parameters; failonerror="false" and resultproperty="return.code" it is possible to tell NAnt that we dont want to fail on error but want the exit-code to be put in a property named return.code. Following a simple <if> task enables us to test and fail if the return code is larger than 8.
The script now looks like this (remember to add property named return.code)



  <target name="upload" depends="release"> 
   <exec 
    program="${robocopy}" 
    commandline=".\Website ${devroot}\www ${robocopyUploadArgs}" 
    failonerror="false" 
    resultproperty="return.code" 
        /> 

   <if test="${int::parse('0' + return.code) > 8}"> 
    <fail message="RoboCopy failed."/> 
   </if> 
  </target> 




Ingen kommentarer:

Send en kommentar