简体   繁体   中英

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.


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

You could get the QueueID in Project Settings -> Agent Pools -> Target Pool .

在此处输入图像描述

Json file sample: Change 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 . Then you could disable the Limit job authorization scope to current project for non-release pipeline option.

在此处输入图像描述

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 . The api-version=6.0-preview.2 is not the version in Official document

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM