Skip to content

Instantly share code, notes, and snippets.

@teyc
Created May 18, 2025 11:18
Show Gist options
  • Save teyc/18badbf1a80a9c2c62bdfad9175ca144 to your computer and use it in GitHub Desktop.
Save teyc/18badbf1a80a9c2c62bdfad9175ca144 to your computer and use it in GitHub Desktop.
Expands Azure Devops Variables
[CmdletBinding()]
param (
[parameter(mandatory)][string]$EntryYamlPath
)
function Resolve-VariableTemplates {
param (
[string]$EntryYamlPath
)
$resolvedVariables = @{}
function Resolve-YamlFile {
param (
[string]$YamlPath,
[string]$BaseDirectory
)
$fullPath = if ([System.IO.Path]::IsPathRooted($YamlPath)) {
$YamlPath
} else {
Join-Path -Path $BaseDirectory -ChildPath $YamlPath
}
if (-not (Test-Path $fullPath)) {
throw "Template file not found: $fullPath"
}
$yamlContent = Get-Content $fullPath -Raw
$parsed = ConvertFrom-Yaml $yamlContent
$variables = $parsed.variables
if ($variables -is [System.Collections.Generic.List[object]]) {
foreach ($var in $variables) {
if ($var.template) {
# Nested template
Resolve-YamlFile -YamlPath $var.template -BaseDirectory (Split-Path $fullPath)
}
elseif ($var.name) {
$resolvedVariables[$var.name] = $var.value
}
}
}
elseif ($variables -is [hashtable]) {
foreach ($key in $variables.Keys) {
$resolvedVariables[$key] = $variables[$key]
}
}
}
function Expand-Expressions {
param (
[hashtable]$variables
)
$expanded = @{}
foreach ($key in $variables.Keys) {
$value = $variables[$key]
if ($value -is [string]) {
# Look for ${{ ... }} patterns
$value = [regex]::Replace($value, '\${{\s*(.*?)\s*}}', {
param($match)
$expression = $match.Groups[1].Value
if ($expression -like 'variables.*') {
$varName = $expression -replace '^variables\.', ''
return $variables[$varName]
} else {
return $match.Value # Leave it unchanged
}
})
}
$expanded[$key] = $value
}
return $expanded
}
Resolve-YamlFile -YamlPath $EntryYamlPath -BaseDirectory "."
$expandedVariables = Expand-Expressions -variables $resolvedVariables
return $expandedVariables
}
Resolve-VariableTemplates $EntryYamlPath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment