Sunday, February 18, 2007

Writing progress of a custom build task

After you decided to use Team system to automate your build process there is the possibility the build-in tasks will not be enough for you, and you will create custom tasks, to be executed during the build.

However, once the build is running and each phase is displayed in the build window, your tasks will not appear there.

I have written a new class which "CreateStep" and "CloseStep" methods, used to add a row for the task in the build window.

Instead of inheriting from "Task", switch to "BuildStepTask", and call "CreateStep" at the beginning of your task code and "CloseStep" at the end.

The class requires several parameters to link with the build server and retrieve a connection to the specific build being performed.

The "CreateStep" method uses those parameters to get a "BuildStore" object, and uses it to write to the screen. The "CloseStep" method is called once the task is complete.

public class BuildStepTask : Task
{
protected string buildUri;
protected string buildStepName;
protected BuildStore buildStore;
private bool abort = false;
private string buildNumber;
private string projectName;
private string serverName;

[Required]
protected string ServerName
{
get { return serverName; }
set { serverName = value; }
}


[Required]
protected string ProjectName
{
get { return projectName; }
set { projectName = value; }
}

[Required]
public string BuildNumber
{
get { return buildNumber; }
set { buildNumber = value; }
}


public BuildStepTask()
{
}

public override bool Execute()
{
return true;
}

protected void CreateStep(string stepName, string stepMessage, string buildNumber)
{
try
{
this.buildStepName = stepName;

TeamFoundationServer tf = new TeamFoundationServer(this.ServerName);

this.buildStore = (BuildStore)(tf.GetService(typeof(BuildStore)));

this.buildUri = buildStore.GetBuildUri(this.ProjectName, buildNumber);

this.buildStore.AddBuildStep(this.buildUri, this.buildStepName, stepMessage);
}
catch (Exception ex)
{
//Logging code
abort = true;
}
}

protected void CloseStep(string stepMessage, bool isSuccessfull)
{
if (!abort)
{
try
{
BuildStepStatus status;
if (isSuccessfull)
status = BuildStepStatus.Succeeded;
else
status = BuildStepStatus.Failed;

this.buildStore.UpdateBuildStep(this.buildUri, this.buildStepName, DateTime.Now, status);
}
catch (Exception ex)
{
//Logging code
}
}
}
}

No comments: