如何在TFS 2015中使用REST API触发构建

本文关键字:API REST 构建 TFS 2015 | 更新日期: 2023-09-27 18:02:32

我在本地安装了TFS 2015 RC2。我试图使用REST API在vNext定义中排队构建。

我使用了VSO的代码示例,并进行了轻微修改(主要是更改了URL和身份验证方法以使用本地TFS)。

我正在使用两个REST API调用。

第一个是:http://mytfssrv: 8080/tfs/DefaultCollection/myproject/_apis/构建/定义/

返回所有指定的项目构建定义:ID为1的构建定义,这是一个我不感兴趣的XAML构建定义和构建定义ID 2,这是vNext构建定义-这就是我想排队我的构建

注意,我省略了?api-version=1.0部分—这是因为如果我不省略,我只得到XAML构建定义。

第二个调用是在vNext构建定义中对新构建进行排队:

文章http://mytfssrv: 8080/tfs/DefaultCollection/myptoject/_apis/构建/请求? api版本= 1.0

包含以下数据:

{"definition":{"id":**2**},"reason":"Manual","priority":"Normal","queuePosition":0,"queueTime":"0001-01-01T00:00:00","requestedBy":null,"id":0,"status":null,"url":null,"builds":null}
我从服务器得到的响应是:

TF215016:构建定义2不存在。请指定有效的构建定义,然后重试。

我尝试更改API版本,以各种方式更改post数据,但从未成功。

知道如何从它的DID治疗TFS吗?

如何在TFS 2015中使用REST API触发构建

TFS 2015 RC2使用了一个新的API (2.0-preview.2版本)。我在问题中提到的VSO示例已经过时,并且当您希望对新构建进行排队时不相关。

目前,没有文档,但是web门户使用REST API,所以只需Fiddler away。

代码如下:

var buildRequestPOSTData =
                    new BuildRequest()
                    {
                        Definition = new Definition()
                        {
                            Id = firstBuildDefinition.Id
                        },
                        Project = new Project { Id = "project guid" },
                        Queue = new Queue {  Id = 1 },
                        Reason = 1,
                        sourceBranch = "$Branch"
                    };
                responseBody = await QueueBuildAsync(client, buildRequestPOSTData, _baseUrl + "build/Builds");

下面是为构建请求提供新参数的类:

public class BuildRequest
{
    [JsonProperty(PropertyName = "definition")]
    public Definition Definition { get; set; }
    [JsonProperty(PropertyName = "demands")]
    public string Demands { get; set; }
    [JsonProperty(PropertyName = "parameters")]
    public IEnumerable<string> Parameters { get; set; }
    [JsonProperty(PropertyName = "project")]
    public Project Project { get; set; }
    [JsonProperty(PropertyName = "queue")]
    public Queue Queue { get; set; }
    [JsonProperty(PropertyName = "reason")]
    public int Reason { get; set; }
    [JsonProperty(PropertyName = "sourceBranch")]
    public string sourceBranch { get; set; }
    [JsonProperty(PropertyName = "sourceVersion")]
    public string RequestedBy { get; set; }
}
public class Definition
{
    [JsonProperty(PropertyName = "id")]
    public int Id { get; set; }
}
public class Queue
{
    [JsonProperty(PropertyName = "id")]
    public int Id { get; set; }
}
public class Project
{
    [JsonProperty(PropertyName = "id")]
    public string Id { get; set; }
}

不用REST也能很好地工作

var tfsurl = new Uri("http://localhost:8080/tfs/<***projectname***>/");    
var ttpc = new TfsTeamProjectCollection(tfsurl);
var bhc = ttpc.GetClient<BuildHttpClient>();
var builds = bhc.GetBuildsAsync("<***projectname***>").Result;
var build = builds
    .Where(x => x != null && x.Definition.Name.Equals("***buildDefinitionName***>"))
    .OrderByDescending(y => y.LastChangedDate)
    .FirstOrDefault();
bhc.QueueBuildAsync(build);

这是我在bounty中需要的"代码示例示例"。

using Microsoft.TeamFoundation.Build.WebApi;
using Microsoft.VisualStudio.Services.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
internal class TfsBuildHelper
{
    private readonly VssConnection connection;
    private readonly BuildHttpClient client;
    internal TfsBuildHelper(Uri tpcUrl)
    {
        this.connection = new VssConnection(tpcUrl, new VssClientCredentials(true));
        this.client = connection.GetClient<BuildHttpClient>();
    }
    /// <summary>
    /// Returns the build definitions for a specific team project.
    /// </summary>
    public async Task<IEnumerable<DefinitionReference>> GetBuildDefinitionsFromTeamProject(string teamProject)
    {
        return await this.client.GetDefinitionsAsync(project: teamProject, type: DefinitionType.Build);
    }
    /// <summary>
    /// Return build numbers for specific team project and build definition.
    /// </summary>
    public async Task<IEnumerable<string>> GetAvailableBuildNumbers(string teamProject, string buildDefinition)
    {
        var builds = await this.client.GetBuildsAsync(project: teamProject, type: DefinitionType.Build);
        return builds.Select(b => b.BuildNumber);
    }
}