Pero P.
Pero P.

Reputation: 26982

Querying for agent running a build

I am trying to query which agents are currently running a build using the TFS Extended Client. This is for the new TFS 2015 (non-XAML) build system.

I can easily query currently running builds with the code below, but I can not see any way to access details of the agent running the build. Neither the properties of the Build type or any of the BuildHttpClient methods appear to expose this.

Is there any way to achieve this?

VssConnection connection = new VssConnection(new Uri("TfsUri"), new VssAadCredential());
var buildClient = connection.GetClient<BuildHttpClient>();               

var builds = await buildClient.GetBuildsAsync(
       statusFilter: BuildStatus.InProgress,
       project:"project"
);

foreach (var build in builds)
{
    var buildNumber = build.BuildNumber;
    var buildQueue = build.Queue;

    //How to query for agent executing build?
}

Upvotes: 2

Views: 804

Answers (1)

Elmar
Elmar

Reputation: 1246

You can potentially use the workername property contained in the build's timeline record as an identification mechanism for the agent.

The agent's worker name typically matches the agent name when added to a queue.

agent name

To access this detail of the build via the api you're using; I tested against builds that have already completed and builds in progress, this detail was populated already at the point of execution, you may want to do additional object reference validity checking in case a worker has not yet been assigned to the specified build.

None the less here is the foreach loop from your snippet modified to include a lookup of the worker name using the build's timeline records.

foreach (var build in builds)
{
  var buildNumber = build.BuildNumber;
  var buildQueue = build.Queue;

  //How to query for agent executing build? ... Perhaps use the the worker name

  var buildDetail = await buildClient.GetBuildTimelineAsync(build.Project.Id, build.Id);
  var workerName = (string.IsNullOrEmpty(buildDetail.Records.Select(x => x.WorkerName).First())) ? "WORKER NOT INDICATED" : buildDetail.Records.Select(x => x.WorkerName).First();
 }

Upvotes: 5

Related Questions