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.
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.
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:
Hope this helps.
azure-pipelines.yml
file and store in the repository like Azure Repos? –
Afebrile 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 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
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.