Creating “Fake” Tfs Builds


Sometimes you can’t always use the tools you want to use. This is especially true when there is already a bunch of stuff setup around existing tools. Lets say for example that you have an existing application doing your builds, for everything you need around TFS you can do it from within the Web Access although this is all fine and works well, when you want to see how the builds are running you need to go out to another tool to see this information.

Today I’m going to help you setup your builds external to TFS to send build information to TFS so that you can see this info in TFS, the purpose of this is just to give you visibility from TFS and is a very basic introduction that can be extended on a lot with bigger builds.

Before we dig into any code, you will need to create a Build Definition that we will use for our fake builds, if you don’t currently have any controllers setup for you specific collection you will need to create one, after you create the build definition you will be able to de register this build controller as it’s not needed for the manually builds which we will be using.

What we had initially was a build.bat file that has the basics in it (build the build.proj file and log to different files for multiple levels of logging and dumps the latest build in a folder) that our current build server would run that looked like below

   1: @echo off

   2: call "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat"

   3: msbuild build.proj /t:DoBuild /v:d  /fl1 /fl2 /fl3 /fl4 /flp1:logfile=build.log /flp2:logfile=build.errors.log;errorsonly /flp3:logfile=build.warnings.log;warningsonly /flp4:logfile=build.details.log;detailsonly /p:OutputPath=\\GORDON-PC\Demos\drops\TfsFakeBuilds-Latest

and a build.proj file that basically just let use build multiple projects that could be part of multiple solutions (for the purpose of this post we only have 1)

   1: <?xml version="1.0" encoding="utf-8"?>

   2: <Project DefaultTargets="DoBuild" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

   3:   <ItemGroup>

   4:     <BuildProject Include="TfsFakeBuilds.sln"/>

   5:   </ItemGroup>

   6:   

   7:   <Target Name="DoBuild">

   8:     <MSBuild Projects="@(BuildProject)">

   9:     </MSBuild>

  10:   </Target>

  11: </Project>

The next steps was to create 2 msbuild tasks that we could use for the starting and stopping of our TFS Build. Create a new class library project and add a reference to the following assemblies

    Microsoft.Build.Framework
    Microsoft.Build.Utilities.v4.0
    Microsoft.TeamFoundation.Build.Client
    Microsoft.TeamFoundation.Client
    Microsoft.TeamFoundation.Common
    Microsoft.VisualStudio.Services.Common

    Add 2 classes that each inherit from Microsoft.Build.Utilities.Task, you can call theses classes TfsFakeBuildStart and TfsFakeBuildFinish. All these 2 tasks are going to do is call a call into a 3rd class that is going to do all the work.

    TfsFakeBuildStart.cs

       1: namespace TfsBuildTask

       2: {

       3:     using System;

       4:     using Microsoft.Build.Framework;

       5:     using Microsoft.Build.Utilities;

       6:     public class TfsFakeBuildStart : Task

       7:     {

       8:         [Required]

       9:         public string Architecture { get; set; }

      10:         [Required]

      11:         public string CollectionUri { get; set; }

      12:         [Required]

      13:         public string Configuration { get; set; }

      14:         [Required]

      15:         public string DefinitionName { get; set; }

      16:         [Required]

      17:         public string DetailedLogPath { get; set; }

      18:         [Required]

      19:         public string DropLocation { get; set; }

      20:         [Required]

      21:         public string ErrorLogPath { get; set; }

      22:         [Required]

      23:         public string PathFromBuildRoot { get; set; }

      24:         [Required]

      25:         public string RegularLogPath { get; set; }

      26:         [Required]

      27:         public string ServerPath { get; set; }

      28:         [Required]

      29:         public string TargetNames { get; set; }

      30:         [Required]

      31:         public string TeamProjectName { get; set; }

      32:         [Required]

      33:         public string WarningLogPath { get; set; }

      34:         public override bool Execute()

      35:         {

      36:             try

      37:             {

      38:                 base.Log.LogMessage("Tfs Fake Build Starting on '" + this.DefinitionName + "'.");

      39:                 FakeBuildObject.Instance.Start(this.DetailedLogPath, this.ErrorLogPath, this.WarningLogPath, this.DropLocation, this.RegularLogPath, this.CollectionUri, this.TeamProjectName, this.DefinitionName, this.Configuration, this.PathFromBuildRoot, this.Architecture, this.ServerPath, this.TargetNames);

      40:                 base.Log.LogMessage("Tfs Fake Build Started on '" + this.DefinitionName + "'.");

      41:                 return true;

      42:             }

      43:             catch (Exception ex)

      44:             {

      45:                 base.Log.LogError("Failed to start Fake Build: " + ex);

      46:                 return false;

      47:             }

      48:         }

      49:         public IBuildEngine BuildEngine { get; set; }

      50:         public ITaskHost HostObject { get; set; }

      51:     }

      52: }

    TfsFakeBuildFinish.cs

       1: namespace TfsBuildTask

       2: {

       3:     using System;

       4:     using Microsoft.Build.Framework;

       5:     using Microsoft.Build.Utilities;

       6:     public class TfsFakeBuildFinish : Task

       7:     {

       8:         public override bool Execute()

       9:         {

      10:             try

      11:             {

      12:                 base.Log.LogMessage("Tfs Fake Build Stopping.");

      13:                 FakeBuildObject.Instance.Stop();

      14:                 base.Log.LogMessage("Tfs Fake Build Stopped.");

      15:                 return true;

      16:             }

      17:             catch(Exception ex)

      18:             {

      19:                 base.Log.LogError("Failed to stop Fake Build: " + ex); 

      20:                 return false;

      21:             }

      22:         }

      23:         public IBuildEngine BuildEngine { get; set; }

      24:         public ITaskHost HostObject { get; set; }

      25:     }

      26: }

    In our 3rd class we will be getting a reference to a build definition and then kicking off a manual build in our start method and then in our stop method we will collection all the information to from the build and finish off the TFS manual build. Create a 3rd class like below

    FakeBuildObject.cs

       1: namespace TfsBuildTask

       2: {

       3:     using System;

       4:     using System.IO;

       5:     using System.Linq;

       6:     using Microsoft.TeamFoundation.Build.Client;

       7:     using Microsoft.TeamFoundation.Client;

       8:     public class FakeBuildObject

       9:     {

      10:         private static FakeBuildObject instance;

      11:         private string architecture;

      12:         private string collectionUri;

      13:         private string configuration;

      14:         private string definitionName;

      15:         private string detailedLogPath;

      16:         private string dropLocation;

      17:         private string errorLogPath;

      18:         private string pathFromBuildRoot;

      19:         private string regularLogPath;

      20:         private string serverPath;

      21:         private string targetNames;

      22:         private string teamProjectName;

      23:         private string warningLogPath;

      24:         private IBuildDefinition buildDefinition;

      25:         private IBuildProjectNode buildProjectNode;

      26:         private IBuildServer buildServer;

      27:         private TfsTeamProjectCollection collection;

      28:         private IBuildDetail detail;

      29:         private DateTime startTime;

      30:         private DateTime endTime;

      31:         public static FakeBuildObject Instance

      32:         {

      33:             get

      34:             {

      35:                 if (instance == null)

      36:                 {

      37:                     instance = new FakeBuildObject();

      38:                 }

      39:                 return instance;

      40:             }

      41:         }

      42:         public void Start(string detailedLogPath, string errorLogPath, string warningLogPath, string dropLocation, string regularLogPath, string collectionUri, string teamProjectName, string definitionName, string configuration, string pathFromBuildRoot, string architecture, string serverPath, string targetNames)

      43:         {

      44:             this.detailedLogPath = detailedLogPath;

      45:             this.errorLogPath = errorLogPath;

      46:             this.warningLogPath = warningLogPath;

      47:             this.dropLocation = dropLocation;

      48:             this.regularLogPath = regularLogPath;

      49:             this.collectionUri = collectionUri;

      50:             this.teamProjectName = teamProjectName;

      51:             this.definitionName = definitionName;

      52:             this.configuration = configuration;

      53:             this.pathFromBuildRoot = pathFromBuildRoot;

      54:             this.architecture = architecture;

      55:             this.serverPath = serverPath;

      56:             this.targetNames = targetNames;

      57:             this.startTime = DateTime.Now;

      58:             this.collection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(this.collectionUri));

      59:             this.buildServer = this.collection.GetService<IBuildServer>();

      60:             this.buildDefinition = this.buildServer.GetBuildDefinition(this.teamProjectName, this.definitionName);

      61:             this.detail = this.buildDefinition.CreateManualBuild(this.definitionName + " - " + DateTime.Now.ToString("yyyyMMddHHmmss"), this.dropLocation);

      62:             this.buildProjectNode = this.detail.Information.AddBuildProjectNode(this.configuration, this.pathFromBuildRoot, this.architecture, this.serverPath, this.startTime, this.targetNames);

      63:         }

      64:         public void Stop()

      65:         {

      66:             this.endTime = DateTime.Now;

      67:             string[] regularLinesFromLog = ConvertToLines(this.ReadFile(this.regularLogPath));

      68:             string[] errorLinesFromLog = ConvertToLines(this.ReadFile(this.errorLogPath));

      69:             string[] warningLinesFromLog = ConvertToLines(this.ReadFile(this.warningLogPath));

      70:             this.buildProjectNode.CompilationErrors = 0;

      71:             this.buildProjectNode.CompilationWarnings = 0;

      72:             DateTime nextMessageLogTime = this.startTime;

      73:             int timeBetweenMessages = Convert.ToInt32((this.endTime - this.startTime).TotalMilliseconds / regularLinesFromLog.Length);

      74:             foreach (string line in regularLinesFromLog)

      75:             {

      76:                 if (!string.IsNullOrEmpty(line))

      77:                 {

      78:                     if (errorLinesFromLog.Contains(line))

      79:                     {

      80:                         this.buildProjectNode.Node.Children.AddBuildError(line, nextMessageLogTime);

      81:                         this.buildProjectNode.CompilationErrors++;

      82:                     }

      83:                     else if (warningLinesFromLog.Contains(line))

      84:                     {

      85:                         this.buildProjectNode.Node.Children.AddBuildWarning(line, nextMessageLogTime);

      86:                         this.buildProjectNode.CompilationWarnings++;

      87:                     }

      88:                     else

      89:                     {

      90:                         this.buildProjectNode.Node.Children.AddBuildMessage(line, BuildMessageImportance.Normal, nextMessageLogTime);

      91:                     }

      92:                 }

      93:                 nextMessageLogTime.AddMilliseconds(timeBetweenMessages);

      94:             }

      95:             this.buildProjectNode.Node.Children.AddExternalLink("Detailed Log File", new Uri(this.detailedLogPath));

      96:             this.buildProjectNode.Node.Children.AddExternalLink("Error Log File", new Uri(this.errorLogPath));

      97:             this.buildProjectNode.Node.Children.AddExternalLink("Warning Log File", new Uri(this.warningLogPath));

      98:             this.buildProjectNode.Save();

      99:             this.detail.Information.Save();

     100:             this.detail.FinalizeStatus(errorLinesFromLog.Length == 1 && string.IsNullOrEmpty(errorLinesFromLog[0]) ? BuildStatus.Succeeded : BuildStatus.Failed);

     101:         }

     102:         private static string[] ConvertToLines(string sr)

     103:         {

     104:             return sr.Replace("\r",string.Empty).Split('\n');

     105:         }

     106:         private string ReadFile(string logPath)

     107:         {

     108:             using (FileStream fs = new FileStream(logPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))

     109:             {

     110:                 using (StreamReader sr = new StreamReader(fs))

     111:                 {

     112:                     return sr.ReadToEnd();

     113:                 }

     114:             }

     115:         }

     116:     }

     117: }

    The last parts is just to include this task in your build and then you will have the info in TFS to display on your dashboard and view without having to bounce out to another tool.

    For this we created a folder called tfs_config along side our build.proj file and then placed the dll of our fake build tasks in that folder as well as 2 files as below that will be used to kick off and complete the TFS Manual Build

    tfs.build.tasks

       1: <?xml version="1.0" encoding="utf-8"?>

       2: <Project DefaultTargets="Default" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

       3:   <UsingTask AssemblyFile="TfsBuildTask.dll" TaskName="TfsFakeBuildStart" />

       4:   <UsingTask AssemblyFile="TfsBuildTask.dll" TaskName="TfsFakeBuildFinish" />  

       5: </Project>

    tfs.build.targets

       1: <?xml version="1.0" encoding="utf-8"?>

       2: <Project DefaultTargets="Default" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

       3:   <Import Project="tfs.build.tasks" />

       4:   <PropertyGroup>

       5:     <BuildRoot>\\GORDON-PC\Demos\TfsFakeBuilds\</BuildRoot>

       6:     <DropLocation>\\GORDON-PC\Demos\TfsFakeBuilds\</DropLocation>

       7:   </PropertyGroup>  

       8:   <Target Name="StartTfsBuild">

       9:     <TfsFakeBuildStart Architecture="x86" CollectionUri="http://TfsServerUri:8080/tfs" Configuration="Debug" DefinitionName="Demo Definition" DetailedLogPath="$(BuildRoot)build.detailed.log" DropLocation="$(DropLocation)" ErrorLogPath="$(BuildRoot)build.errors.log" PathFromBuildRoot="$(BuildRoot)build.bat" RegularLogPath="$(BuildRoot)build.log" ServerPath="$/TfsFakeBuilds/build.bat" TargetNames="Defaults" TeamProjectName="Demos" WarningLogPath="$(BuildRoot)build.warnings.log">      

      10:     </TfsFakeBuildStart>

      11:   </Target>

      12:   <Target Name="EndTfsBuild" DependsOnTargets="StartTfsBuild;DoBuild">

      13:     <TfsFakeBuildFinish />

      14:   </Target>

      15: </Project>

    next we opened the build.proj file and added the line below above the target DoBuild

       1: <import Project="tfs_config\tfs.build.targets" />

    and for the last piece we duplicated the build.bat file so that full builds on dev machines would not log to TFS, all that we changed in that build.bat file was the target that we use with the msdbuild command to EndTfsBuild.

    Any finally we could stay in TFS for build info once again and we got all the info you see below Smile. Hope this helps somebody else as well.

    clip_image002clip_image002[4]imageimage

    Download Files

    Advertisements

    Leave a Reply

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

    WordPress.com Logo

    You are commenting using your WordPress.com 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 )

    Google+ photo

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

    Connecting to %s