DEV Community

CodeWithCaen
CodeWithCaen

Posted on • Originally published at tips.desilva.se

Simple YAML Linter/Validator Workflow for GitHub Actions

Here's a quick tip if you work with YAML on GitHub!

The GitHub Actions Ubuntu runners comes with yamllint installed, meaning it's super simple to create linting/validating workflows to ensure your YAML is valid!

name: Validate YAML

on:
  push:
  pull_request:

jobs:
  validate-yaml:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Validate YAML file
        run: yamllint my-file.yml
Enter fullscreen mode Exit fullscreen mode

Top comments (5)

Collapse
 
ismael_hommani profile image
Ismael Hommani

And one day it is not available anymore ^^.
I'd rather be explicit about the dependency you need rather than relying on "by conincedence" installed tools on the runner.
Consider your pipeline as an application. Follow the factor II from 12 factors: 12factor.net/dependencies

Collapse
 
codewithcaen profile image
CodeWithCaen

Is it no longer available, or are you speaking generally?

Collapse
 
ismael_hommani profile image
Ismael Hommani

Generally but it happens to me once for a different tool.

Thread Thread
 
ismael_hommani profile image
Ismael Hommani

For instance, even if it works rightaway on the runner I avoid to do that:

jobs:
  format_github_action_files:
    runs-on: ubuntu-22.04
    steps:
      - name: Check out repository
        uses: actions/checkout@v4

      - name: format gha files'
        id: gha_fmt
        run: yamlfmt ${{ inputs.gha_definition_files_folder }}
Enter fullscreen mode Exit fullscreen mode

but rather do that:

jobs:
  format_github_action_files:
    runs-on: ubuntu-22.04
    steps:
      - name: Check out repository
        uses: actions/checkout@v4

      - uses: actions/setup-go@v5
        with:
          go-version: '>=1.17.0'

      - name: install yamlfmt
        id: yamlfmt_install
        run: go install github.com/google/yamlfmt/cmd/yamlfmt@latest

      - name: format gha files'
        id: gha_fmt
        run: |-
          yamlfmt ${{ inputs.gha_definition_files_folder }}
Enter fullscreen mode Exit fullscreen mode

Some comments may only be visible to logged-in visitors. Sign in to view all comments.