我试图从TFS获得一个特定的构建,但它是令人沮丧的困难.我有一个MVC应用程序触发这样的构建:
IBuildServer buildServer = (IBuildServer)Server.GetService(typeof(IBuildServer)); IBuildDefinition def = buildServer.GetBuildDefinition(TeamProject,BuildDefinition); var queuedBuild = buildServer.QueueBuild(def);
QueueBuild返回IQueuedBuild,我希望这样做:
return queuedBuild.Build.BuildNumber
这样我就可以使用一些独特的值来查询构建服务器以获得正确的构建.不幸的是,当执行退出此方法时,Build可能为null也可能不为null,因此这是不行的.
在构建排队后,我然后轮询此方法
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; }
这在某种程度上有效,但如果队列中有多个构建,我无法知道这个轮询方法,如果我正在使用的构建是我在第一个方法中排队的构建.有没有人知道我可以做些什么来取回在第一种方法中排队的特定构建?
谢谢!
解决方法
此方案的关键是使用排队构建的ID.所以我做的是:
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; }
然后在轮询方法中
public string GetBuildStatus(string TeamProject,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; }
希望这有助于有人在路上.