Skip to content

Latest commit

 

History

History
206 lines (140 loc) · 3.8 KB

README.md

File metadata and controls

206 lines (140 loc) · 3.8 KB

Pre-commit Hooks

This repository uses pre-commit to automatically run checks before commits.

Installation

Pre-commit is already installed in the devcontainer. To enable the hooks:

pre-commit install
pre-commit install --hook-type commit-msg

What Gets Checked

General

  • Trailing whitespace
  • End of file fixes
  • Large files detection
  • Merge conflict markers
  • Private key detection

Shell Scripts

  • ShellCheck linting
  • Shebang validation
  • Execute permissions

Terraform

  • Format checking (terraform fmt)
  • Validation (terraform validate)
  • Documentation generation
  • Security scanning (checkov)
  • Linting (tflint)

Ansible

  • Ansible-lint for playbooks
  • YAML syntax validation

Python

  • Code formatting (black)
  • Style checking (flake8)
  • Import sorting

Docker

  • Dockerfile linting (hadolint)

Documentation

  • Markdown linting
  • YAML linting

Security

  • Secret detection
  • Private key scanning

Git

  • Conventional commit message format

Usage

Automatic (Recommended)

After installation, hooks run automatically on git commit:

git add .
git commit -m "feat: add new feature"
# Pre-commit hooks run automatically

Manual Run

Run checks on all files:

pre-commit run --all-files

Run specific hook:

pre-commit run terraform-fmt --all-files
pre-commit run shellcheck --all-files

Update Hooks

Update to latest versions:

pre-commit autoupdate

Bypassing Hooks

Not recommended, but if needed:

git commit --no-verify -m "emergency fix"

Configuration

Hooks are configured in .pre-commit-config.yaml. To modify:

  1. Edit .pre-commit-config.yaml
  2. Update hook versions or add new hooks
  3. Run pre-commit install again
  4. Test with pre-commit run --all-files

Troubleshooting

Hook fails to run

# Reinstall hooks
pre-commit uninstall
pre-commit install

Clean hook cache

pre-commit clean
pre-commit run --all-files

Skip specific files

Add to .pre-commit-config.yaml:

exclude: |
  (?x)^(
      path/to/exclude/|
      specific-file.txt
  )

CI/CD Integration

CI does not run pre-commit itself - it runs the linters that matter for this repository directly, in the lint job of .github/workflows/build.yml: bash -n and shellcheck over every shell script, a JSON parse over every JSON file, and hadolint over the Dockerfile.

Dockerfile lint rules live in .hadolint.yaml and are shared, so the hadolint-docker hook here and the CI job agree by construction.

Common Issues

Terraform validation fails

Ensure Terraform is initialized:

cd terraform/
terraform init

Ansible-lint fails

Check .ansible-lint configuration or update playbook syntax.

Hadolint fails

Fix Dockerfile issues or add ignore rules:

args: ['--ignore', 'DL3008', '--ignore', 'DL3009']

Hook List

Full list of enabled hooks:

Hook Purpose Auto-fix
trailing-whitespace Remove trailing spaces ✓
end-of-file-fixer Ensure newline at EOF ✓
check-yaml Validate YAML syntax ✗
check-json Validate JSON syntax ✗
shellcheck Lint shell scripts ✗
terraform-fmt Format Terraform ✓
terraform-validate Validate Terraform ✗
checkov Security scanning ✗
ansible-lint Lint Ansible ✗
black Format Python ✓
flake8 Lint Python ✗
hadolint Lint Dockerfile ✗
markdownlint Lint Markdown ✓
detect-secrets Find secrets ✗
conventional-pre-commit Validate commit msg ✗

Contributing

When adding new file types or tools, update .pre-commit-config.yaml with appropriate hooks.