通过API运行Azure管道

时间:2020-01-22 04:55:25

标签: powershell api azure-pipelines

我做了一些事情来通过API在Azure中排队/构建管道。现在,我需要通过API运行管道。因此,我附上了用于构建/排队管道的代码。

    $body = '
    { 
            "definition": {
                "id": 1
            } 
    }
    '
    write-host "`n"
    $bodyJson=$body 
    write-host $bodyJson -ForegroundColor Cyan

    Write-Output $bodyJson | ConvertFrom-JSON
    write-output $bodyJson
    $bodyString=$bodyJson | ConvertTo-Json 
    write-host $bodyString -foregroundcolor green

    $user="name"

    $personalToken = "token"

    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personaltoken)"))

    $Uri = "https://dev.azure.com/demoworldDemoworld/talentstogether/_apis/build/builds?api-version=5.1"
    $buildresponse = Invoke-RestMethod -Method Post -UseDefaultCredentials -ContentType application/json -Uri $Uri -Body $bodyJson -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
    write-host $buildresponse

请帮助我以类似的方式通过API运行管道。

1 个答案:

答案 0 :(得分:1)

请尝试以下脚本:

Param(
   [string]$orgurl = "https://dev.azure.com/{organization}",
   [string]$projectName = "0508-t",
   [string]$BuildDefinitionId = "166",
   [string]$user = "",
   [string]$token = "PAT"
)

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

function CreateJsonBody
{

    $value = @"
  {
  "definition": {
    "id": $BuildDefinitionId
  }

}
"@

 return $value
}

$json = CreateJsonBody

$uri = "$($orgurl)/$($projectName)/_apis/build/builds?api-version=5.1"

$buildresponse  = Invoke-RestMethod -Uri $uri -Method Post -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
Write-Host $buildresponse