Azure DevOps 通过给定现有 YAML 的 REST API 创建构建定义 [英] Azure DevOps create build definition via REST API given existing YAML

查看:26
本文介绍了Azure DevOps 通过给定现有 YAML 的 REST API 创建构建定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在现有 Git 存储库中有 100 多个 YAML 文件,每个文件都定义了自己的构建管道.我正在尝试创建一个 PowerShell 脚本来创建这些构建定义,这样我就不必花费数小时使用 Web UI 手动添加新的构建定义并指向它们各自的 YAML 文件.

I have over 100 YAML files within an existing Git repo, each defining their own build pipeline. I am trying to create a PowerShell script to create these build definitions so I don't have to spend hours using the web UI to manually add new build definitions and point to their respective YAML files.

我遇到过类似的问题和资源,但无法使该脚本正常工作.

I've come across similar questions and resources, but haven't been able to get this script to work.

我知道 REST API 文档 支持克隆,但它是否支持创建链接到 Git 存储库中 YAML 文件的构建定义?

I know the REST API documentation supports cloning, but does it support creating a build definition that links to a YAML file within the Git repo?

$organization = "my-company"
$project = "MyProject"
$projUrl = "https://dev.azure.com/$($organization)/$($project)/"
$patToken = "<PAT>"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($patToken)"))
$header = @{authorization = "Basic $token"}
$Url = "$($projUrl)_apis/build/definitions?api-version=5.1"

$json = @{
    project = "$($project)";
    name = "My.New.Definition.Linked.To.Existing.YAML.File";
    repository = @{
        url = "<the-https-link-to-my-Git-repo>";
    };
    # The script still fails with definition.Process cannot be null.
    # process = 0;
    path = "A New Folder";
    type = "build"
}

$body = ($json | ConvertTo-Json -Depth 3)
Invoke-RestMethod -Uri $Url -Headers $header -Body $body -Method Post -ContentType application/json;

上面的脚本出现以下错误:

I get the following error with the script above:

Invoke-RestMethod : {"$id":"1","innerException":null,"message":"Value cannot be null.
Parameter name:
definition.Process","typeName":"System.ArgumentNullException, mscorlib","typeKey":"ArgumentNullException","errorCode":0,"eventId":0}
At create_pipelines.ps1:22 char:1
+ Invoke-RestMethod -Uri $Url -Headers $header -Body $body -Method Post ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

是否可以在不克隆我必须通过 Web UI 手动创建的现有构建定义的情况下创建新的构建定义?

Is it possible to create a new build definition without cloning an existing one I have to manually create via the web UI?

我有 100 多个 YAML 文件位于 Git 存储库内的 /azure-pipeline-YAML/ 文件夹中.我怀疑我需要以某种方式将它包含在我通过 REST API 发送的 JSON 中,但是在哪里/如何?我被这个definition.Process 错误困住了.

I have the 100+ YAML files located in a folder /azure-pipeline-YAML/ inside the Git repo. I suspect I need to somehow include that in the JSON I send via the REST API, but where/how? I'm stuck at this definition.Process error.

感谢@danielmann,我最终需要获取一些额外信息(即repository.Id 并更改repository.Type).我将以下内容放入脚本中以获得我基于现有 YAML 文件创建的临时定义的示例.

Thanks to @danielmann I ended up needing to get some additional info (i.e. repository.Id and changing the repository.Type). I put the following in the script to get an example of a temporary definition I created based on an existing YAML file.

$Url = "$($projUrl)_apis/build/definitions/13?api-version=5.1"
Invoke-RestMethod $Url -Headers $header -Method Get -ContentType application/json;

工作脚本最终是:

$organization = "my-company"
$project = "MyProject"
$projUrl = "https://dev.azure.com/$($organization)/$($project)/"
$patToken = "<PAT>"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($patToken)"))
$header = @{authorization = "Basic $token"}
$Url = "$($projUrl)_apis/build/definitions?api-version=5.1"

$json = @{
    project = "$($project)";
    name = "My.New.Definition.Linked.To.Existing.YAML.File";
    repository = @{
        url = "<the-https-link-to-my-Git-repo>";
        defaultBranch = "refs/heads/feature/my-new-feature-branch";
        id = "<taken-from-the-GET-API-request>";
        type = "TfsGit";
    };
    process = @{
        yamlFilename = "azure-pipeline-YAML/my-pipeline.yml";
        type = 2;
    };
    path = "A New Folder";
    type = "build";
}

$body = ($json | ConvertTo-Json -Depth 3)
Invoke-RestMethod -Uri $Url -Headers $header -Body $body -Method Post -ContentType application/json;

推荐答案

当您指定 process = 0 时会失败,因为 process 不应该是数字数据类型.进程需要指定一个 YAML 文件和一个类型"参数.

It's failing when you specify process = 0 because process shouldn't be a numeric data type. Process needs to specify a YAML file and a "type" parameter.

  "process": {
    "yamlFilename": "build.yaml",
    "type": 2
  }

老实说,我忘记了2"类型与1"类型与90072972"类型的区别,但我过去曾使用过.

I honestly forget what type "2" is versus type "1" versus type "90072972", but I've used that in the past.

解决此类问题的最简单方法是创建 YAML 构建并使用 REST API 提取定义 JSON.我就是这样想出来的.

The easiest way to figure this kind of thing out is to create a YAML build and pull down the definition JSON using the REST API. That's how I figured it out.

这篇关于Azure DevOps 通过给定现有 YAML 的 REST API 创建构建定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆