Forcing a CruiseControl.net project build from PowerShell

While we are in the process of migrating to TeamCity for our CI server, we're (sadly) still using CruiseControl.NET for some of our older automated continuous integrations. I have a project that I don't want to have build every time CCNET detects a check in. It's not ideal but this is a special case.

I quickly became bored/frustrated etc. having to open a browser and force a build every time I wanted a new build to kick off. I thought this would be a perfect job to automate using PowerShell.

Below is the script I use to kick off a build of my project. I've made it a bit more generic than the one I use at the office. :)

cls
$projectName = "Test Project"

# Return the project node from the XML 
function GetProject() {
	$doc = New-Object System.Xml.XmlDocument
	$doc.Load("http://ccnetURL/XmlStatusReport.aspx")
	$doc.Projects.Project | Where-Object {$_.name -eq $projectName}
}

#Return the value of the activity element of the project
function GetActivity() {
	$project = GetProject
	$project.activity	
}

#Return the lastBuildStatus element of the project
function GetLastBuildStatus() {
	$project = GetProject
	$project.lastBuildStatus
}

#Update this to be the URL to the ViewProjectReport page of the project you are viewing
$result = Invoke-WebRequest "http://SERVER_NAME/server/local/project/$projectName/ViewProjectReport.aspx?ForceBuild=Force" -Method Get

if ($result.StatusCode -eq 200) {
	
	Write-Host "Build started" -foregroundcolor green
	Start-Sleep -s 10
	$i = 0

	while($i -ne 20) {
		$i++
		$activity = GetActivity
		Write-Host "Activity: $activity" 
		
		if ($activity -eq 'Sleeping') {
			Write-Host "Build done" -foregroundcolor green
			break
		}

		Start-Sleep -s 10
	}

	$status = GetLastBuildStatus

	if ($status -eq "Success") {
		Write-Host "Build status: $status" -foregroundcolor green
	} else {
		Write-Host "Build status: $status" -foregroundcolor red
	}

} else {
	Write-Host "NOPE!" -foregroundcolor red
}

It's been working great and has saved my sanity. Always automate something that you have to do more than twice.