[英]How to import/upload multiple json files to Azure Devops Pipeline Release and Builds?
[英]How to import/upload 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
}
但是我收到以下錯誤消息。
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
我已經測試了樣本並重現了相同的問題。 您似乎正在另一個項目中創建構建管道。
要解決此問題,您需要更改以下設置:
在構建 json 文件中,您需要更改projectID和QueueID 。
您可以在Project Settings -> Agent Pools -> Target Pool中獲取 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 。 然后,您可以禁用將作業授權 scope 限制到當前項目以進行非發布管道選項。
注意:如果您無法在項目設置中更改此設置,則需要在組織設置->設置中關閉相應選項。
這是我的腳本示例:
$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不是官方文檔中的版本
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.