Run Azure Pipeline with REST call

You can invoke Azure Pipeline via REST API.

Here's such invocation implemented with PowerShell.

$creds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("user:$PAT"))
$basicAuth = "Basic $creds"

$Headers = @{
    Authorization = $basicAuth
}

$Payload = [PSCustomObject]@{
    resources = [PSCustomObject]@{ 
        repositories =[PSCustomObject]@{ 
            self = [PSCustomObject] @{ 
                refName = "refs/heads/$Branch"
            }
        }
    }
    variables = [PSCustomObject]@{
        DeploymentId = [PSCustomObject]@{
            value = "$Deployment"
        }
    }
} | ConvertTo-Json -Depth 10

$Payload

$url = "https://dev.azure.com/<organisation>/$Project/_apis/pipelines/$Pipeline/runs?api-version=5.1-preview.1"
$url

$resp = Invoke-WebRequest -Method Post -Uri $url -Headers $Headers -Body $Payload -ContentType "application/json" -UseBasicParsing
$resp

I've used it as a script to create Octopus Custom Step Template. The undeclared variables, like PAT, Branch, Deployment were either declared in Library Variable Sets, passed in to Octopus deployment as parameter or provided by Octopus.

comments powered by Disqus