Skip to content

Instantly share code, notes, and snippets.

@sheldonhull
Created August 22, 2024 20:40
Show Gist options
  • Save sheldonhull/a8b01f2d821224e2b8c7d6c845853339 to your computer and use it in GitHub Desktop.
Save sheldonhull/a8b01f2d821224e2b8c7d6c845853339 to your computer and use it in GitHub Desktop.
render an expanded template view of the azure pipeline yaml for debugging without running. could use refinement, but wanted to capture this cause was a bit confusing with the API initially.
function Invoke-PipelinePreview {
param (
[string]$organization,
[string]$project,
[int]$pipelineId,
[int]$pipelineVersion,
[switch]$previewRun,
[string]$yamlOverride
)
$headers = @{
Authorization = "Basic $([System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("dev:${ENV:AZURE_DEVOPS_EXT_PAT}")))"
}
[string]$PipelineVersionArgs = ''
if ($PipelineVersion) {
$PipelineVersionArgs = "&pipelineVersion=$pipelineVersion"
}
$uri = "https://dev.azure.com/${organization}/${project}/_apis/pipelines/${pipelineId}/preview?api-version=6.1-preview.1${PipelineVersionArgs}"
$invokeRestMethodSplat = @{
Method = 'Post'
Uri = $uri
Headers = $headers
Body = (
@{
previewRun = $true
# resources = @{}
# stagesToSkip = @()
# templateParameters = @{}
# variables = @{}
yamlOverride = $yamlOverride
} | ConvertTo-Json -Depth 10
)
ContentType = 'application/json'
}
$response = Invoke-RestMethod @invokeRestMethodSplat
$originalPipelineName = 'rendered.yaml'
$outputFile = [io.path]::combine('.artifacts', "preview.${originalPipelineName}")
$null = New-Item -Path '.artifacts' -ItemType Directory -ErrorAction SilentlyContinue
Set-Content -Path $outputFile -Encoding utf8 -Force -Value $response.finalYaml
Write-Output "Preview saved to $outputFile"
}
$InvokePipelinePreview = @{
organization = ''
project = ''
pipelineId = 1393
yamlOverride = (Get-Content -Raw 'pipelines/deploy.yaml')
}
Invoke-PipelinePreview @InvokePipelinePreview
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment