0

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.

Matt
  • 3,658
  • 3
  • 14
  • 27
Raghu Hoskote
  • 117
  • 2
  • 14

2 Answers2

1

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

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
1

Just like Shayki Abramczyk commented not sure if you want to azure-pipelines.yml file or another .yml file

For azure-pipelines.yml

There 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

For other .yml file

You could either use a script in powershell task or some 3rd-party extension Files Validator to handle this.

PatrickLu-MSFT
  • 49,478
  • 5
  • 35
  • 62