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

2

3

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.

Afebrile answered 22/1, 2020 at 6:33 Comment(0)
W
6

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.

Winterfeed answered 22/1, 2020 at 10:4 Comment(9)
Thanks! Is there REST API to run the pipeline?Afebrile
Can you please provide mention the properties which are mandatory for creating new build pipeline?Afebrile
@KumareshBabuNS, Yes, we could queue pipeline by this REST API:learn.microsoft.com/zh-cn/rest/api/azure/devops/build/Builds/… and you could check this thread for some details info:#50483572Winterfeed
@KumareshBabuNS, I am afraid we could not provide mention the properties which are mandatory for creating new build pipeline, there is no any document for each properties. If we want to know that, we may need to test them one by one.Winterfeed
After creating the build definition, Will it create azure-pipelines.yml file and store in the repository like Azure Repos?Afebrile
@KumareshBabuNS, 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. As I test, the result is same. Check my update answer for some more details.Winterfeed
Let us continue this discussion in chat.Afebrile
You can also use the AzurePipelinePS powershell module to make the api calls easierDiscernment
Why do you have to use the build definitions endpoint instead of the pipelines endpoint when creating a YAML pipeline?Sliding
K
3

I know this question is old but I wanted to add how I did this in 2023 while automating a move from on premise DevOps to Azure DevOps (cloud) with powershell where we had yaml based builds (azure-pipelines.yml) in the repository source root.

The body to submit for creating a yaml based build (pipeline) I found to be the following json:

{
"configuration": {
    "path": "azure-pipelines.yml",
    "repository": {
        "id": "YourRepoGUID",
        "type": "azureReposGit"
    },
    "type": "yaml"
},
"name": "WHATEVER_YOU_WANT_HERE"

}

I am using the VSTeam PS Module to help in wrapping the API calls nicely in powershell.

First we set the team, api and project using Set-VSTeamAccount, Set-VSTeamApiVersion and Set-VSTeamDefaultProject. I get the repository Id using Get-VSTeamGitRepository, update the name and repository id in the json snippet and then submit a post to create using InvokeVSTeamRequest. I couldn't find an Add-VSTeamPipeline and I couldn't get Add-VSTeamBuildDefinition to work plus it needs a file. I've used Add-VSTeamBuildDefinition for classic builds (GUI based/non-yaml) so I know it works for those.

Set-VSTeamAccount <your settings>
Set-VSTeamApiVersion AzD2019U1
Set-VSTeamDefaultProject <your project>

$repoId = Get-VSTeamGitRepository -Name <yourreponame> | select -ExpandProperty id

$buildJson = '{"configuration": {"path": "azure-pipelines.yml","repository": {"id": "","type": "azureReposGit"},"type": "yaml"},"name": ""}'

$jsonObj = ConvertFrom-Json $buildJson
$jsonObj.configuration.repository.id = $repoId
$jsonObj.name = "<YourNamingConvention>"
$jsonRaw = $jsonObj | ConvertTo-Json -Compress

$buildDefMeta = Invoke-VSTeamRequest -resource pipelines -version '7.0' -method Post -body $jsonRaw -JSON | ConvertFrom-Json
Kinetic answered 3/3, 2023 at 21:36 Comment(1)
IMO, this should be the accepted answer. The body given here is correct. I believe it would also accept an optional configuration.variables property that is a an object holding key/value pairs to be used as variables by the pipeline.Colleen

© 2022 - 2025 — McMap. All rights reserved.