Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: run tofu fmt and validate using pre-commit #12

Merged
merged 3 commits into from
Jun 27, 2024
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
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
Loading