jump to navigation

Cleaning up old Cruise Control artifacts November 12, 2007

Posted by Phill in General J2EE.
Tags: ,
trackback

One of the minor issues I have with Cruise Control is that there is currently no easy way to remove old artifacts – they just stay there indefinitely. You will probably not want to keep artifacts around indefinitely, so I wrote a quick script to remove all artifacts except for the most recent X artifacts (where X is configurable).

This will work if you have one root artifacts directory, and then publish artifacts to subdirectories within that directory (i.e., your artifacts dir is /builds and your projects are published to /builds/project-name).

It’s a Python script, and I have a cron job set to run it every ten minutes.

Note: apologies for the formatting, WordPress seems to screw up a bit when copying and pasting in code…

#! /usr/bin/env python
# ==== Variables ====
ARTIFACTS_DIR="changeme"
NUM_BUILDS_TO_KEEP=10
# ==== End Variables ====

import os

def rm_recursive(path):
	"""Removes a directory recursively"""

	for root, dirs, files in os.walk(path, topdown=False):
		for name in files:
			os.remove(os.path.join(root, name))
		for name in dirs:
			os.rmdir(os.path.join(root, name))
		os.rmdir(path)

def delete_old_builds(path):
	"""Deletes old builds"""

	builds = os.listdir(path)
	if len(builds) <= NUM_BUILDS_TO_KEEP:
		print "No builds to delete"
		return

	builds.sort()
	builds.reverse()

	i = 0

	for build in builds:
		i += 1
		if i <= NUM_BUILDS_TO_KEEP:
			continue

		print "Deleting", build
		rm_recursive(os.path.join(path, build))

for file in os.listdir(ARTIFACTS_DIR):
	path = os.path.join(ARTIFACTS_DIR, file)

	if (os.path.isdir(path)):
		delete_old_builds(path)

Comments»

1. Cleaning Up After CruiseControl | Ben Cornwell - July 7, 2008

[...] a python script for cleaning up your artifacts dir. Posted by Phill (apparently doesn’t like to share his last name). Just put it somewhere sensible and slot [...]

2. Phill - July 7, 2008

I do share my last name – the clue is in the URL to this website ;)

3. Adrien - August 3, 2009

Hi Phill, i’m new on CruiseControl and i’m using release 2.8.2
Problem is: “” does not delete anything… when works!

May you help me?

(English is not my first langage… so please forgive me if my problem is not well explained)

4. Adrien - August 3, 2009

I’m sorry but the true question is: have you wrote this because CC doesn’t implement deleteartifacts, or because this function doesn’t work??

Thank you

5. Phill - August 3, 2009

Hi Adrien, at the time I wrote this Cruise Control didn’t have a “delete artifacts” function. That may well have changed now – these days I don’t use Cruise Control but use Hudson instead.

6. Fernando M. Pinheiro - October 26, 2009

Very thanks for the tip, saved me time and A LOT of disk space.