-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from mineiros-io/initial-module
pre-commit hooks for terraform fmt, validate and tflint
- Loading branch information
Showing
7 changed files
with
125 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Configuring our pre-commit hooks to be used with pre-commit: http://pre-commit.com/ | ||
|
||
# https://www.terraform.io/docs/commands/fmt.html | ||
- id: terraform-fmt | ||
name: Terraform fmt | ||
description: The terraform fmt command is used to rewrite Terraform configuration files to a canonical format and style. This command applies a subset of the Terraform language style conventions, along with other minor adjustments for readability. | ||
entry: pre_commit_hooks/terraform/fmt.sh | ||
language: script | ||
files: \.tf$ | ||
exclude: \.+.terraform\/.*$ | ||
require_serial: true | ||
|
||
# https://www.terraform.io/docs/commands/validate.html | ||
- id: terraform-validate | ||
name: Terraform validate | ||
description: The terraform validate command validates all Terraform configuration files, referring only to the configuration and not accessing any remote services such as remote state, provider APIs, etc. | ||
entry: pre_commit_hooks/terraform/validate.sh | ||
language: script | ||
files: \.tf$ | ||
exclude: \.+.terraform\/.*$ | ||
require_serial: true | ||
|
||
# https://github.com/terraform-linters/tflint | ||
- id: tflint | ||
name: tflint | ||
description: TFLint is a Terraform linter focused on possible errors, best practices, etc. (Terraform >= 0.12) | ||
entry: hooks/tflint.sh | ||
language: script | ||
files: \.tf$ | ||
exclude: \.+.terraform\/.*$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// AsciiDoc TOC settings | ||
:toc: | ||
:toc-placement!: | ||
:toc-title: | ||
|
||
// GitHub Flavored Asciidoc (GFA). See https://gist.github.com/dcode/0cfbf2699a1fe9b46ff04c41721dda74 for details. | ||
ifdef::env-github[] | ||
:tip-caption: :bulb: | ||
:note-caption: :information_source: | ||
:important-caption: :heavy_exclamation_mark: | ||
:caution-caption: :fire: | ||
:warning-caption: :warning: | ||
endif::[] | ||
|
||
= pre-commit-hooks | ||
|
||
This repository is a collection of https://pre-commit.com/[pre-commit hooks] used by https://mineiros.io[mineiros.io]. | ||
|
||
Currently, the following hooks are supported: | ||
|
||
* terraform-fmt: The terraform fmt command is used to rewrite Terraform configuration `*.tf` files to a canonical format and style. | ||
* terraform-validate: The terraform validate command validates all Terraform configuration `*.tf` files, referring only to the configuration and not accessing any remote services such as remote state, provider APIs, etc. | ||
* tflint: TFLint is a Terraform linter focused on possible errors, best practices, etc. (Terraform >= 0.12). Applied to all Terraform configuration `*.tf` files. | ||
|
||
== Installation | ||
Install https://pre-commit.com/[pre-commit]. E.G. `brew install pre-commit` | ||
|
||
== Usage | ||
|
||
Create a `.pre-commit-config.yaml` inside your repositories. You can dynamically add and remove hooks inside the configuration file. | ||
Please see the https://pre-commit.com/#usage[documentation] for further information. | ||
|
||
``` | ||
repos: | ||
- repo: https://github.com/mineiros-io/pre-commit-hooks | ||
rev: <VERSION> # Check for the latest version: https://github.com/mineiros-io/pre-commit-hooks/releases | ||
hooks: | ||
- id: terraform-fmt | ||
- id: terraform-validate | ||
- id: tflint | ||
``` | ||
|
||
Once you created the configuration file inside your repository, you must run `pre-commit install` to activate the hooks. | ||
|
||
That's it, pre-commit will now listen for changes in your files and run the checks accordingly. | ||
|
||
=== Run Check against All Files | ||
|
||
==== Example: Run A Specific Hook | ||
``` | ||
pre-commit run terraform-validate --all-files | ||
``` | ||
|
||
==== Example: Run All Hooks ( Useful Inside CI ) | ||
``` | ||
|
||
pre-commit run --all-files | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/bash | ||
|
||
# set -e is considered to be a bad practice as written here http://mywiki.wooledge.org/BashFAQ/105 | ||
# so we use a trap instead | ||
trap 'exit' ERR | ||
|
||
# Make environment variables working in OSX GUI apps such as Github Desktop https://stackoverflow.com/q/135688/483528 | ||
export PATH=$PATH:/usr/local/bin | ||
|
||
terraform fmt -recursive | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/bin/bash | ||
|
||
# set -e is considered to be a bad practice as written here http://mywiki.wooledge.org/BashFAQ/105 | ||
# so we use a trap instead | ||
trap 'exit' ERR | ||
|
||
# Make environment variables working in OSX GUI apps such as Github Desktop https://stackoverflow.com/q/135688/483528 | ||
export PATH=$PATH:/usr/local/bin | ||
|
||
for file in "$@"; do | ||
tflint "$file" | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/bin/bash | ||
|
||
# set -e is considered to be a bad practice as written here http://mywiki.wooledge.org/BashFAQ/105 | ||
# so we use a trap instead | ||
trap 'exit' ERR | ||
|
||
# Make environment variables working in OSX GUI apps such as Github Desktop https://stackoverflow.com/q/135688/483528 | ||
export PATH=$PATH:/usr/local/bin | ||
|
||
for dir in $(echo "$@" | xargs -n1 dirname | sort -u | uniq); do | ||
terraform init -backend=false "$dir" | ||
terraform validate "$dir" | ||
done |