As mentioned in my previous post about Azure DevOps Local Pipeline Testing, the available method of testing with the Azure DevOps API doesn’t have the feature to merge the YAML Templates. Therefore, I have set out to try and solve this issue..
You can view the full PowerShell script on GitHub.(https://github.com/PureRandom/AzureDevOpsYamlMerge-ps)
Please feel free to advise on more efficient coding and suggestions of other use cases that need to be considered.
Below I will walk through what it currently does as of the date of this post. I have tried to consider most, if not all, of the scenarios that I have come across, but I am sure there are other ways that need to be solved.
To use the script you simply need to pass it the root location of where your YAML is stored and the name of the main YAML file. For Example:
$yamlPath = "C:\Users\pateman.workspace\CodeRepos\" $yamlName = "azurepipeline.yml" $outputPath = processMainPipeline -pipelineYaml $yamlName -rootPath $yamlPath Write-Host "Parsed into $outputPath"
This will read through each line and rebuild the YAML file. As it reads through if it finds a line that contains the template syntax then the processing starts, but only if the template path does not contain the ‘@’ symbol as that is assumed to be a path in a remote repository.
In the processing it will extract the parameters that are being passed to the template. Then getting a copy of the template YAML into a variable, it will start reading this file and rebuilding it. First it will assume the Parameters are set at the top, so it will extract the parameters. If the parameter found has been set by the main YAML then it will do nothing, else it will create the entry and update value from the default property.
Once it has all the parameters it can find and replace these as it goes through the file. Finally insert this now update version of the template into the same slot as where the template reference was in the main YAML.
This is then saved in the root location, where you can use this file in the pipeline testing API.