简体   繁体   English

如何将多个 json 文件导入/上传到 Azure Devops Pipeline Builds?

[英]How to import/upload multiple json files to Azure Devops Pipeline Builds?

The following script tries to import multiple json files to Azure DevOps Pipeline builds.以下脚本尝试将多个 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


}


However I receive the following error message.但是我收到以下错误消息。

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

As mentioned on the error message, I opened one JSON file on Notepad++ and updated the project ID.如错误消息中所述,我在 Notepad++ 上打开了一个 JSON 文件并更新了项目 ID。 I tried to run it again but receive the following message.我尝试再次运行它,但收到以下消息。

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

Then I made minor changes on the script.然后我对脚本做了一些小的改动。

$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'

}

I received the following message.我收到以下消息。 Could someone please help me?有人可以帮我吗? I have tried multiple things and I am not still not able to get this resolved.我已经尝试了多种方法,但我仍然无法解决这个问题。

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

I have tested the sample and reproduce the same issues.我已经测试了样本并重现了相同的问题。 It seems that you are create the build pipeline in another project.您似乎正在另一个项目中创建构建管道。

To solve this issue, you need to change the following settings:要解决此问题,您需要更改以下设置:

In build json file, you need to change the projectID and the QueueID .在构建 json 文件中,您需要更改projectIDQueueID

You could get the QueueID in Project Settings -> Agent Pools -> Target Pool .您可以在Project Settings -> Agent Pools -> Target Pool中获取 QueueID。

在此处输入图像描述

Json file sample: Change 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}},

In the target Project, you need to navigate to Project Settings -> Settings .在目标项目中,您需要导航到Project Settings -> Settings Then you could disable the Limit job authorization scope to current project for non-release pipeline option.然后,您可以禁用将作业授权 scope 限制到当前项目以进行非发布管道选项。

在此处输入图像描述

Note: If you cannot change this setting in Project Settings, you need to turn off the corresponding option in Organization Settings -> Settings .注意:如果您无法在项目设置中更改此设置,则需要在组织设置->设置中关闭相应选项。

Here is my script sample:这是我的脚本示例:

$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


}

Note: You need to change the API Version api-version=6.0-preview.2 to api-version=6.0 .注意:您需要将 API 版本api-version=6.0-preview.2更改为api-version=6.0 The api-version=6.0-preview.2 is not the version in Official document api-version=6.0-preview.2不是官方文档中的版本

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将多个 json 文件导入/上传到 Azure Devops Pipeline Release and Builds? - How to import/upload multiple json files to Azure Devops Pipeline Release and Builds? 如何将多个 json 文件导入/上传到 Azure Devops Pipeline Releases? - How to import/upload multiple json files to Azure Devops Pipeline Releases? 如何从 Azure DevOps 管道将多个发布或构建导出到桌面? - How to export multiple Release or Builds from Azure DevOps Pipeline to desktop? 如何在 Azure Devops 中相同管道的两个版本之间共享文件? - How to share a file between two builds of same pipeline in Azure Devops? Azure DevOps Pipeline AZ CLI 上传-批处理不推送文件 - Azure DevOps Pipeline AZ CLI upload-batch not pushing files 在 azure DevOps 中将构建从一个管道复制到另一管道 - Copy Builds from one pipeline to other pipeline in azure DevOps 能否在 Azure DevOps 中触发 CD 管道之前组合多个 CI 构建? - Can you combine multiple CI builds prior to a CD pipeline being triggered in Azure DevOps? 如何从 Azure DevOps 管道读取 Azure 文件共享文件 - How to Read Azure File Share Files from Azure DevOps Pipeline 如何在Azure devops中将管道模板用于多个管道(在多个项目中) - How to use a pipeline template for multiple pipelines (in multiple projects) in Azure devops 如何将 Json 变量传递给 Azure DevOps 管道中的命令行任务? - How to pass Json variable to command line task in Azure DevOps pipeline?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM