sapher / gitlab-ci-component-linter

Linter for Gitlab CI Component project

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Gitlab CI Component Linter

GitHub Release

Linter for Gitlab CI components. Validate that project follow the guidelines defined by Giltab for CI components.

This tool name is not that great, if you have a better idea, please open an issue.

Rules

Rule Message Severity
missing-root-readme No README.md file found in root directory error
missing-root-templates-dir No templates directory found in root directory error
wrong-yaml-file-extension YAML files must have .yml extension, not .yaml error

Install

You can either install it using go install :

go install github.com/sapher/gitlab-ci-component-linter

Or download the binary from the release page.

Usage

CLI

Validate Gitlab CI Component files against set of rules
Workdir is the directory where the Gitlab CI Component project is located

Usage:
  gitlab-ci-component-linter [workdir] [flags]

Flags:
  -h, --help            help for gitlab-ci-component-linter
      --only-failures   Only display checks that failed
  -o, --output string   Output format, one of: json, yaml, junit, table, none (default "table")
  -s, --soft-fail       Run checks and exit with 0 even if errors are found

Git hook

You can use this linter as git hook with pre-commit to validate your changes before commiting them.

You need the executable to be in your PATH and then you can add this to your .pre-commit-config.yaml:

repos:
  - repo: https://github.com/sapher/gitlab-ci-component-linter
    rev: v1.0.1
    hooks:
      - id: gitlab-ci-component-linter

Or you could use the docker version:

repos:
  - repo: https://github.com/sapher/gitlab-ci-component-linter
    rev: v1.0.1
    hooks:
      - id: gitlab-ci-component-linter-docker

Gitlab CI

You can use this linter as a job in your Gitlab CI pipeline.

stages:
  - lint

lint:
  stage: lint
  image: ghrc.io/sapher/gitlab-ci-component-linter:latest
  script:
    - gitlab-ci-component-linter $CI_PROJECT_DIR --output junit | tee junit.xml
  artifacts:
    reports:
      junit: junit.xml

Contribute

Feel free to open an issue or a pull request if you want to contribute.

Add new rule

To add a new rule, you need to create a new function in pkg/linter/rules.go that implements the type LinterRuleFunc func(linter *Linter) (LinterResult, error) interface.

Like for example:

func NewRule(linter *Linter) (LinterResult, error) {
	result := LinterResult{
		Name:     "new-rule",
		Success:  false,
		Message:  "This is a new rule",
		Metadata: map[string]interface{}{},
		Severity: SeverityWarning,
	}
	return result, nil
}

Then you need to add it to the ruleFuncs array in pkg/linter/rules.go:

var ruleFuncs = []LinterRuleFunc{
  // ... omitted
  NewRule,
}

Then you are good to go, you can run the linter and see your new rule in action.

About

Linter for Gitlab CI Component project


Languages

Language:Go 93.6%Language:Shell 4.8%Language:Makefile 1.1%Language:Dockerfile 0.4%