jump to navigation

Cleaning up old Cruise Control artifacts November 12, 2007

Posted by Phill in General J2EE.
Tags: ,
6 comments

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)

Follow

Get every new post delivered to your Inbox.