使用 TeamCity REST API 跟踪构建进度 [英] Tracking build progress with TeamCity REST API

查看:16
本文介绍了使用 TeamCity REST API 跟踪构建进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 TeamCity (7.0) REST API 来允许开发人员触发自定义构建.我像这样将构建添加到队列中:

I use the TeamCity (7.0) REST API to allow developers to trigger custom builds. I add the build to the queue like this:

http://teamcity/httpAuth/action.html?add2Queue=[buildTypeId]&name=[propName]&value=[propValue]

http://teamcity/httpAuth/action.html?add2Queue=[buildTypeId]&name=[propName]&value=[propValue]

我的问题是如何最好地跟踪刚刚触发的构建进度.REST 调用不会返回有关分配给构建的构建 ID 的任何信息,因此即使我轮询构建列表(正在运行/已完成),我也不知道其中一个是否是我触发的.队列中可能有针对同一 buildTypeId 的多个构建,因此我需要一种方法来分离出我所追求的那个.

My question is how I best can track the progress of the build just triggered. The REST call does not return any info about build ID assigned to the build, so even if I poll the list of builds (running/finished) I will not know if one of them is the one I triggered. There could potentially be several builds for the same buildTypeId in the queue, so I need a way to separate out the one I am after.

我在某处读到一个建议,您可以为放入队列中的每个构建添加一个具有唯一值的构建属性,然后轮询构建列表并查找具有该确切属性值的构建列表.然而,我还没有找到一种列出构建属性的方法,所以我仍然卡住了.此 REST 调用不提供有关属性的信息:

I read somewhere a suggestion that you could add a build property with a unique value to each build you put in the queue, and then later poll the build list and look for one with that exact property value. I have however not found a way of listing the properties for the builds, so I am still stuck. This REST call does not provide information about properties:

http://teamcity/httpAuth/app/rest/builds/?locator=buildType:[buildTypeId]

http://teamcity/httpAuth/app/rest/builds/?locator=buildType:[buildTypeId]

关于如何解决这个问题的任何建议?理想情况下,我想知道构建是否在队列中,是否正在运行,以及何时完成我想获得状态.然而,最重要的是要知道它是否已经完成以及状态是什么.

Any suggestions on how to solve this? I would ideally like to know if the build is in the queue, if it is running, and when it's done I would like to get the status. The most important is however to know if it is done and what the status has.

推荐答案

经过进一步调查,我想出了一个似乎可以正常工作的解决方案:

After some further investigation I came up with a solution for this which seems to work fine:

我发现,即使您没有使用/builds/?locator=buildType:x"调用获得有关自定义构建属性的任何信息,您也可以为该列表中的每个构建提取构建 ID然后进行另一个 REST 调用以获取有关某个特定构建的更多详细信息.其余调用如下所示:

I found out that even though you did not get any information about the custom build properties using the "/builds/?locator=buildType:x" call, you could extract the build ID for each one of the builds in that list and then do another REST call to get more details about one specific build. The rest call looks like this:

http://teamcity/httpAuth/app/rest/builds/id:{0}

此调用的响应将为您提供一个构建对象",其中包含构建属性列表等.

The response from this call will give you a "build object" which contains a list of build properties, among other things.

我用于跟踪构建进度的解决方案是这样的:

My solution for tracking the build progress was then like this:

将构建添加到 TeamCity 队列时,我首先向 URL 添加一个名为BuildIdentifier"的属性.该值只是一个 GUID.我将此标识符传递回客户端应用程序,然后客户端开始轮询服务器,询问具有此特定标识符的构建状态.然后服务器会通过一些步骤来识别构建的当前阶段:

When a build is added to the TeamCity queue, I first add a property to the URL called "BuildIdentifier". The value is just a GUID. I pass this identifier back to the client application, and then the client starts polling the server, asking for the status of the build with this specific identifier. The server then goes through some steps to identify the current stage of the build:

1:检查构建是否正在运行.我通过调用/builds?locator=running:true"获取正在运行的构建列表,遍历构建并使用构建 ID 查询 REST API 以获取详细信息.然后,我会查看每个正在运行的构建的详细信息,以寻找与我从客户端收到的具有匹配BuildIdentifier"属性的构建.如果在其中一个正在运行的构建中存在匹配项,我会向正在跟踪进度的客户端发送一条响应,其中包含构建正在以 x%(构建对象的 PercentageComplete 属性)运行的消息.如果未找到匹配项,我将继续执行第 2 步.

1: Check if the build is running. I get the list of running builds with the call "/builds?locator=running:true", iterate through the builds and use the build ID to query the REST API for details. I then go through the details for each running build looking for a build with a matching "BuildIdentifier" property to the one I received from the client. If there is a match in one of the running builds I send a response with a message that the build is running at x percent (PercentageComplete property of the build object) to the client who is tracking the progress. If a match is not found I move on to step 2.

2:检查是否完成:首先使用/builds/?locator=buildType:x"调用获取最新的构建列表.然后执行与步骤 1 相同的操作,并从列表中提取 X 个最新版本(我选择了 5).为了限制 REST 调用的数量,我假设构建将在最新的 5 个构建中完成.然后,我在 BuildIdentifier 上查找匹配项,如果找到匹配项,则返回构建状态(FAILED、SUCCESS 等).

2: Check if it is finished: First get the latest build list using the "/builds/?locator=buildType:x" call. Then do the same thing as in step 1, and extract the X latest builds from the list (I chose 5). In order to limit the number of REST calls I set an assumption that the build would be in the latest 5 builds if it was finished. I then look for a match on the BuildIdentifier, and if I get one I return the status of the build (FAILED, SUCCESS, etc.).

3:如果在第 1 步或第 2 步中没有与 BuildIdentifier 匹配,我可以假设构建在队列中,因此我将其作为当前状态返回.

3: If there was no match for the BuildIdentifier in step 1 or 2 I can assume that the build is in the queue, so I return that as the current status.

在客户端,只要状态表明构建在队列中或正在运行,我就会每 x 秒轮询一次服务器的状态.

On the client side I poll the server for the status every x seconds as long as the status is saying that the build is either in the queue, or running.

如果有其他人遇到同样的问题,希望这个解决方案会有所帮助!如果您使用 TeamCity REST API,我认为跟踪触发构建的进度是一项非常常见的任务.

Hope this solution could be helpful if there are someone else with the same problem out there! I would think that tracking the progress of a triggered build is a pretty common task if you use the TeamCity REST API.

这篇关于使用 TeamCity REST API 跟踪构建进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆