This document covers how to set up and use pre-commit hooks with racktopia shared tooling.
Pre-commit is a framework for managing git hooks that automatically run checks before commits. It ensures code quality and consistency across all repositories by running linters, formatters, and custom validation tools.
Using pip:
pip install pre-commitUsing Homebrew (macOS/Linux):
brew install pre-commitUsing conda:
conda install -c conda-forge pre-commit# Navigate to your repository
cd your-repo
# Install the hooks
pre-commit installOnce installed, hooks run automatically on git commit. Dependencies are checked just-in-time with helpful
installation instructions if anything is missing.
Create .pre-commit-config.yaml in your repository root:
---
repos:
- repo: https://github.com/racktopia/.github
rev: main # or specific commit hash
hooks:
- id: racktopia-standards # Always include this first
# Keep remaining hooks in alphabetical order:
- id: ansible-lint # if you have ansible/ directory
- id: ansible-syntax-check # if you have ansible/ directory
- id: detect-secrets # if you have sensitive config files
- id: markdownlint # if you have .md files
- id: yamllint # if you have .yml/.yaml files📖 For detailed hook information, see: Hooks Documentation
---
repos:
- repo: https://github.com/racktopia/.github
rev: main
hooks:
- id: racktopia-standards
- id: markdownlint
- id: yamllint---
repos:
- repo: https://github.com/racktopia/.github
rev: main
hooks:
- id: racktopia-standards
- id: ansible-lint
- id: ansible-syntax-check
- id: detect-secrets
- id: markdownlint
- id: yamllint---
repos:
- repo: https://github.com/racktopia/.github
rev: main # Must use 'main' to avoid circular dependency
hooks:
- id: racktopia-standards
- id: detect-secrets
- id: markdownlint
- id: yamllintHooks run automatically when you commit:
git add .
git commit -m "Your commit message"
# Hooks run automatically hereRun all hooks on all files:
pre-commit run --all-filesRun specific hook on all files:
pre-commit run markdownlint --all-files
pre-commit run yamllint --all-filesRun hooks only on staged files:
pre-commit runSometimes you need to commit without running hooks:
git commit --no-verify -m "Emergency fix"Use sparingly - bypassing hooks can introduce issues.
Check for updates:
pre-commit autoupdateThe racktopia-standards hook will fail if hooks are outdated, ensuring you stay current with the latest shared tooling.
Review changes before updating:
# See commit summaries (commands provided by racktopia-standards hook)
git log --oneline OLD_REV..NEW_REV --no-merges
# See actual code changes
git diff OLD_REV..NEW_REVIf you encounter issues, reinstall hooks:
pre-commit uninstall
pre-commit installIf hooks seem outdated after updates:
pre-commit clean
pre-commit installAll racktopia hooks use a "just-in-time" dependency approach:
- Hook runs and checks if required tool is installed
- If missing, hook fails with clear installation instructions
- Install tool using the provided command
- Re-run commit - hook passes on subsequent runs
The hooks may require these tools:
# Markdown linting
npm install -g markdownlint-cli
# YAML linting
pip install yamllint
# Ansible tools
pip install ansible ansible-lint
# Secret detection
pip install detect-secretsDon't install everything upfront - only install tools when hooks request them for your specific repository.
Hooks automatically run only on relevant files:
racktopia-standards- Always runs (validates configuration)markdownlint- Only.mdfilesyamllint- Only.yml/.yamlfilesansible-lint- Only files underansible/directoryansible-syntax-check- Only files underansible/directorydetect-secrets- Only sensitive configuration files
If no matching files are staged, the hook is skipped. This allows all repositories to use the same configuration while only running applicable checks.
Check file patterns:
# See what files match hook patterns
pre-commit run --all-files --verboseForce hook execution:
pre-commit run hook-name --all-filesReinstall pre-commit:
pip install --upgrade pre-commit
# or
brew upgrade pre-commitClear and reinstall hooks:
pre-commit clean
pre-commit install --install-hooksMake sure hook scripts are executable:
chmod +x .git/hooks/pre-commitSkip hooks for large commits:
git commit --no-verify -m "Large refactor"Run hooks only on changed files:
pre-commit run # instead of --all-filesYou can add additional hooks alongside racktopia hooks:
repos:
- repo: https://github.com/racktopia/.github
rev: main
hooks:
- id: racktopia-standards
- id: markdownlint
- id: yamllint
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixerSome hooks can be configured with additional arguments:
- id: markdownlint
args: ['--config', '.markdownlint.custom.json']Pre-commit works great in CI/CD pipelines:
# GitHub Actions example
- name: Run pre-commit hooks
uses: pre-commit/action@v3.0.1- Always include
racktopia-standardsfirst - ensures configuration compliance - Keep hooks in alphabetical order (except racktopia-standards)
- Only include relevant hooks - based on file types in your repository
- Review changes when updating hook revisions
- Test locally before pushing configuration changes
- Don't bypass hooks unless absolutely necessary
- Install dependencies as requested rather than proactively
- Keep configurations consistent across similar repositories
- Hook-specific issues: See Hooks Documentation
- Configuration validation: The
racktopia-standardshook provides detailed guidance - Pre-commit framework: Official documentation
- Tool-specific help: Each tool provides
--helpflag for detailed options
If migrating from older validation scripts:
- Remove old validation scripts (like
scripts/setup.sh) - Create
.pre-commit-config.yamlwith racktopia hooks - Install pre-commit and hooks
- Test thoroughly with
pre-commit run --all-files - Update CI/CD to use pre-commit instead of custom scripts
- Document changes for team members