我们如何使用Azure Devops rest API创建Azure PipelineYAML方法

问题描述

我们如何使用Azure Devops rest API创建Azure管道(yaml方法)。 基本上,我试图以编程方式而不是通过Azure Devops门户创建新管道 我在下面提到了此链接https://docs.microsoft.com/en-us/rest/api/azure/devops/pipelines/pipelines/create?view=azure-devops-rest-6.0 但这不能提供创建和配置指向代码存储库的新管线所需的确切json主体格式。请在这里帮助我

解决方法

您可以首先使用Pipelines - Get rest api检查管道的定义json,并相应地更改字段。

调用Pipelines - Create rest api时,您可以如下定义请求正文json:

$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= "\\"
 }

variables字段在UI页面中定义管道变量:

enter image description here

path字段指向代码存储库中的管道yaml文件。

repository字段定义此管道目标为的代码存储库。

folder字段定义放置管道的文件夹:

enter image description here


如果您使用Build Definitions - Create rest api创建yaml管道。您可以检查以下请求正文json示例:

$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"
                   
                }
}'

更新:

如果您的代码仓库是Githbub。您将必须在您的azure devops项目中创建一个github service connection。然后在您的api resquest正文中传递连接ID。

$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= "\\"
     }

您可以在地址栏中获取服务连接ID。见下文:

enter image description here