How to trigger a build in TFS 2015 using REST API
This works like a charm without 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);
TFS 2015 RC2 uses a new API (version 2.0-preview.2). The VSO sample I mentioned in the question is outdated and not relevant when you wish to queue a new build.
Currently, there is no documentation but the web portal uses REST API so just Fiddler away.
Here is the code:
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");
And here is the class with new parameters for build requests:
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; }
}