Kumaresh Babu N S
Kumaresh Babu N S

Reputation: 1708

How to provide pipeline parameters in creating build definition using Azure DevOps REST API?

I can able to create the build definition using Azure DevOps REST API. When I ran the build definition from the portal, it is not using azure-pipeline.yml file which is part of the codebase in Azure Repos. The pipeline file(azure-pipeline.yml) includes the docker task which will build an image for the application. But this operation is not working. To figure out this problem, I have created two definitions.

  1. Created the build definition manually from Azure DevOps Portal.
  2. Created the build definition using Azure DevOps REST API.

The first approach works as expected. It is creating a docker image for the application and the docker tasks works. The second approach didn't work and it just check out the source code from the repository.

I came across the process parameters which include the path of the pipeline file. I don't know how to configure the same using REST API.

I'm following this documentation https://learn.microsoft.com/en-us/rest/api/azure/devops/build/definitions/create?view=azure-devops-rest-5.1

EDIT 1

{
"name": "myapp2",
"badgeEnabled": "true",
"queue": {
    "name": "Hosted Ubuntu 1604",
    "pool": {
        "name": "Hosted Ubuntu 1604",
        "isHosted": true
    }
},
"jobAuthorizationScope": "projectCollection",
"jobTimeoutInMinutes": 60,
"jobCancelTimeoutInMinutes": 5,
"quality": "definition",
"type" : "build",
"process": {
    "type": 2
},
"processParameters" : {
    "inputs" : [{
        "name" : "azure-pipeline.yml",
        "required" : true
    }]
}

Rest of the configuration is related to repositories.

Creating Build Definition in Portal

enter image description here

Creating Build Definition using Azure DevOps REST API

enter image description here

Upvotes: 0

Views: 1240

Answers (1)

Levi Lu-MSFT
Levi Lu-MSFT

Reputation: 30353

From above post if you already have the yaml pipeline azure-pipeline.yml. There is no need to call Create Definition api to create a same one with the azure-pipeline.yml as parameter. you can call then queue the pipeline yaml manually or via Build Queue API

If you want to use api to create definition, You can first use Get Definition api to call an existing build pipeline definition, so that you can refer to the returned response to check what are the properties need to be configured for the Create Definition api.

Here is example to Create VSTS Build Definitions using PowerShell, Additionally to this blog, you can define the pipeline tasks(defined in process:{phases:[{steps:}]}) by referring to the response from Get Definition api.

As you can see to define a build pipeline using api is complicated, and it is not suggested as discussed in this thread. The most convenient way to create a pipeline is via GUI manually or using yaml.

Update:

If you call the Get Definition api to get the json format definition of an existing yaml pipeline. You will see there is no processParameters property. So you donot need to define processParameters property when you use api to create a yaml pipeline. Yon only to define the yaml file in process property "process": { "yamlFilename": "azure-pipelines.yml", "type": 2}.

For testing purpose, I firstly get the json format definition of an existing yaml pipeline and then modified a little bit of the json file. Then I delete the yaml pipeline from azure pipeline UI. Lastly I use Create Definiton API to create the yaml pipeline. Then yaml pipeline get created as expected.

The last screenshot you posted in your question is in the classic pipeline. Yaml pipeline doesnot have the Parameters section.

property processParameters will define this Parameters section. It is used to link all important arguments for tasks used across the build definition. check here for more information about processParameters in Classic pipeline.

enter image description here

Upvotes: 1

Related Questions