Share via


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