Skip to content

Commit

Permalink
Add packer validate (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
queglay authored Sep 27, 2021
1 parent ebe15a2 commit 2053191
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
exclude: \.+.terraform\/.*$
require_serial: true

- id: packer-validate
name: Packer validate
description: Validates all Packer configuration files
entry: hooks/packer-validate.sh
language: script
files: \.pkr.*$
require_serial: true

- id: tflint
name: tflint
description: Linter for Terraform source code
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ supported hooks are:

* **terraform-fmt**: Automatically run `terraform fmt` on all Terraform code (`*.tf` files).
* **terraform-validate**: Automatically run `terraform validate` on all Terraform code (`*.tf` files).
* **packer-validate**: Automatically run `packer validate` on all Packer code (`*.pkr.*` files).
* **terragrunt-hclfmt**: Automatically run `terragrunt hclfmt` on all Terragrunt configurations.
* **tflint**: Automatically run [`tflint`](https://github.com/terraform-linters/tflint) on all Terraform code (`*.tf` files).
* **shellcheck**: Run [`shellcheck`](https://www.shellcheck.net/) to lint files that contain a bash [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)).
Expand Down
20 changes: 20 additions & 0 deletions hooks/packer-validate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

set -e

# OSX GUI apps do not pick up environment variables the same way as Terminal apps and there are no easy solutions,
# especially as Apple changes the GUI app behavior every release (see https://stackoverflow.com/q/135688/483528). As a
# workaround to allow GitHub Desktop to work, add this (hopefully harmless) setting here.
export PATH=$PATH:/usr/local/bin

# Store and return last failure from validate so this can validate every directory passed before exiting
VALIDATE_ERROR=0

for dir in $(echo "$@" | xargs -n1 dirname | sort -u | uniq); do
echo "--> Running 'packer validate -syntax-only' in directory '$dir'"
pushd "$dir" >/dev/null
packer validate -syntax-only || VALIDATE_ERROR=$?
popd >/dev/null
done

exit ${VALIDATE_ERROR}

0 comments on commit 2053191

Please sign in to comment.