Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.Build.WebApi;
using Microsoft.VisualStudio.Services.Common;
class Program
{
static void Main(string[] args)
{
CancelBuild("https://<yourdomain>.VisualStudio.com/DefaultCollection/", "<username>", "<password>", "<project>", "<BuildDefinitionname>").GetAwaiter().GetResult();
Console.ReadLine();
}
static async Task CancelBuild(string url, string userName, string password, string project, string buildDefinitionName)
{
var build = new BuildHttpClient(new Uri(url), new VssBasicCredential(userName, password));
// Get the specified name build definition to get the id of build definition
var definitions = await build.GetDefinitionsAsync(project: project);
var target = definitions.FirstOrDefault(d => d.Name.StartsWith(buildDefinitionName, StringComparison.InvariantCultureIgnoreCase));
if (target == null)
{
throw new ArgumentException($"No build definition with name: {buildDefinitionName}", nameof(buildDefinitionName));
}
//Get the builds of the specified name build definiion
var builds = await build.GetBuildsAsync(project, new[] { target.Id });
//Update the builds with the status other than "Completed" and "Cancelling" to cancel status
foreach (var b in builds.Where(b => !(b.Status == BuildStatus.Completed || b.Status == BuildStatus.Cancelling)))
{
b.Status = BuildStatus.Cancelling;
var res = await build.UpdateBuildAsync(b, b.Id);
Console.WriteLine($"Cancelled build id {res.Id}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.