Simple YAML Linter/Validator Workflow for GitHub Actions
CodeWithCaen

CodeWithCaen @codewithcaen

About: Background in freelance frontend, currently a fullstack Laravel/PHP engineer with a strong focus on backend and cybersecurity. Created Laravel-based static site generator HydePHP.

Location:
Sweden
Joined:
Apr 22, 2022

Simple YAML Linter/Validator Workflow for GitHub Actions

Publish Date: Jun 23 '24
3 5

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

Comments 5 total

  • Alex Pliutau
    Alex PliutauJul 5, 2024

    Great write-up! We also have a bunch of articles on Github Actions in our Newsletter, check it out - packagemain.tech/p/github-actions-...

  • Ismael Hommani
    Ismael HommaniNov 15, 2024

    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

    • CodeWithCaen
      CodeWithCaenNov 15, 2024

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

      • Ismael Hommani
        Ismael HommaniNov 15, 2024

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

        • Ismael Hommani
          Ismael HommaniNov 16, 2024

          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
Add comment