Create a Pipeline with the Azure DevOps API

John Reilly May 8, 2021
Source

Creating an Azure Pipeline using the Azure DevOps REST API is possible, but badly documented. This post goes through how to do this; both using curl and using TypeScript.

If you're curious as to how to list pipelines, then check out my post on listing pipelines with the Azure DevOps API.

curling a pipeline

The documentation for creating an Azure Pipeline using the Azure DevOps API is somewhat lacking. It doesn't mention key parameters like repository and path which are necessary to create a correctly configured pipeline. However, whilst it isn't documented officially, it can be done. You just need the recipe.

Here's a curl to make you a pipeline:

Looking at the above there's two things you need:

  1. A personal access token. You can make one of those here: https://dev.azure.com/organisation-name/_usersSettings/tokens (where organisation-name is the name of your organisation)
  2. A makepipeline.json file, which contains the details of the pipeline you want to create:

Let's talk through the significant properties above:

  • folder - can be null if you'd like the pipeline to be created in the root of Pipelines; otherwise provide the folder name. Incidentally a null will be translated into a value of \ which appears to be the magic value which represents the root.
  • name - your pipeline needs a name
  • path - this is the path to the yaml pipelines file in the repo. Note we're creating the pipeline itself here; what's actually in the pipeline sits in that file.
  • repository.id - this is the guid that represents the repo you're creating the pipeline for. You can find this out by going to your equivalent https://dev.azure.com/organisation-name/project-name/_settings/repositories (substituting in appropriate values) and looking up your repository there.
  • repository.name - the name of your repo

When you execute your curl you should be returned some JSON along these lines:

And inside Azure DevOps you'll now have a shiny new pipeline:

What if I want to use TypeScript?

If you'd like to do the same in TypeScript, you can use the azure-devops-node-api package. Just like the official documentaion, the package is short of the necessary parameters to create a pipeline such as repository and path. But by combining a little fetch with the azure-devops-node-api you can get the job done.

Here's a TypeScript example:

The above code:

  1. Gets the repository id using the azure-devops-node-api
  2. Creates a pipeline using fetch and the Azure DevOps REST API, with the types we've created to help us understand the shape of the data we're working with

Discussion in the ATmosphere

Loading comments...