【问题标题】:How can we create a Azure Pipeline(yaml approach) using Azure Devops rest api我们如何使用 Azure Devops rest api 创建 Azure Pipeline(yaml 方法)
【发布时间】:2021-01-11 10:58:27
【问题描述】:

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

【问题讨论】:

标签: azure-devops azure-devops-rest-api


【解决方案1】:

您可以先使用Pipelines - Getrest api查看管道的定义json,并相应更改字段。

在调用Pipelines - Createrest 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 页面中的管道变量:

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

repository 字段定义了此管道目标的代码仓库。

folder 字段定义了管道所在的文件夹:


如果你使用Build Definitions - Createrest 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 body 中传递连接 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。见下文:

【讨论】:

  • 非常感谢@Levi 的快速回复。我最初收到此错误:"message": "Value cannot be null.\r\nParameter name: Url"。但后来我在存储库下传递了 URL 的值,它创建了一个新的管道。但现在无法从 Azure Devops UI 编辑或打开设置,并显示以下错误:“值不能为空。参数名称:serviceEndpointId”另外如何在以编程方式创建管道后立即执行管道
  • 你的代码仓库在 github 上吗?管道中是否使用了任何服务连接?
  • 是的,我的代码仓库是 GitHub。是的,我在管道中使用了服务连接名称@Levi
  • 如果你的代码仓库是 Githbub。您必须在 azure devops 项目中创建一个github service connection。然后在您的 api resquest 正文中传递连接 ID。请参阅上面的更新。
猜你喜欢
  • 2021-03-10
  • 2021-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 2021-08-12
  • 1970-01-01
相关资源
最近更新 更多