How to QUEUE a new build using VSTS REST API How to QUEUE a new build using VSTS REST API powershell powershell

How to QUEUE a new build using VSTS REST API


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

$body = '{         "definition": {            "id": number        } }'$bodyJson=$body | ConvertFrom-JsonWrite-Output $bodyJson$bodyString=$bodyJson | ConvertTo-Json -Depth 100Write-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


This variant of Marina's answer worked for me against an on-prem TFS 2017 server:

$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