Home > database >  Is there a way I can validate YAML files on Github?
Is there a way I can validate YAML files on Github?

Time:03-03

I am currently working on a Ruby on Rails App. I have a directory filled with different Yaml File that gets edited from time to time. Anytime a Developer accidentally merge with invalid Yaml syntax to the Main branch. The entire Application breaks.

Is there anyway i can setup a Yaml Validator on Github that checks for validity of my Yaml files in a specific directory in my Repo and stop a PR from being merged into Main if this Yaml Validator check fails?

CodePudding user response:

You can use a YAML linter in a GitHub Action that runs for every pull request. yamllint is already installed on Ubuntu-based GitHub runners according to the docs.

Here is a basic workflow:

name: Validate-YAML

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  validate-yaml:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Validate YAML file
        run: yamllint abc.yml

CodePudding user response:

A more generic approach without using Github Hooks.

One option, which I currently use, is overcommit. It has a lot of hooks that can be executed before a git commit (amongst others).

For your scenario, this should work.

YamlSyntax:
   enabled: true
   description: 'Check YAML syntax'
   required_library: 'yaml'
   include:
     - '**/*.yaml'
     - '**/*.yml'

Here's a list of those hooks: https://github.com/sds/overcommit/blob/master/config/default.yml.

You can add rubocop, sorbet, etc. This will only work if the entire team has it installed, otherwise those who have it will most likely need to fix things left out by the other team members.

  • Related