0
votes

How can we create a Azure Pipeline(yaml approach) using Azure Devops rest apis. Basically I am trying to create a new pipeline programmatically and not through Azure Devops portal I referred to this link below: https://docs.microsoft.com/en-us/rest/api/azure/devops/pipelines/pipelines/create?view=azure-devops-rest-6.0 but this does not provide the exact json body format required to create and configure a new pipline pointing to code repo. Kindly help me here

1
Create your pipeline with the web interface and use the GET from API to see the JSON format : docs.microsoft.com/en-us/rest/api/azure/devops/pipelines/…vernou

1 Answers

1
votes

You can first use the Pipelines - Get rest api to check the definition json of the pipeline, and change the fields accordingly.

You can define the request body json as below, when calling Pipelines - Create rest api:

$body = @{
  configuration=@{
                    variables=@{
                                customVariable=@{
                                        value="value"
                                }
                    };
                    path="azure-pipelines.yml";
                    repository=@{
                                    id=  "repository-id";
                                    name="repository-name"
                                    type=  "azureReposGit"
                                  };
                    type=  "yaml"
       };
   name=  "pipeline-name";
   folder= "\\"
 }

The variables field define the pipeline Variables in the UI Page:

enter image description here

The path field points to pipeline yaml file in the code repo.

The repository field defines the code repo this pipeline targets to.

The folder field defines which folder the pipeline is placed:

enter image description here


If you use Build Definitions - Create rest api to create the yaml pipeline. You can check below request body json example:

$body='{ "variables":  {
                      
                      "customVariable":  {
                                 "value":  "customValue",
                                 "allowOverride":  true
                             }
                  },
 
    "process":  {
                    "yamlFilename":  "azure-pipelines.yml",
                    "type":  2
                },
    "repository":  {
                       "id":  "repo-id",
                       "type":  "TfsGit",
                       "name":  "repo-Nanme",
                       
                       "defaultBranch":  "refs/heads/master",
                       "clean":  null,
                       "checkoutSubmodules":  false
                   },
  
    "name":  "pipeline-name",
  
    "path":  "\\",
    "type":  "build",
    "queueStatus":  "enabled",
    
    "project":  {
                    "id":  "project-id",
                    "name":  "project-name"
                   
                }
}'

Update:

If you code repo is Githbub. You will have to create a github service connection in your azure devops project. And then pass the connection id in your api resquest body.

$body = @{
      configuration=@{
                        variables=@{
                                    customVariable=@{
                                            value="value"
                                    }
                        };
                        path="azure-pipelines.yml";
                        repository=@{
                                    
                                    FullName="githubAccount/repoName";
                                    type=  "gitHub";
                                    Connection= @{
                                                   id=  "github service connection id"
                                                  }
                                  };
                        type=  "yaml"
           };
       name=  "pipeline-name";
       folder= "\\"
     }

You can get the service connection id in the address bar. See below:

enter image description here