How to get a specific assembly using the TFS API

I am trying to get a specific assembly from TFS, but it is complicated. I have an MVC application that runs a build like this:

IBuildServer buildServer = (IBuildServer)Server.GetService(typeof(IBuildServer)); IBuildDefinition def = buildServer.GetBuildDefinition(TeamProject, BuildDefinition); var queuedBuild = buildServer.QueueBuild(def); 

QueueBuild returns IQueuedBuild, and I was hoping to do something like this:

 return queuedBuild.Build.BuildNumber 

So, I will have some unique value that I could use to query the build server to get the correct build. Unfortunately, Build may or may not be null when execution completes this method, so this is not an option.

After assembling in the queue, I then poll this method

  public string GetBuildStatus(string TeamProject, string BuildDefinition, string BuildNumber) { string status = string.Empty; IBuildDetailSpec buildDetailSpec = buildServer.CreateBuildDetailSpec(TeamProject, BuildDefinition); buildDetailSpec.MaxBuildsPerDefinition = 1; buildDetailSpec.Status = BuildStatus.InProgress | BuildStatus.None; buildDetailSpec.QueryOrder = BuildQueryOrder.FinishTimeDescending; IBuildQueryResult queryResult = buildServer.QueryBuilds(buildDetailSpec); if (queryResult.Builds.Length > 0) { status = queryResult.Builds[0].Status.ToString(); } return status; } 

This works to some extent, but if there are several assemblies in the queue, I cannot know in this polling method if the assembly I am working with is the one I queued in the first method. Does anyone know what I can do to return a specific assembly that is queued in the first method?

Thanks!

+6
source share
2 answers

The key to this scenario is the use of the identifier of a queued assembly. So what I did:

  public int QueuBuild(string TeamProject, string BuildDefinition) { IBuildServer buildServer = (IBuildServer)Server.GetService(typeof(IBuildServer)); IBuildDefinition def = buildServer.GetBuildDefinition(TeamProject, BuildDefinition); var queuedBuild = buildServer.QueueBuild(def); return queuedBuild.Id; } 

Then in the polling method

  public string GetBuildStatus(string TeamProject, string BuildDefinition, int BuildID) { IBuildServer buildServer = (IBuildServer)Server.GetService(typeof(IBuildServer)); string status = string.Empty; IQueuedBuildSpec qbSpec = buildServer.CreateBuildQueueSpec(TeamProject, BuildDefinition); IQueuedBuildQueryResult qbResults = buildServer.QueryQueuedBuilds(qbSpec); if(qbResults.QueuedBuilds.Length > 0) { IQueuedBuild build = qbResults.QueuedBuilds.Where(x => x.Id == BuildID).FirstOrDefault(); status = build.Status.ToString(); } return status; } 

Hope this helps someone along the way.

+11
source

If the assembly is queued waiting for the agent, you can call queueBuild.WaitForStart();

Then return the queryResult.Build.Uri; property queryResult.Build.Uri; and use it for polling, the assembly number is not assigned immediately.

Then you can use the IBuildDetail buildDetail = server.GetBuild(buildUri); to get the build state.

+2
source

Source: https://habr.com/ru/post/915531/


All Articles