Linting Via GitHub Actions

Reading Time: 2 minutes

Before moving towords to the linting via GitHub Actions. Lets see first that what is linting and why it is important. And How we can achieve that through GitHub Actions. i.e the “Linting Via GitHub Actions”

What is Linting ?

It is the process of automatically checking your source code for programmatic and stylitic error. This is done by lint tool ,to whom we can call linter. There are differant linter are present for differant types of programming languages. Like and xml, yaml, java also for the Dockerfile and many more….

Why Linting is important ?

It is important to reduce errors and improve the overall quality of your code. Using lint tools can help you accelerate development and reduce costs by finding errors earlier.

Why to use github actions rather than lint tool ?

Whenever we use lint tool we need to execute some everytime when we’ll make changes to the code. It would become a repetative task. In SRE term we call it as “TOIL”. SO to reduce this toil we can autimate linting via GitHub Actions.
So, before wasting any time lets see some example how we can use github-actions for linting differant languages.

1. Hadolint:

Hadolint is a tool for performing linting on Dockerfile. You can run hadolint locally to lint your Dockerfile. For that you need to download “hadolint” first.

But using the following github action into your github workfow , whenever you make chages/push your code the linting will get automatically performed.

name: HadoLint

on:
  push:
    branches: [ branch-name ]
  
jobs:
  job1:
    name: build
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Hlint
        uses: hadolint/hadolint-action@v1.5.0
        with:
         dockerfile: Dockerfile

2. Yamllint:

A linter for YAML files.

yamllint does not only check for syntax validity, but for weirdnesses like key repetition and cosmetic problems such as lines length, trailing spaces, indentation, etc.

You can run yamllint file.yml” locally to lint your Dockerfile.

Following github action into your github workfow , whenever you make chages/push your code the linting will get automatically performed.

name: 'YamlLint'
on:
  push:
    branches: [ branch-name ]
jobs:
  yamllint:
    name: 'Yamllint'
    runs-on: ubuntu-latest
    steps:
      - name: 'Checkout'
        uses: actions/checkout@master
      - name: 'Yamllint'
        uses: karancode/yamllint-github-action@master
        with:
          yamllint_file_or_dir: './.github/workflows/maven.yml'
          yamllint_strict: false
          yamllint_comment: true
        env:
          GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}

3. Xmllint:

Xmllint is as linter which is used for detecting the errors in both XML and XML pasrser itself.

Locally you can execute the “xmllint –options xmlfile” with differant options and can return output based on that. To see about xmllint in details refere this

Following github action into your github workfow , whenever you make chages/push your code the lint operation will get automatically performed.

name: XmlLint

on:
  push:
    branches: branch-name

jobs:
  xml-linters:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@master
    - name: Download schema
      run: wget https://apps.nextcloud.com/schema/apps/info.xsd
    - name: Lint info.xml
      uses: ChristophWurst/xmllint-action@v1
      with:
        xml-file: ./pom.xml
        xml-schema-file: ./info.xsd

To see the actual implementaion of above code you can see my repo “linting-via-github-action

If you want to learn how GitHub action Works see this

Referances:

To see more details aboubt the hadolint and yamllint. Plz refer the following

Written by 

Sakshi Gawande is a software consultant at "KNOLDUS" having more than 2 years of experience. She's working as a DevOps engineer. She always wants to explore new things and solve problems herself. on personal, she likes dancing, painting, and traveling.