【发布时间】:2019-11-26 19:37:27
【问题描述】:
我在现有的 Git 存储库中有 100 多个 YAML 文件,每个文件都定义了自己的构建管道。我正在尝试创建一个 PowerShell 脚本来创建这些构建定义,这样我就不必花费数小时使用 Web UI 来手动添加新的构建定义并指向它们各自的 YAML 文件。
我遇到过类似的问题和资源,但无法让此脚本正常工作。
- How to create Build Definitions through VSTS REST API
- Create Build Definition using Azure Devops API
- https://www.nebbiatech.com/2018/11/29/automating-build-pipeline-creation-using-azure-devops-services-rest-api/
我知道the REST API documentation 支持克隆,但它是否支持创建链接到 Git 存储库中的 YAML 文件的构建定义?
$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;
上面的脚本出现以下错误:
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 手动创建的现有定义的情况下创建新的构建定义?
我有 100 多个 YAML 文件位于 Git 存储库内的文件夹 /azure-pipeline-YAML/ 中。我怀疑我需要以某种方式将它包含在我通过 REST API 发送的 JSON 中,但是在哪里/如何?我被这个 definition.Process 错误困住了。
更新
感谢@danielmann,我最终需要获得一些额外的信息(即repository.Id 并更改repository.Type)。我将以下内容放入脚本中,以获取基于现有 YAML 文件创建的临时定义的示例。
$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;
【问题讨论】:
标签: azure azure-devops azure-pipelines