2015-07-13 100 views
11

我已經在本地安裝了TFS 2015 RC2。我正在嘗試使用REST API將構建在vNext定義中排隊。如何使用REST API在TFS 2015中觸發構建

我使用VSO的代碼示例稍作修改(主要是更改URL和身份驗證方法以使用本地TFS)。

我使用了兩個REST API調用。

第一個是: GET http://mytfssrv:8080/tfs/DefaultCollection/myproject/_apis/build/definitions/

它返回所有指定項目構建的定義: 構建定義ID爲1,這是一個XAML構建定義我沒有興趣在 排隊,並與ID生成定義2,這是vNext構建的定義 - 這是我想要排隊我的版本

請注意,我省略了?api-version = 1.0部分 - 這是因爲如果我不這樣做,我只會得到XAML構建定義。

第二個電話是在vNext來排隊一個新的構建構建定義:

POST http://mytfssrv:8080/tfs/DefaultCollection/myptoject/_apis/build/requests?api-version=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版本,以各種方式更改發佈數據,但從未成功。

任何想法如何從它的DID治療TFS?

回答

7

TFS 2015 RC2使用了一個新的API(版本2.0-preview.2)。我在問題中提到的VSO示例已過時,並且在您希望排隊新構建時不相關。

目前,沒有文檔,但門戶網站使用REST API,所以只是Fiddler。

下面是代碼:

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; } 
} 
+1

現在這記載: https://www.visualstudio.com/integrate/api/build/builds#Queueabuild – gregjhogan

0

這是我在賞金要求「的代碼示例的例子」。

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); 
    } 
}