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.

Kumaresh Babu N S
  • 1,648
  • 5
  • 23
  • 39

2 Answers2

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.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • Thanks! Is there REST API to run the pipeline? – Kumaresh Babu N S Jan 22 '20 at 10:27
  • Can you please provide mention the properties which are mandatory for creating new build pipeline? – Kumaresh Babu N S Jan 22 '20 at 10:28
  • @KumareshBabuNS, Yes, we could queue pipeline by this REST API:https://learn.microsoft.com/zh-cn/rest/api/azure/devops/build/Builds/Queue?view=azure-devops-rest-5.0&viewFallbackFrom=vsts-rest-4.1 and you could check this thread for some details info:https://stackoverflow.com/questions/50483072/how-to-queue-a-new-build-using-vsts-rest-api – Leo Liu Jan 22 '20 at 11:31
  • @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. – Leo Liu Jan 22 '20 at 11:34
  • 1
    After creating the build definition, Will it create `azure-pipelines.yml` file and store in the repository like Azure Repos? – Kumaresh Babu N S Jan 22 '20 at 12:21
  • @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. – Leo Liu Jan 23 '20 at 02:57
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206490/discussion-between-kumaresh-babu-n-s-and-leo-liu-msft). – Kumaresh Babu N S Jan 23 '20 at 04:59
  • You can also use the AzurePipelinePS powershell module to make the api calls easier – Dejulia489 Oct 30 '20 at 17:35
  • Why do you have to use the build definitions endpoint instead of the pipelines endpoint when creating a YAML pipeline? – Rye bread Jan 28 '22 at 11:37
1

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
Kevin LaBranche
  • 20,908
  • 5
  • 52
  • 76