Silver Light and Blue Sky

C# and Infrastructure. Code is everything.

How to queue and cancel build from C# code with VSTS Nuget library

Overview

Visual Studio Team Services has an API, so we can queue and cancel build via API.

Visual Studio Team Services and Team Foundation Server REST API Reference

Here is an API about the build.

Builds | REST API Reference for Visual Studio Team Services and Team Foundation Server

Microsoft provides four NuGet libraries for VSTS API.

.NET Client Libraries for Visual Studio Team Services (and TFS)

Microsoft.TeamFoundationServer.Client is the library to execute build operation. However, it's hard to use. I write sample codes for queuing and cancelling build.

Queue build

Here is a sample.

gist.github.com

QueueBuildAsync, which is a method to queue a build, has the Build object as a parameter. Build object has many properties. However, there is no document which property is required. I found Definition.Id (id of build definition) and Project property are necessary after several tries.

Cancel build

Next, I'd like to cancel the queued build from C# code. What do I do? There is no API in the list. "Delete" method is not the API for cancelling but for deleting the completed build itself.

The answer is executing "update" method. We should update the queued build status to "cancel".

gist.github.com

We should execute two or more API calls to queue or cancel build. That's the way to execute them from C# code.