I have the following query which is very useful overall, but I cannot seem to determine which build agent my build used. Note: - This is now working code based on the excellent help given by @Patrick-MSFT.
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("***"));
IBuildServer buildServer = (IBuildServer) tfs.GetService(typeof (IBuildServer));
var buildDetail = buildServer.CreateBuildDetailSpec("Team Project","Dev-CI");
buildDetail.MinFinishTime = DateTime.Now.Date.AddDays(-2);
var buildResult = buildServer.QueryBuilds(buildDetail).Builds.Dump();
buildInformation.Nodes.Where (i => i.Type == "ActivityTracking").FirstOrDefault ().Children.Nodes.Where (n => n.Type == "AgentScopeActivityTracking").FirstOrDefault ().Fields["ReservedAgentUri"].Dump("Agent");
The build will be assigned to any of the online agents. When the build finishes you should be able to get the agent information from the Information (IBuildInformation) property of IBuildDetail
. You can try getting the node with the following type: InformationTypes.AgentScopeActivityTracking Field and InformationFields.ReservedAgentName Field or InformationFields.ReservedAgentUri Field
A example code:
IBuildInformation buildInformation = buildDetail.Information;
IBuildInformationNode[] buildInformationNodes = buildInformation.Nodes;
string agentUri = buildInformationNodes[0].Children.Nodes[3].Fields["ReservedAgentUri"];
IBuildAgent buildAgent = buildService.GetBuildAgent(new Uri(agentUri));
More details you can refer a similar question: TFS build duration report by agent