【问题标题】:How to import/upload multiple json files to Azure Devops Pipeline Builds?如何将多个 json 文件导入/上传到 Azure Devops Pipeline Builds?
【发布时间】:2021-02-15 07:33:08
【问题描述】:

以下脚本尝试将多个 json 文件导入 Azure DevOps Pipeline 版本。


$JsonNames = Get-ChildItem C:\Users\<UserName>\Desktop\*.json | Select-Object -ExpandProperty Name

ForEach ($JN in $JsonNames)

{

$token = "PAT token"

$url = "https://dev.azure.com/{organization}/{Project}/_apis/build/definitions?api-version=6.0-preview.2"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$JSON= Get-Content "C:\Users\<UserName>\Desktop\$($JN).json"


$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -ContentType application/json -body $JSON


}


但是我收到以下错误消息。

Invoke-RestMethod : {"$id":"1","innerException":null,"message":"The request specifies project ID xxxxxxxxxxxxxxxxxxxxx but the supplied pipeline specifies project ID 
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.","typeName":"Microsoft.TeamFoundation.Build.WebApi.RouteIdConflictException, 
Microsoft.TeamFoundation.Build2.WebApi","typeKey":"RouteIdConflictException","errorCode":0,"eventId":3000}
At line:17 char:13
+ $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "B ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

如错误消息中所述,我在 Notepad++ 上打开了一个 JSON 文件并更新了项目 ID。我尝试再次运行它,但收到以下消息。

Invoke-RestMethod : {"$id":"1","innerException":null,"message":"To get sources from a different team project, on the Options tab you must set the build job authorization 
scope to ‘Project collection’ and ensure that the 'Limit job authorization scope to current project' pipeline setting is 
disabled.","typeName":"Microsoft.TeamFoundation.Build.WebApi.InvalidDefinitionException, 
Microsoft.TeamFoundation.Build2.WebApi","typeKey":"InvalidDefinitionException","errorCode":0,"eventId":3000}
At line:17 char:13
+ $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "B ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

然后我对脚本做了一些小改动。

$Json = Get-ChildItem C:\users\username\desktop\test\*.json | Select-Object -ExpandProperty Name
ForEach ($J in $Json)

{
$token = "PAT token"
$header = @{Authorization = 'Basic ' +[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($token)"))}
$JN = Get-Content "C:\users\username\desktop\test\$($J)"
$body = $JN | ConvertTo-Json

$response = Invoke-RestMethod -Method POST -uri 'https://dev.azure.com/organization/project/_apis/build/definitions?api-version=6.0' -Body '$body' -Headers '$header' -ContentType 'application/json'

}

我收到以下消息。有人可以帮我吗?我尝试了多种方法,但仍然无法解决此问题。

Invoke-RestMethod : Cannot bind parameter 'Headers'. Cannot convert the "$header" value of type "System.String" to type "System.Collections.IDictionary". At line:10 char:146

... h/_apis/build/definitions?api-version=6.0' -Headers '$header' -Conten ...
                                                    ~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod], ParameterBindingException
FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

【问题讨论】:

  • 嗨@albbla91 这张票有更新吗?如果答案能给你一些帮助,请随时告诉我。只是提醒this。
  • @Kevin Lu-MSFT,很抱歉回复晚了。谢谢你的帮助。非常感谢。
  • 很高兴为您提供帮助。

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


【解决方案1】:

我已经测试了样本并重现了相同的问题。看来您是在另一个项目中创建构建管道。

要解决此问题,您需要更改以下设置:

在构建 json 文件中,您需要更改 projectID 和 QueueID。

您可以在项目设置 -> 代理池 -> 目标池中获取 QueueID。

Json 文件示例:更改 Queueid

{"href":"https://dev.azure.com/{Org}/_apis/build/Queues/337"}},"id":337,"name":"Azure Pipelines","url":"https://dev.azure.com/{Org}/_apis/build/Queues/337","pool":{"id":9,"name":"Azure Pipelines","isHosted":true}},

在目标项目中,您需要导航到 Project Settings -> Settings 。然后您可以禁用将作业授权范围限制到当前项目以进行非发布管道选项。

注意:如果您无法在项目设置中更改此设置,则需要在组织设置->设置中关闭相应选项。

这是我的脚本示例:

$JsonNames = Get-ChildItem C:\Users\path\Downloads\*.json | Select-Object -ExpandProperty Name

ForEach ($JN in $JsonNames)

{

$token = "PAT"

$url = "https://dev.azure.com/{organizationName}/{ProjectName}/_apis/build/definitions?api-version=6.0"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$JSON= Get-Content "C:\Users\<UserName>\Desktop\$($JN).json"

echo $JSON


$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -ContentType application/json -body $JSON


}

注意:您需要将 API 版本 api-version=6.0-preview.2 更改为 api-version=6.0。 api-version=6.0-preview.2 不是Official document

中的版本

【讨论】:

  • 感谢您的帮助。是的,我有一个现有项目,我正在尝试在另一个项目上创建一个构建。我正在将现有项目的构建保存到桌面,然后尝试使用 PowerShell 将构建从桌面导入到新项目。我知道有一种方法可以在项目之间克隆构建,但我需要按照这个步骤将构建保存在桌面上。我需要导入几个构建。你知道是否有一种方法可以将它们从桌面导入新项目而不更改队列 ID 或项目 ID?
  • 嗨@albbla91。由于您使用的是经典 UI 管道,因此您只能将定义导出为 json 文件。然后您可以在另一个项目中生成相同的管道。在这种情况下,如果不更改队列 ID 或项目 ID,则无法将它们从桌面导入新项目。我能理解你的要求。但是没有任何方法可以达到这个目标。您可以检查答案中的示例是否有效。感谢您的理解。
猜你喜欢
  • 1970-01-01
  • 2021-05-14
  • 2021-05-14
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 1970-01-01
  • 2020-11-06
  • 1970-01-01
相关资源
最近更新 更多