Shared Label Plugin for CCNET

Recently, i have to develop plugin to share the build label across CCNET projects depending upon previously success or failure build. So, I came up with below solution in C#-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThoughtWorks.CruiseControl.Core;
using ThoughtWorks.CruiseControl.Remote;
using System.IO;
using Exortech.NetReflector;

namespace ThoughtWorks.CruiseControl.Core.labeller

// this is the labeller name that will be used in  ccnet.config
public class SharedLabeller : ILabeller
[ReflectorProperty(“sharedLabelFilePath”, Required = true)]
public string sharedLabelFilePath { get; set; }

[ReflectorProperty(“buildSuccessFile”, Required = true)]
public string buildSuccessFile { get; set; }

[ReflectorProperty(“alwaysincrement”, Required = true)]
public string alwaysincrement { get; set; }

#region ILabeller Members

public string Generate(IIntegrationResult previousResult)
if (ShouldIncrementLabel(previousResult))
return IncrementLabel();

if (previousResult.Status == IntegrationStatus.Unknown)
return “0”;

return previousResult.Label;

public void Run(IIntegrationResult result)
result.Label = Generate(result);


private string IncrementLabel()
if (!File.Exists(sharedLabelFilePath))
return “0”;

using (FileStream fileStream = File.Open(sharedLabelFilePath,
// read last build number from file
var bytes = new byte[fileStream.Length];
fileStream.Read(bytes, 0, bytes.Length);

string rawBuildNumber = Encoding.ASCII.GetString(bytes);

// parse last build number
int previousBuildNumber = int.Parse(rawBuildNumber);
int newBuildNumber=0;
if (File.Exists(buildSuccessFile) && alwaysincrement==”yes”)
newBuildNumber = previousBuildNumber + 1;

// increment build number and write back to file
bytes = Encoding.ASCII.GetBytes(newBuildNumber.ToString());

fileStream.Seek(0, SeekOrigin.Begin);
fileStream.Write(bytes, 0, bytes.Length);

return newBuildNumber.ToString();

private static bool ShouldIncrementLabel(IIntegrationResult previousResult)
return (previousResult.Status == IntegrationStatus.Success || previousResult.Status == IntegrationStatus.Unknown);


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s