yamlazure-devops-extensionsyamllint

How to lint yaml file in azuredevops task


In continuation to the solution provided in stack overflowWe are using an input yaml file in our applications repository to get azure apims policy parameters from our developers, and then using a yaml parser we converted this yaml inputs to the bash variable and there after through azuredevops tasks generated policy.xml file and updating the apis. This works for the tested scenario.

But we faced some unexpected behavior when the yaml file is having any intendation issues or any syntax/format issues due to copying in windows or browsers. So looking for a best way to lint or format the input.Yaml file before passing it over azuredevops task. Is there any azuredevops tools or li ux offline extensions to achieve this?


Solution

  • Is there any azuredevops tools or li ux offline extensions to achieve this?

    I am afraid that Azure DevOps doesn't have built-in tasks or extensions can lint or format the YAML files.

    To meet your requirement, you can use the following tools to lint or format yaml files.

    1.Lint YAML file: yamllint

    You can install the yamllint tool and use it to lint yaml file.

    For example:

    steps:
    - bash: |
       pip install --user yamllint
       
       yamllint test.yml 
      displayName: 'lint yaml file'
    

    It will show the error in Pipeline log when the YAML format has issues.

    For example:

    enter image description here

    2.Format YAML file: yamlfmt

    You can install the yamlfmt tool in Azure Pipeline and use it to format the yaml file.

    For example:

    steps:
    - bash: |
       go install github.com/google/yamlfmt/cmd/yamlfmt@latest
       
       ~/go/bin/yamlfmt test.yml
       
       
      displayName: 'format yaml file'
    

    It will format the yaml file and let it pass the yaml lint step.

    For example:

    enter image description here

    These two tools supports using configuration to custom the rule when formatting or linting the yaml files.