0 Comments

So you could make a script that can do the bits below but if you don’t here is how you can easily create build agents fast for TFS 2015+ or VSO.

Now I’m not saying you should run a million agents of a single machine but this could be applied the same if you switching between multiple TS sessions to configure on multiple servers.

Download the build agent

Downloading the build agent is really simple. Navigate to your TFS control panel, click on Agent pools and then Download agent.

image

This post will assume you have setup your agent pool already but that’s as simple as click New pool and giving it a name on the screen above Smile

Setup agent file structure

Copy this to your build server and then extract the zip to look like below for example, create these folders.

image

then inside each of those folders you place a copy of the extracted zip

image

 

Get your configuration details

For this step we going option up old trusty notepad. What you are going to need is the following info in notepad, put each of these on a new line

  • Agent Name
  • Url to TFS
  • Agent Pool
  • Working directory (a blank line to use the default which is where your agent is with _working)
  • Configure as a windows service (this post assumes a y)
  • Service run as username
  • Service run as password
  • another empty line

this should look something like this

image

Setup lots of agents

This part is now fast Smile. Make sure you in the directory with your ConfigureAgent.bat file

image

Run the ConfigureAgent.bat as administrator

image

You should see something like this now

image

Copy everything in your notepad, so you should have all that config with the extra new line at the end. Now right click in cmd and paste

image

and that would all work (if you want it to stay open you can open a cmd in admin and then run the bat file from there)

image

Now go to each folders ConfigureAgent.bat and run as admin, paste in the same config (changing just the agent name to increment the number at the end)

All Done

At this point if you go to your agent pool in TFS you will see they are all setup.

image

0 Comments

This post has been ported from https://gbeeming.wordpress.com/2013/10/06/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

@echo off
call "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat"
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)

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="DoBuild" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<BuildProject Include="TfsFakeBuilds.sln"/>
</ItemGroup>

<Target Name="DoBuild">
<MSBuild Projects="@(BuildProject)">
</MSBuild>
</Target>
</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

namespace TfsBuildTask
{
using System;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
public class TfsFakeBuildStart : Task
{
[Required]
public string Architecture { get; set; }
[Required]
public string CollectionUri { get; set; }
[Required]
public string Configuration { get; set; }
[Required]
public string DefinitionName { get; set; }
[Required]
public string DetailedLogPath { get; set; }
[Required]
public string DropLocation { get; set; }
[Required]
public string ErrorLogPath { get; set; }
[Required]
public string PathFromBuildRoot { get; set; }
[Required]
public string RegularLogPath { get; set; }
[Required]
public string ServerPath { get; set; }
[Required]
public string TargetNames { get; set; }
[Required]
public string TeamProjectName { get; set; }
[Required]
public string WarningLogPath { get; set; }
public override bool Execute()
{
try
{
base.Log.LogMessage("Tfs Fake Build Starting on '" + this.DefinitionName + "'.");
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);
base.Log.LogMessage("Tfs Fake Build Started on '" + this.DefinitionName + "'.");
return true;
}
catch (Exception ex)
{
base.Log.LogError("Failed to start Fake Build: " + ex);
return false;
}
}
public IBuildEngine BuildEngine { get; set; }
public ITaskHost HostObject { get; set; }
}
}

TfsFakeBuildFinish.cs

namespace TfsBuildTask
{
using System;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
public class TfsFakeBuildFinish : Task
{
public override bool Execute()
{
try
{
base.Log.LogMessage("Tfs Fake Build Stopping.");
FakeBuildObject.Instance.Stop();
base.Log.LogMessage("Tfs Fake Build Stopped.");
return true;
}
catch(Exception ex)
{
base.Log.LogError("Failed to stop Fake Build: " + ex);
return false;
}
}
public IBuildEngine BuildEngine { get; set; }
public ITaskHost HostObject { get; set; }
}
}

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

namespace TfsBuildTask
{
using System;
using System.IO;
using System.Linq;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;
public class FakeBuildObject
{
private static FakeBuildObject instance;
private string architecture;
private string collectionUri;
private string configuration;
private string definitionName;
private string detailedLogPath;
private string dropLocation;
private string errorLogPath;
private string pathFromBuildRoot;
private string regularLogPath;
private string serverPath;
private string targetNames;
private string teamProjectName;
private string warningLogPath;
private IBuildDefinition buildDefinition;
private IBuildProjectNode buildProjectNode;
private IBuildServer buildServer;
private TfsTeamProjectCollection collection;
private IBuildDetail detail;
private DateTime startTime;
private DateTime endTime;
public static FakeBuildObject Instance
{
get
{
if (instance == null)
{
instance = new FakeBuildObject();
}
return instance;
}
}
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)
{
this.detailedLogPath = detailedLogPath;
this.errorLogPath = errorLogPath;
this.warningLogPath = warningLogPath;
this.dropLocation = dropLocation;
this.regularLogPath = regularLogPath;
this.collectionUri = collectionUri;
this.teamProjectName = teamProjectName;
this.definitionName = definitionName;
this.configuration = configuration;
this.pathFromBuildRoot = pathFromBuildRoot;
this.architecture = architecture;
this.serverPath = serverPath;
this.targetNames = targetNames;
this.startTime = DateTime.Now;
this.collection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(this.collectionUri));
this.buildServer = this.collection.GetService<IBuildServer>();
this.buildDefinition = this.buildServer.GetBuildDefinition(this.teamProjectName, this.definitionName);
this.detail = this.buildDefinition.CreateManualBuild(this.definitionName + " - " + DateTime.Now.ToString("yyyyMMddHHmmss"), this.dropLocation);
this.buildProjectNode = this.detail.Information.AddBuildProjectNode(this.configuration, this.pathFromBuildRoot, this.architecture, this.serverPath, this.startTime, this.targetNames);
}
public void Stop()
{
this.endTime = DateTime.Now;
string[] regularLinesFromLog = ConvertToLines(this.ReadFile(this.regularLogPath));
string[] errorLinesFromLog = ConvertToLines(this.ReadFile(this.errorLogPath));
string[] warningLinesFromLog = ConvertToLines(this.ReadFile(this.warningLogPath));
this.buildProjectNode.CompilationErrors = 0;
this.buildProjectNode.CompilationWarnings = 0;
DateTime nextMessageLogTime = this.startTime;
int timeBetweenMessages = Convert.ToInt32((this.endTime - this.startTime).TotalMilliseconds / regularLinesFromLog.Length);
foreach (string line in regularLinesFromLog)
{
if (!string.IsNullOrEmpty(line))
{
if (errorLinesFromLog.Contains(line))
{
this.buildProjectNode.Node.Children.AddBuildError(line, nextMessageLogTime);
this.buildProjectNode.CompilationErrors++;
}
else if (warningLinesFromLog.Contains(line))
{
this.buildProjectNode.Node.Children.AddBuildWarning(line, nextMessageLogTime);
this.buildProjectNode.CompilationWarnings++;
}
else
{
this.buildProjectNode.Node.Children.AddBuildMessage(line, BuildMessageImportance.Normal, nextMessageLogTime);
}
}
nextMessageLogTime.AddMilliseconds(timeBetweenMessages);
}
this.buildProjectNode.Node.Children.AddExternalLink("Detailed Log File", new Uri(this.detailedLogPath));
this.buildProjectNode.Node.Children.AddExternalLink("Error Log File", new Uri(this.errorLogPath));
this.buildProjectNode.Node.Children.AddExternalLink("Warning Log File", new Uri(this.warningLogPath));
this.buildProjectNode.Save();
this.detail.Information.Save();
this.detail.FinalizeStatus(errorLinesFromLog.Length == 1 && string.IsNullOrEmpty(errorLinesFromLog[0]) ? BuildStatus.Succeeded : BuildStatus.Failed);
}
private static string[] ConvertToLines(string sr)
{
return sr.Replace("\r",string.Empty).Split('\n');
}
private string ReadFile(string logPath)
{
using (FileStream fs = new FileStream(logPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (StreamReader sr = new StreamReader(fs))
{
return sr.ReadToEnd();
}
}
}
}
}

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

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Default" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask AssemblyFile="TfsBuildTask.dll" TaskName="TfsFakeBuildStart" />
<UsingTask AssemblyFile="TfsBuildTask.dll" TaskName="TfsFakeBuildFinish" />
</Project>

tfs.build.targets

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Default" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="tfs.build.tasks" />
<PropertyGroup>
<BuildRoot>\\GORDON-PC\Demos\TfsFakeBuilds\</BuildRoot>
<DropLocation>\\GORDON-PC\Demos\TfsFakeBuilds\</DropLocation>
</PropertyGroup>
<Target Name="StartTfsBuild">
<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">
</TfsFakeBuildStart>
</Target>
<Target Name="EndTfsBuild" DependsOnTargets="StartTfsBuild;DoBuild">
<TfsFakeBuildFinish />
</Target>
</Project>

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

<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.

Download Files

4 Comments

I have been playing around with TypeScript for a while and usually I just publish from my machine Embarrassed smile but today I decided to setup a CI build for the solution and found that I received the error

 <Path to file>.ts (1): Emit Error: Write to file failed..

When a build agent checks out code for building the primary source files are locked so obviously it wouldn't let me overwrite any as part of my build process.

It took me a couple of minutes to realize it but this error was basically due to me having checked in the .js and .jsmap file that is generated for .ts files when you build a TypeScript enabled project. Simple enough to fix:

1. Remove all .js and .jsmap files that are generated off .ts files from your project

2. Make sure those same files are deleted from source control

3. Kick off the build and smile because things should be running smoothly again Smile