Member-only story
How To Build And Test YAML Pipeline Templates Leveraging CSharp And PowerShell
There is an everlasting debate on how you can test YAML pipeline templates from your local development environment more easily.
Many attempts, feature requests, and good initiatives have already taken place to boost the YAML writing experience.
One of the easiest methods is using the Azure DevOps API. The API exposes a call you can make to perform a so-called “dry-run”. You know that button right, when you press Validate template in the Azure DevOps pipeline editor.
That’s something you can also programmatically do. Let me give you an example:
$Content = (Get-Content -Raw "<YamFileContent>")
$OrganizationName = "<organizationName>"
$ProjectName = "<projectName>"
$PipelineId = "<pipelineId>"
$PersonalAccessToken = "<patToken>"
$Body = @{
"PreviewRun" = "true"
"YamlOverride" = $content
}
$Url = "https://dev.azure.com/$OrganizationName/$ProjectName/_apis/pipelines/$PipelineId/runs?api-version=7.0"
$Arguments = @{
Method = "POST"
Uri = $Url
Body = $Body | ConvertTo-Json
ContentType = "application/json"
Headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PersonalAccessToken)")) }
}
Invoke-RestMethod @Arguments
# Or use the VSTeam…