给定现有的YAML,Azure DevOps通过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.
- 如何通过VSTS REST API创建构建定义
- 使用Azure Devops API创建构建定义
- 我知道
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.\r\nParameter 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?
我在Git存储库内的/azure-pipeline-YAML/文件夹中有100多个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 therepository.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.