powershell 如何使用VSTS REST API对新构建进行排队

kxxlusnw  于 2022-12-23  发布在  Shell
关注(0)|答案(3)|浏览(124)

我有以下脚本

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,

所以我尝试从浏览器中对构建进行排队,并使用开发人员工具查看有效负载:

{"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\"}"}

所以,我把它替换成了我的剧本:

$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\"}"}
"@

然而,我不断得到同样的错误。
此端点的官方文档在这里,但不清楚https://learn.microsoft.com/en-us/rest/api/vsts/build/builds/queue?view=vsts-rest-4.1#request-body

uxhixvfz

uxhixvfz1#

对于queue a build with REST API,您可以使用以下powershell脚本:

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

whitzsjs2#

Marina回答的这个变体对我来说在内部部署TFS 2017服务器上是有效的:

$b= '{"buildNumber":<build id>,"definition":{"id":<build id>}}'
$user="DOMAIN\username"
$token="<PAT token>"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("${user}:${token}"))

$Uri = "https://tfs.mycompany.local/<team-name>/<project-name>/_apis/build/builds?api-version=4.1"
$buildresponse = Invoke-RestMethod -Method Post -UseDefaultCredentials -ContentType application/json -Uri $Uri -Body $b -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
write-host $buildresponse
wkyowqbh

wkyowqbh3#

如果您在Azure Devops服务器上运行PowerShell脚本,则可以利用多个环境变量来自动进行身份验证:

# From https://learn.microsoft.com/en-us/rest/api/azure/devops/build/builds/queue?view=azure-devops-server-rest-6.0
$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/builds?api-version=6.0&definitionId=<definition_id_here>"

Write-Host "URL: $url"

# From https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/powershell?view=azure-devops-2020&tabs=yaml#example-powershell-script-access-rest-api
$pipeline = Invoke-RestMethod -Method 'Post' -Uri $url -ContentType "application/json" -Headers @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"

相关问题