Start an Azure build by API

logo azure

Did you know that it is possible to start a build Azure by API? It's not something that we need every day, but knowing that it exists, and that we have a lot of possibilities with Azure APIs, can make our life easier when the time comes.

In context

Just to give a concrete example to see its usefulness, here is what I had to do recently. From a web application .NET Core, I had to be able to build an image Docker, according to certain parameters. As the app is hosted on Azure, I couldn't just install Docker on my server and trigger a process to do it. So I turned to Azure to prepare a pipeline of build. Once the build ready, I had to be able to start it on demand.


To be able to trigger a build, we need a Personal Access Tokens at Azure level. When creating it, it is important to have permission Read & Execute of selected, as seen here:

Http Client

For the creation of the http client, the important thing is to add the personal access token in the headers for authorization and to specify the accept header for application / json. Here is the method that took care of building my client (note that the token is here constantly, but it should normally come from appsettings):

private static HttpClient BuildClient () {const string personalaccesstoken = " "; var client = new HttpClient (); client.DefaultRequestHeaders.Accept.Add (new MediaTypeWithQualityHeaderValue (" application / json ")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue (" Basic ", Convert.ToBase64String (Encoding. GetBytes ($ ": {personalaccesstoken}"))); return client;}

Unleash the build

Then all we have to do is call the API to trigger the build. Here we need 3 parameters, which again should come to us from the appsettings. This is the name of the organization, the project as well as the definition ID of the build, which can be found in the URL when consulting our build.

URL d'un build avec indication de l'organisation, du projet et du ID

Once we have this information, we just need to call the URL with a POST with the necessary information. In my example, I needed to send a parameter to the build, hence the inclusion of the dictionary for the TemplateParameters:

private static async Task StartBuild () {const string organizationName = " "; const string projectName =" "; const int buildDefinitionId = 6; var client = BuildClient (); await client.PostAsJsonAsync ($" PublicitéOrganizationNameBeeperPojectNameBeep/_apis/build/builds?api-version= 6.0 ", new PipelineBody {Definition = new DefinitionReference {Id = buildDefinitionId}, TemplateParameters = new Dictionary {{"testParam", "test.json"}}}); }

The classes PipelineBody and DefinitionReference are classes I created explicitly for these calls and only contain the properties you see in the method.


I hope you find this little tip useful if you ever need to go through APIs to start a build Azure. Also note that there are several other APIs that can be used in a similar context. Here is the link on the documentation of theAPI Build, but do not hesitate to consult the different APIs available in the left menu on this page.


Author: Bruno

By day, I am a developer, team leader and co-host of the Bracket Show. By evening, I am a husband and the father of two wonderful children. The time I have left after all of this I spend trying to get moving, playing and writing video game reviews, as well as preparing material for the Bracket Show recordings and for this blog. Through it all, I am also passionate about music and microbreweries.