如何使用REST API在TFS 2015中触发构建
我在本地安装了TFS 2015 RC2。 我正在尝试使用REST API在vNext定义中对构建进行排队。
我正在使用来自VSO的代码示例稍作修改(主要是更改URL和身份validation方法以使用内部部署TFS)。
我正在使用两个REST API调用。
第一个是:GET http:// mytfssrv:8080 / tfs / DefaultCollection / myproject / _apis / build / definitions /
返回所有指定的项目构建定义:ID为1的构建定义,这是一个XAML构建定义我不想排队并使用ID 2构建定义,这是vNext构建定义 – 这就是II想要对我的构建进行排队的地方
请注意,我省略了?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吗?
TFS 2015 RC2使用新的API(版本2.0-preview.2)。 我在问题中提到的VSO样本已经过时,并且当您希望对新构建进行排队时不相关。
目前,没有文档,但Web门户使用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 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; } }
这是我在赏金中要求的“代码示例示例”。
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(); } /// /// Returns the build definitions for a specific team project. /// public async Task> GetBuildDefinitionsFromTeamProject(string teamProject) { return await this.client.GetDefinitionsAsync(project: teamProject, type: DefinitionType.Build); } /// /// Return build numbers for specific team project and build definition. /// public async Task> GetAvailableBuildNumbers(string teamProject, string buildDefinition) { var builds = await this.client.GetBuildsAsync(project: teamProject, type: DefinitionType.Build); return builds.Select(b => b.BuildNumber); } }
这就像没有REST的魅力
var tfsurl = new Uri(“ http:// localhost:8080 / tfs / < projectname > /”);
var ttpc = new TfsTeamProjectCollection(tfsurl);
var bhc = ttpc.GetClient();
var builds = bhc.GetBuildsAsync(“< projectname >”)。结果;
var build = builds.Where(x => x!= null && x.Definition.Name.Equals(“ buildDefinitionName >”))。OrderByDescending(y => y.LastChangedDate).FirstOrDefault();
bhc.QueueBuildAsync(生成);