How to create new build pipeline using Azure DevOps REST API?

I'm trying to create new build pipeline through programmatically. I'm looking for Azure DevOps REST API for this action. I didn't get the proper examples for creating the pipeline using REST API.

Please advise.


Solution 1:

How to create new build pipeline using Azure DevOps REST API?

To create the new build pipeline, we could use the REST API Definitions - Create:

POST https://dev.azure.com/{organization}/{project}/_apis/build/definitions?api-version=5.0

But we need provide too much information in the Request Body, this will be a big project and error-prone. That also the reason why the document not provide a sample Request Body there.

To resolve this issue, usually we would use the REST API Definitions - Get to get the Response Body from the template pipeline:

GET https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=5.0

Then we just need to update the corresponding properties by modifying the Response Body.

Now, we get the new Request Body for the new pipeline, we could use it with REST API Definitions - Create to create a new pipeline.

You could check this thread for some more details.

Update:

After creating the build definition, Will it create azure-pipelines.yml file and store in the repository like Azure Repos?

No, if you want to create a YAML type build definition using the rest api, it will not create the corresponding yaml file to our repo, because this API Definitions - Create only send the request body to the pipeline does not operate our repo. Currently, it support creating a build definition that links to a YAML file within the Git repo. If you want to create the yaml automatically, you could check the REST API Pushes - Create.

This should be where the REST API Definitions - Create needs to be improved to support the YAML pipeline. You could add your request for this feature on our UserVoice site (https://developercommunity.visualstudio.com/content/idea/post.html?space=21 ), which is our main forum for product suggestions. Thank you for helping us build a better Azure DevOps:

enter image description here

Hope this helps.