Skip to content

Commit

Permalink
test: run tofu fmt and validate using pre-commit (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
jooola authored Jun 27, 2024
1 parent 4b28450 commit 6f2dd85
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 27 deletions.
32 changes: 7 additions & 25 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,6 @@ on:
pull_request:

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: opentofu/setup-opentofu@v1
with:
tofu_version: v1.7.2 # renovate: datasource=github-releases depName=opentofu/opentofu
tofu_wrapper: false

- run: |
tofu init
tofu validate
tofu fmt
- name: Check uncommitted changes
run: git diff --exit-code

- if: failure()
run: echo "::error::Check failed, please run 'tofu validate; tofu fmt' and commit the changes."

pre-commit:
runs-on: ubuntu-latest
steps:
Expand All @@ -37,8 +16,11 @@ jobs:
with:
python-version: 3.x

- name: Install dependencies
run: pip install pre-commit
- uses: opentofu/setup-opentofu@v1
with:
tofu_version: v1.7.2 # renovate: datasource=github-releases depName=opentofu/opentofu
tofu_wrapper: false

- name: Run pre-commit
run: pre-commit run --all-files --show-diff-on-failure
- uses: pre-commit/[email protected]
with:
extra_args: --all-files
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.terraform/
20 changes: 20 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,23 @@ repos:
- id: prettier
files: \.(md|ya?ml)$
exclude: ^CHANGELOG.md$

- repo: local
hooks:
- id: tofu fmt
name: tofu fmt
description: Format Terraform files.
require_serial: true
pass_filenames: false
entry: make fmt
language: system
files: (\.tf|\.tfvars)$

- id: tofu validate
name: tofu validate
description: Validates Terraform files.
require_serial: true
pass_filenames: false
entry: make validate
language: system
files: (\.tf|\.tfvars)$
114 changes: 114 additions & 0 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
SHELL = bash

.terraform:
tofu init -backend=false

fmt: .terraform
tofu fmt -recursive

validate: .terraform
tofu validate
7 changes: 5 additions & 2 deletions example/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ ENV ?= dev
K3S_CHANNEL ?= stable

env.auto.tfvars:
@echo 'name = "$(ENV)"' > "$@"
@echo 'name = "$(ENV)"' > "$@"
@echo 'hcloud_token = "$(HCLOUD_TOKEN)"' >> "$@"
@echo 'k3s_channel = "$(K3S_CHANNEL)"' >> "$@"
@echo 'k3s_channel = "$(K3S_CHANNEL)"' >> "$@"

.terraform:
tofu init

validate: .terraform
tofu validate

up: .terraform env.auto.tfvars
tofu apply -auto-approve
$(MAKE) port-forward
Expand Down

0 comments on commit 6f2dd85

Please sign in to comment.