本文介绍了如何使用VSTS REST API排队新版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本

Param(
   [string]$vstsAccount = "abc,
   [string]$projectName = "abc",
   [string]$user = "",
   [string]$token = "xyz"
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$verb = "POST"


$body = @"
{

    "definition": {
         "id": 20
    }
}
"@


$uri = "https://$($vstsAccount).visualstudio.com/DefaultCollection/$($projectName)/_apis/build/builds?api-version=4.1"
$result = Invoke-RestMethod -Uri $uri -Method $verb -ContentType "application/json" -Body (ConvertTo-Json $body)  -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

但是我收到此错误

Invoke-RestMethod : {"$id":"1","innerException":null,"message":"This request expects an object in the request body, but the supplied data could not be
deserialized.","typeName":"Microsoft.TeamFoundation.Build.WebApi.RequestContentException,

因此,我尝试通过浏览器将构建放入队列中,并使用开发人员工具查看有效负载:

So I tried to queue a build from the browser and see the payload using developer tools:

{"queue":{"id":70},"definition":{"id":20},"project":{"id":"b0e8476e-660a-4254-a100-92ef0ec255e5"},"sourceBranch":"refs/heads/master","sourceVersion":"","reason":1,"demands":[],"parameters":"{\"system.debug\":\"false\"}"}

因此,我将其替换为脚本:

So, I replaced that into my script:

$body = @"
{"queue":{"id":70},"definition":{"id":20},"project":{"id":"b0e8476e-660a-4254-a100-92ef0ec255e5"},"sourceBranch":"refs/heads/master","sourceVersion":"","reason":1,"demands":[],"parameters":"{\"system.debug\":\"false\"}"}
"@

但是我仍然遇到相同的错误.

However I keep getting the same error.

此端点的官方文档在这里,但不清楚 https ://docs.microsoft.com/zh-cn/rest/api/vsts/build/builds/queue?view = vsts-rest-4.1#request-body

The official documentation for this endpoint is here, but its not clearhttps://docs.microsoft.com/en-us/rest/api/vsts/build/builds/queue?view=vsts-rest-4.1#request-body

推荐答案

使用REST API排队构建,您可以使用以下powershell脚本:

To queue a build with REST API, you can use below powershell script:

$body = '
{
        "definition": {
            "id": number
        }
}
'
$bodyJson=$body | ConvertFrom-Json
Write-Output $bodyJson
$bodyString=$bodyJson | ConvertTo-Json -Depth 100
Write-Output $bodyString
$user="name"
$token="PAT"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$Uri = "https://account.visualstudio.com/project/_apis/build/builds?api-version=4.1"
$buildresponse = Invoke-RestMethod -Method Post -UseDefaultCredentials -ContentType application/json -Uri $Uri -Body $bodyString -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
write-host $buildresponse

这篇关于如何使用VSTS REST API排队新版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 02:55