From 4c0e672875ddef35d6edf27af7e6c8d5dd0d0fe8 Mon Sep 17 00:00:00 2001 From: Maciej Michalski Date: Tue, 24 Nov 2026 21:33:36 +0100 Subject: [PATCH] Add a reusable workflow for Terraform modules --- .github/workflows/terraform-module.yml | 145 +++++++++++++++++++++++++ README.md | 48 ++++++++ 2 files changed, 193 insertions(+) create mode 100644 .github/workflows/terraform-module.yml diff --git a/.github/workflows/terraform-module.yml b/.github/workflows/terraform-module.yml new file mode 100644 index 0000000..eb8ad71 --- /dev/null +++ b/.github/workflows/terraform-module.yml @@ -0,0 +1,145 @@ +name: Terraform module + +on: + workflow_call: + inputs: + terraform-versions: + description: JSON array of Terraform versions the tests run with; include the oldest version the module supports + required: false + type: string + default: '["1.14.9", "1.17.2"]' + tflint-version: + description: TFLint version to use + required: false + type: string + default: v0.66.0 + tflint-azurerm-version: + description: Version of the TFLint azurerm ruleset, used when the repository has no .tflint.hcl + required: false + type: string + default: 0.33.0 + terraform-docs-version: + description: terraform-docs version that checks the README + required: false + type: string + default: v0.25.1 + trivy-severity: + description: Misconfiguration severities that fail the build + required: false + type: string + default: CRITICAL,HIGH + +permissions: {} + +jobs: + lint: + runs-on: ubuntu-24.04 + permissions: + contents: read + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: hashicorp/setup-terraform@dfe3c3f87815947d99a8997f908cb6525fc44e9e # v4.0.1 + with: + terraform_version: ${{ fromJSON(inputs.terraform-versions)[0] }} + terraform_wrapper: false + - name: Check formatting + run: terraform fmt -check -recursive -diff + - name: Validate the module and its examples + run: | + for dir in . examples/*/; do + [ -d "$dir" ] || continue + echo "::group::terraform validate $dir" + terraform -chdir="$dir" init -backend=false -input=false > /dev/null + terraform -chdir="$dir" validate + echo "::endgroup::" + done + - uses: terraform-linters/setup-tflint@6ffdbaa3be476b3431f7275c26966ef32ff60337 # v6.3.2 + with: + tflint_version: ${{ inputs.tflint-version }} + - name: Lint with TFLint + env: + GITHUB_TOKEN: ${{ github.token }} + AZURERM_RULESET: ${{ inputs.tflint-azurerm-version }} + run: | + if [ ! -f .tflint.hcl ]; then + cat > "$RUNNER_TEMP/.tflint.hcl" < "$RUNNER_TEMP/terraform-docs.sha256" + (cd "$RUNNER_TEMP" && sha256sum -c terraform-docs.sha256) + tar -xzf "$RUNNER_TEMP/$archive" -C "$RUNNER_TEMP" terraform-docs + if ! "$RUNNER_TEMP/terraform-docs" . --output-check; then + "$RUNNER_TEMP/terraform-docs" . > /dev/null + git diff + echo "::error::README.md is out of date; run terraform-docs $VERSION in the repository root and commit the result shown above" + exit 1 + fi + - name: Scan for misconfigurations + uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0 + with: + scan-type: config + scan-ref: . + severity: ${{ inputs.trivy-severity }} + exit-code: "1" + + terraform-test: + runs-on: ubuntu-24.04 + permissions: + contents: read + strategy: + fail-fast: false + matrix: + terraform: ${{ fromJSON(inputs.terraform-versions) }} + name: test (${{ matrix.terraform }}) + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: hashicorp/setup-terraform@dfe3c3f87815947d99a8997f908cb6525fc44e9e # v4.0.1 + with: + terraform_version: ${{ matrix.terraform }} + terraform_wrapper: false + - name: Run terraform test + run: | + if [ -z "$(find tests -name '*.tftest.hcl' 2>/dev/null)" ]; then + echo "::error::The module has no tests in tests/*.tftest.hcl" + exit 1 + fi + terraform init -backend=false -input=false + terraform test + + # A single required check that does not change when the Terraform versions do. + test: + if: always() + needs: terraform-test + runs-on: ubuntu-24.04 + permissions: {} + steps: + - name: Require every Terraform version to pass + env: + RESULT: ${{ needs.terraform-test.result }} + run: | + echo "terraform test: $RESULT" + [ "$RESULT" = "success" ] diff --git a/README.md b/README.md index 49840f0..1c92f52 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ Reusable GitHub Actions workflows for Think-Cube repositories. |---|---| | [`container-image.yml`](.github/workflows/container-image.yml) | Lint, test, scan, sign and publish a multi-arch container image | | [`helm-chart.yml`](.github/workflows/helm-chart.yml) | Lint, validate, install, sign and publish a Helm chart as an OCI artifact | +| [`terraform-module.yml`](.github/workflows/terraform-module.yml) | Lint, scan and test a Terraform module | | [`version-check.yml`](.github/workflows/version-check.yml) | Open an issue when versions Dependabot does not track have newer releases | ## container-image @@ -143,6 +144,53 @@ cosign verify ghcr.io/think-cube/charts/: \ --certificate-identity-regexp '^https://github\.com/Think-Cube/workflows/\.github/workflows/helm-chart\.yml@refs/tags/v.+$' ``` +## terraform-module + +Checks a Terraform module that is consumed straight from GitHub, so nothing is published. The `lint` job checks `terraform fmt`, validates the module and every directory in `examples/`, runs TFLint with all rules of the Terraform ruleset and the azurerm ruleset, checks that the tables in the README match `terraform-docs` and scans for misconfigurations with Trivy. The `test` job runs `terraform test` once for every Terraform version, so the oldest supported version is tested too. It fails when the module has no tests. + +Write the tests against mocked providers (`mock_provider`) so they need no cloud credentials. + +### Usage + +```yaml +name: Module + +on: + push: + branches: [main] + pull_request: + branches: [main] + schedule: + - cron: "41 4 * * 1" + workflow_dispatch: + +permissions: {} + +jobs: + module: + permissions: + contents: read + uses: Think-Cube/workflows/.github/workflows/terraform-module.yml@v1.4.0 +``` + +The scheduled run catches new provider releases that break the module before its users do. Require the `module / lint` and `module / test` checks in the repository's ruleset; `module / test` passes only when the tests pass on every Terraform version. + +### Inputs + +| Input | Default | Description | +|---|---|---| +| `terraform-versions` | `["1.14.9", "1.17.2"]` | JSON array of Terraform versions to test with; the first one also runs the lint job | +| `tflint-version` | `v0.66.0` | TFLint version | +| `tflint-azurerm-version` | `0.33.0` | azurerm ruleset version, used when the repository has no `.tflint.hcl` | +| `terraform-docs-version` | `v0.25.1` | terraform-docs version that checks the README | +| `trivy-severity` | `CRITICAL,HIGH` | Misconfiguration severities that fail the build | + +A `.tflint.hcl` in the repository replaces the default configuration. The README needs `` and `` markers, and a `.terraform-docs.yml` sets how the tables look. + +### Releases + +Consumers pin a tag in the module source, for example `git::https://github.com/Think-Cube/terraform-azurerm-resource-group.git?ref=v1.0.0`, and Dependabot's `terraform` ecosystem proposes newer tags. A release is a `vX.Y.Z` tag with a GitHub release. + ## version-check Dependabot keeps base images, packages and actions current, but it cannot see versions pinned in build arguments, Helm values or Git commits, and it deliberately skips updates such as a new RabbitMQ minor. This workflow compares those pins with the latest releases and keeps a single issue, labelled `version-check`, up to date: it opens the issue when something is outdated, updates it as findings change and closes it once everything is current. It does not change any files, because these updates usually need a manual step.