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:
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]projectsprojectname and the logs are in [CRUISE_CONTROL_ROOT]logsprojectname, 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
2 Responses to Getting the last build date from CruiseControl