DCSIMG
How to : Add custom build step messages to TeamBuild process - Shai Raiten's Blog

Shai Raiten's Blog

It's all about code...

How to : Add custom build step messages to TeamBuild process

How to : Add custom build step messages to TeamBuild process

The following sample task illustrates how to add custom build step messages to the build process.

using System;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Build.Proxy;
using System.Threading;
 
// This sample references Microsoft.Build.Framework.dll, Microsoft.Build.Utilities.dll,
// Microsoft.TeamFoundation.Build.Common.dll, Microsoft.TeamFoundation.Client.dll,
// System.dll, System.Web.Services.dll
 
namespace TeamBuildSampleTasks
{
    public class SampleTaskWithCustomBuildStep : Task
    {
        private string m_teamFoundationUrl;
        private string m_buildNumber;
        private string m_teamProject;
 
        [Required]
        public string TeamFoundationUrl
        {
            get { return m_teamFoundationUrl; }
            set { m_teamFoundationUrl = value; }
        }
 
        [Required]
        public string BuildNumber
        {
            get { return m_buildNumber; }
            set { m_buildNumber = value; }
        }
 
        [Required]
        public string TeamProject
        {
            get { return m_teamProject; }
            set { m_teamProject = value; }
        }
 
        public override bool Execute()
        {
            // Create TeamBuild BuildStore web service.
            TeamFoundationServer tfs = new TeamFoundationServer(m_teamFoundationUrl);
            BuildStore bs = (BuildStore)(tfs.GetService(typeof(BuildStore)));
           
            // buildUri is used later on to identify the build
            string buildUri = bs.GetBuildUri(m_teamProject, m_buildNumber);
 
            // This string is used internally in TeamBuild to identify the message.
            string buildStepName = "Sleep Messages";
            string buildStepMsg = "Build Step: Sleep and get random result";
 
            // Add the build step message to the build process and this will show up in the build report
            // now the status set to 'in progress'
            bs.AddBuildStep(buildUri, buildStepName, buildStepMsg);
 
            // Do task actions here. My sample does nothing but goes to a sound sleep and gets a random result :)
            Thread.Sleep(10000);
            bool result = GetRandomResult();
 
            //update the build step message with pass/fail information
            if(result)
                bs.UpdateBuildStep(buildUri, buildStepName, DateTime.Now, BuildStepStatus.Succeeded);
            else
                bs.UpdateBuildStep(buildUri, buildStepName, DateTime.Now, BuildStepStatus.Failed);
 
            // Log the result into build log file
            Log.LogMessage("SampleTaskWithCustomBuildStep completed, result: " + result);
 
            return true;
        }
 
        private bool GetRandomResult()
        {
            return ((1==new Random().Next() % 2) ? true : false);
        }
    }
}

Comments

Gavin Stevens said:

I can't resolve :

using Microsoft.TeamFoundation.Client;

and

using Microsoft.TeamFoundation.Build.Proxy;

with VS2008, and VS 2008 SDk...  where are these namespaces?

# September 29, 2008 2:48 PM

... said:

Oh, its great!

# February 2, 2009 1:23 AM

Rev said:

What does this code look like now that the BuildStore Object is now obselete

# May 26, 2009 5:22 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: