如何使用 Team Foundation Server 对象模型获取可编写脚本的构建定义?

How to get scriptable build definitions using Team Foundation Server object model?

是否可以使用常规对象模型获取 Team Foundation Server 2015 新构建定义,或者我是否被迫使用 REST API?

如果可以使用对象模型,我应该使用什么class?

我能够使用 IBuildServer.QueryBuildDefinitions 获得 XAML 构建定义。是否有与此方法等效的方法,以便我可以访问新的构建定义及其变量?

您正在寻找 Build 2.0 REST API。 documentation can be found here.

有一个客户端包装器作为新 NuGet 包的一部分提供,Microsoft.TeamFoundation.Build2.WebApi 程序集提供了一个 BuildHttpClient 对象,它是访问新构建系统的起点。

您可以使用 Microsoft.TeamFoundation.Build.WebApi.BuildHttpClient:

获取构建定义
        var cred = new VssCredentials(new WindowsCredential(new NetworkCredential("{userid}", "{password}")));
        string collectionURL = "http://{tfs-server-url}:8080/tfs/{collection-name}";
        var buildClient = new BuildHttpClient(new Uri(collectionURL, UriKind.Absolute), cred);

        //this is the source project's build definition
        //http://{tfs-server-url}:8080/tfs/{collection-name}/{project-name}/_build?_a=completed&definitionId={buildDefId}
        var buildDefId = 20;
        //"http://{tfs-server-url}:8080/tfs/{collection-name}/{project-name}";
        var projectName = "{project-name}";
        var buildDef = (await buildClient.GetDefinitionAsync(projectName, buildDefId)) as BuildDefinition;

        Console.WriteLine(buildDef.Name);//here you can get all the other properties

VssCredentials class 来自 Microsoft.VisualStudio.Services.Common 程序集。将所有 {...} 替换为您的参数。