Getting the last build date from CruiseControl

Imagine you have a part of your build process that is a time consuming operation that only needs to take place when a particular file has been updated. How would you get the time of the last successful build? Well, CruiseControl stores a log file for each build, so one way to do it is to list the contents of the log directory and locate the latest file. Here is the Ant syntax:

?Download build.xml
   <target name="last-build">
     <exec executable="sh" outputproperty="last_build_timestamp">
       <arg value="-c" />
       <arg value="find ../../logs/projectname -name 'log*build.*.xml' | sort | tail -n 1 | sed 's/.*\([0-9]\{14\}\).*/\1/'" />
     <echo message="Last CruiseControl build was: ${last_build_timestamp}"/>

Obviously this uses a shell command so it will only run on unix / linux. You need to be careful when passing a sequence of pipe joined commands to the shell from Ant. You can’t use the name of the first command as the executable, as the remainder of the command will be fed in as a parameter, which isn’t correct. Instead, simply specify the shell as the executable and use -c to specify the full command line that you want to execute. The breakdown of the above command is:

  • Find all of the log files which start with log and have the word ‘build’ in. Unsuccessful builds will have a log file, but it won’t have the word ‘build’ in. In the example above, I’m assuming that your ant script is in [CRUISE_CONTROL_ROOT]\projects\projectname and the logs are in [CRUISE_CONTROL_ROOT]\logs\projectname, so the find command has to recurse two directories before going into the log directory.
  • Sort the list of successful build files alphabetically.
  • Get the last file name – the latest build.
  • Use the sed stream editor to extract the latest build timestamp. Unlike a normal regex search and replace command, which removes elements of a string not matched by the regex, sed passes the entire string through, only replacing sections that it has matched. Hence to remove parts of a string you need to explicitly match them, then ensure that the output part of the sed command does not print them out. In the above example, the sed command does the following:
    • .* – match the first part of the file name, but don’t capture the characters
    • \([0-9]\{14\}\) – match the 14 digit timestamp and store it in capturing group 1
    • .* – match the remainder of the file name, but don’t capture the characters
    • Then the replace part of the sed command simply prints out the captured timestamp using \1
This entry was posted in Ant. Bookmark the permalink.

2 Responses to Getting the last build date from CruiseControl

  1. ncurrie says:

    Or you could use the cruise control property ${cclastbuildtimestamp} :-)

Leave a Reply

Your email address will not be published. Required fields are marked *

502,806 Spambots Blocked by Simple Comments

HTML tags are not allowed.