I need to validate the YAML file in azure pipeline either via PowerShell or CMD, if the YAML file is invalid then it should fail the task.
Can you please suggest some inputs on how to achieve it.
I need to validate the YAML file in azure pipeline either via PowerShell or CMD, if the YAML file is invalid then it should fail the task.
Can you please suggest some inputs on how to achieve it.
You can use powershell-yaml module to validate YAML file as below:
steps:
- pwsh: Install-Module powershell-yaml -Force
- pwsh: |
$content = Get-Content -Path stackoverflow\80-yaml\test.yaml -Raw
$correct = true
try {
$obj = ConvertFrom-Yaml $content
Write-Host $obj
}
catch {
Write-Host "An error occurred:"
$correct = false
}
Write-Host $correct
But please remember about setting $ErrorActionPreference
Just like Shayki Abramczyk commented not sure if you want to azure-pipelines.yml file or another .yml file
azure-pipelines.ymlThere is a server-side API for this :
POST to dev.azure.com/<org>/<project>/_apis/pipelines/<pipelineId>/runs?api-version=5.1-preview with a JSON body like this:
{
"PreviewRun": true,
"YamlOverride": "
# your new YAML here, optionally
"
}
The response will contain the rendered YAML. More details you could refer link here: Expose YAML validation features as standalone tool
.yml fileYou could either use a script in powershell task or some 3rd-party extension Files Validator to handle this.