Skip to content
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
145 changes: 145 additions & 0 deletions .github/workflows/terraform-module.yml
Original file line number Diff line number Diff line change
@@ -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" <<EOF
plugin "terraform" {
enabled = true
preset = "all"
}

plugin "azurerm" {
enabled = true
version = "$AZURERM_RULESET"
source = "github.com/terraform-linters/tflint-ruleset-azurerm"
}
EOF
export TFLINT_CONFIG_FILE="$RUNNER_TEMP/.tflint.hcl"
fi
tflint --init
tflint --recursive --format compact
- name: Check the README is up to date
env:
VERSION: ${{ inputs.terraform-docs-version }}
run: |
archive="terraform-docs-$VERSION-linux-amd64.tar.gz"
base="https://github.com/terraform-docs/terraform-docs/releases/download/$VERSION"
curl -fsSLo "$RUNNER_TEMP/$archive" "$base/$archive"
curl -fsSL "$base/terraform-docs-$VERSION.sha256sum" | grep " $archive\$" > "$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" ]
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -143,6 +144,53 @@ cosign verify ghcr.io/think-cube/charts/<chart>:<version> \
--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 `<!-- BEGIN_TF_DOCS -->` and `<!-- END_TF_DOCS -->` 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.
Expand Down