Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# This configuration file allows our pre-commit hooks to be used with pre-commit: http://pre-commit.com/
---
# This configuration file allows our pre-commit hooks to be used with
# pre-commit: http://pre-commit.com/

- id: terraform-fmt
name: Terraform fmt
Expand All @@ -25,3 +27,29 @@
language: script
pass_filenames: false

- id: helm-lint
name: Helm lint
description: Runs 'helm lint' on Helm charts
entry: hooks/helm-lint.sh
language: script
files: (Chart\.yaml|values.*\.yaml|templates/.*\.yaml)$
exclude: '(^|.*/)(\.terraform|examples)/'
require_serial: true

- id: yamllint
name: YAML Lint
description: Validates YAML syntax and style
entry: hooks/yamllint.sh
language: script
files: \.(yaml|yml)$
exclude: '(^|.*/)(\.terraform|examples)/'
require_serial: false

- id: helm-template-check
name: Helm template check
description: Validates that Helm templates can render without errors
entry: hooks/helm-template-check.sh
language: script
files: (Chart\.yaml|values.*\.yaml|templates/.*\.yaml)$
exclude: '(^|.*/)(\.terraform|examples)/'
require_serial: true
279 changes: 251 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,55 +1,278 @@
# Pre-commit hooks
# Pre-commit Hooks Collection

This repo defines Git pre-commit hooks intended for use with [pre-commit](https://pre-commit.com/). The currently
supported hooks are:
<div align="center">

* **terraform-fmt**: Checks that all Terraform files (`*.tf`) are properly formatted (`terraform fmt --check -diff`).
* **terraform-validate**: Runs `terraform init -backend=false` and then `terraform validate`.
> Notes: directories requiring a private registry and lacking credentials are marked as **skipped** (do not fail the commit). Both hooks ignore `.terraform/` and `examples/`.
**Quality gates for your Infrastructure as Code**
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Replace bold text with proper markdown heading per MD036.

Line 5 uses emphasis (**...**) for what should be a subtitle/heading. Replace with a proper heading format.

-**Quality gates for your Infrastructure as Code**
+### Quality gates for your Infrastructure as Code
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
**Quality gates for your Infrastructure as Code**
### Quality gates for your Infrastructure as Code
🧰 Tools
🪛 markdownlint-cli2 (0.18.1)

5-5: Emphasis used instead of a heading

(MD036, no-emphasis-as-heading)

🤖 Prompt for AI Agents
In README.md around line 5, the subtitle currently uses bold markup (**Quality
gates for your Infrastructure as Code**) which violates MD036; replace the
bold-emphasis with a proper Markdown heading (e.g., use a suitable heading level
like "## Quality gates for your Infrastructure as Code" or "#" / "###" as
appropriate for document structure) so the line is a real heading rather than
bolded text.


## General Usage
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://pre-commit.com/)

In each of your repos, add a file called `.pre-commit-config.yaml` with the following contents:
</div>

---

## Overview

This repository provides a curated collection of **Git pre-commit hooks** designed to enforce best practices and catch issues early in your Infrastructure as Code (IaC) workflows. By integrating these hooks into your development process, you can ensure code quality, consistency, and compliance before changes ever reach your repository.

### Why Use Pre-commit Hooks?

- **Catch issues early** - Identify problems before they reach CI/CD
- **Fast feedback loop** - Get instant validation on your local machine
- **Enforce standards** - Maintain consistent code quality across teams
- **Prevent broken commits** - Block commits that don't meet your criteria
- **Team collaboration** - Share the same quality gates with everyone

---

## Available Hooks

### Terraform Hooks

| Hook | Description | What it does |
|------|-------------|--------------|
| `terraform-fmt` | **Format checker** | Verifies all `.tf` files are properly formatted using `terraform fmt --check -diff` |
| `terraform-validate` | **Syntax validator** | Runs `terraform init -backend=false` followed by `terraform validate` to catch configuration errors |

> **Note:** Directories requiring private registries without credentials are automatically skipped. Both hooks ignore `.terraform/` and `examples/` directories.

### Helm Hooks

| Hook | Description | What it does |
|------|-------------|--------------|
| `helm-lint` | **Chart linter** | Validates Helm charts using `helm lint` to check for common issues and best practices |
| `helm-template-check` | **Template validator** | Renders templates with `helm template` to ensure they generate valid Kubernetes manifests |

> **Note:** Helm hooks automatically discover charts by locating `Chart.yaml` files in your repository.

Comment thread
LautaroTorchia marked this conversation as resolved.
---

## Quick Start

### Prerequisites

First, ensure you have [pre-commit](https://pre-commit.com/) installed on your system:

```bash
# macOS
brew install pre-commit

# Linux (recommended)
pipx install pre-commit

# Linux (alternative)
pip install --user pre-commit

# Windows (WSL or Git Bash)
pip install pre-commit
```

### Installation

### Step 1: Add the configuration file

Create a `.pre-commit-config.yaml` file in the root of your repository:

```yaml
repos:
- repo: git@github.com:craftech-io/pre-commit.git # or https://github.com/craftech-io/pre-commit.git
rev: <VERSION>
- repo: git@github.com:craftech-io/pre-commit.git
# or use: https://github.com/craftech-io/pre-commit.git
rev: <VERSION> # Use the latest release tag
hooks:
# Terraform hooks
- id: terraform-fmt
- id: terraform-validate
verbose: true
verbose: true # Show detailed output

# Helm hooks
- id: helm-lint
- id: helm-template-check
```

Next, have every developer:
> **Tip:** Replace `<VERSION>` with the latest release tag (e.g., `v1.0.0`). Check the [releases page](https://github.com/craftech-io/pre-commit/releases) for available versions.

**Step 2: Install the hooks**

1. Install [pre-commit](https://pre-commit.com/#install).
- macOS: `brew install pre-commit`
- Linux: `pipx install pre-commit` (or `pip install --user pre-commit`)
2. Run `pre-commit install` in the repo.
Run this command in your repository:

```bash
pre-commit install
```

That's it! Now every time you commit a code change (`.tf` file), the hooks in the `hooks:` config will execute.
If any hook fails, the commit is aborted; if all pass, the commit succeeds.
**Step 3: You're all set!**

## Running Against All Files At Once
Now, every time you run `git commit`, the configured hooks will automatically execute. If any hook fails, the commit will be blocked, allowing you to fix issues before they're committed.

### Example: Formatting and validating all files
---

If you'd like to run the hooks across the whole repo (useful the first time), you can run:
## Usage

### Automatic Validation (Recommended)

Once installed, hooks run automatically on every commit:

```bash
# Check formatting for all Terraform files
pre-commit run terraform-fmt --all-files
git add .
git commit -m "feat: add new infrastructure"
# Hooks will run automatically here!
```

# Validate all Terraform directories
pre-commit run terraform-validate --all-files
### Manual Execution

Run hooks manually without committing:

# Or run every configured hook across the repo
```bash
# Run all configured hooks on staged files
pre-commit run

# Run a specific hook on staged files
pre-commit run terraform-fmt

# Run hooks on all files in the repository
pre-commit run --all-files

# Run a specific hook on all files
pre-commit run terraform-validate --all-files
```

### Common Commands

```bash
# Run all hooks with verbose output
pre-commit run --all-files -v

# Run only Terraform hooks
pre-commit run terraform-fmt --all-files
pre-commit run terraform-validate --all-files

# Run only Helm hooks
pre-commit run helm-lint --all-files
pre-commit run helm-template-check --all-files

# Update hooks to the latest version
pre-commit autoupdate

# Temporarily bypass hooks (not recommended!)
git commit --no-verify
```

---

## Advanced Configuration

### Customizing Hook Behavior

You can customize hooks in your `.pre-commit-config.yaml`:

```yaml
repos:
- repo: git@github.com:craftech-io/pre-commit.git
rev: <VERSION>
hooks:
- id: terraform-fmt
# Run on specific file patterns only
files: ^modules/

- id: terraform-validate
# Exclude specific directories
exclude: ^(examples|tests)/

- id: helm-lint
# Always show verbose output
verbose: true

- id: helm-template-check
# Run even if files haven't changed
always_run: false
```

### Combining with Other Hooks

Mix these hooks with other pre-commit hooks for comprehensive validation:

```yaml
repos:
# This repository's hooks
- repo: git@github.com:craftech-io/pre-commit.git
rev: <VERSION>
hooks:
- id: terraform-fmt
- id: helm-lint

# Additional hooks from other sources
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
```

> Tip: for detailed output on demand, use `-v`, e.g. `pre-commit run -v terraform-validate --all-files`.
---

## Contributing

We welcome contributions! Here's how you can help improve this project:

### Adding New Hooks

1. **Create the hook script** in the `hooks/` directory
2. **Make it executable**: `chmod +x hooks/your-hook.sh`
3. **Add entry** to `.pre-commit-hooks.yaml`
4. **Test thoroughly** with various scenarios
5. **Submit a pull request** with a clear description

### Testing Your Changes

Before submitting a PR, test your hooks:

```bash
# Test on a specific file
bash hooks/your-hook.sh path/to/test/file

# Test with pre-commit
pre-commit try-repo /path/to/your/local/repo your-hook-id --verbose --all-files
```

### Development Guidelines

- Follow existing code style and structure
- Include error handling and clear error messages
- Add colorized output for better readability
- Write descriptive commit messages
- Update documentation (README, comments)
- Test on multiple scenarios (success, failure, edge cases)

### Reporting Issues

Found a bug or have a suggestion? Please [open an issue](https://github.com/craftech-io/pre-commit/issues) with:

- **Clear description** of the problem or enhancement
- **Steps to reproduce** (for bugs)
- **Expected vs actual behavior**
- **Environment details** (OS, tool versions)

---

## Additional Resources

- [Pre-commit Documentation](https://pre-commit.com/)
- [Terraform Documentation](https://www.terraform.io/docs)
- [Helm Documentation](https://helm.sh/docs/)
- [Pre-commit Hook Examples](https://github.com/pre-commit/pre-commit-hooks)

---

## License

This code is released under the Apache 2.0 License. Please see [LICENSE](LICENSE) for more details.
This project is released under the **Apache 2.0 License**.


<div align="center">

**[Back to Top](#pre-commit-hooks-collection)**

Made by Craftech

</div>
Loading