Skip to content

Workflow tofu ci

github-actions[bot] edited this page Nov 28, 2026 · 10 revisions

tofu-ci

What it does. CI for infrastructure as code (OpenTofu or Terraform): fmt -check, validate with no backend, tflint and a Trivy configuration scan on every push and pull request, none of which needs a cloud credential; two opt-in legs that need none either, tofu test for tests that mock their providers and opa test for Rego policies kept beside the configuration; a plan on the default branch only, with credentials through the Doppler gate; the same CI green gate. Nothing applies, ever.

Why it exists. A repository with Terraform wants the checks that need no credential on every pull request and the one that needs a credential on a trusted ref only. The first consumer's modules use the AWS provider and an aws_region data source, so plan cannot run on a pull request even with no state; that measurement rewrote the roadmap item from "plan on pull requests" to this shape. The second consumer, Clouds-Against-Humanity, carried its own tofu test loop and needed a place to test a Spacelift push policy; both are the same job in every repository with tests or policies, so they live here now.

Jobs and trust boundaries

Job Holds Notes
fmt, validate, tflint, trivy, workflow-lint contents: read No credential. OpenTofu and tflint are downloaded at a pinned version and sha256
test (with test: true) contents: read init -backend=false, then tofu test, in each directory. No credential, so a test that reaches a real provider fails here; that is what mock_provider is for
policy (with policy-directories) contents: read opa fmt --list --fail, opa check --strict and opa test in each policy directory. OPA is downloaded at a pinned version and sha256
plan contents: read, id-token: write Runs only on a push to the default branch, a version tag (vN.N.N) or a schedule; fetches the Doppler config's secrets into the environment and runs plan-command. The one place in this workflow a secret exists
ci-green none Needs all of the above

A minimal caller

jobs:
  tofu:
    uses: ChiefGyk3D/git-your-ship-together/.github/workflows/tofu-ci.yml@<sha> # vX.Y.Z
    permissions:
      contents: read
      pull-requests: read
      id-token: write   # the plan job's Doppler fetch, off pull requests only
    secrets:
      DOPPLER_TOKEN: ${{ secrets.DOPPLER_TOKEN }}   # optional fallback; unset means OIDC only
    with:
      directories: infra/terraform/examples/ecs-fargate
      test: true                      # only when the tests use mock_provider
      policy-directories: policies    # Rego plus its _test.rego files
      plan-command: |
        cd infra/terraform/examples/ecs-fargate
        tofu init -input=false && tofu plan -input=false
      egress-policy: block
      extra-allowed-endpoints: registry.terraform.io:443 sts.amazonaws.com:443
      doppler-project: ci
      doppler-config: ci
      doppler-identity-id: ${{ vars.DOPPLER_IDENTITY_ID }}

binary: terraform uses the Terraform the runner image ships instead of downloading OpenTofu. A provider registry or a cloud API your plan reaches goes in extra-allowed-endpoints. A plan that needs a credential means the Doppler ci config (or your own) holds a read-only role for it; the maintainer's first consumer still needed that role in Doppler before plan-command was set.

Inputs (32), read from tofu-ci.yml:

Input Type Default Meaning
directories string . Space-separated root modules to validate, lint and scan, relative to the repository root.
binary string tofu tofu (downloaded at tofu-version) or terraform (the runner image's).
tofu-version string 1.13.2 OpenTofu release to download (opentofu/opentofu), without the v.
tofu-sha256 string 723bebb0df8411daff1e5fc3862e28ff5581fe5634087f4c29a9aa41d78208b5 SHA-256 of the OpenTofu linux_amd64 zip for that version.
tflint boolean true Run tflint over every directory.
tflint-version string 0.65.0 tflint release to download (terraform-linters/tflint), without the v.
tflint-sha256 string d4c545ed2e29d6338604b586f34f544f380f70f243aa72f7b484713835eaec61 SHA-256 of the tflint linux_amd64 zip for that version.
test boolean false Run tofu test (or terraform test) in every directory, after init -backend=false. Off by default because a test file that reaches a real provider would need a credential this job never has; turn it on when the tests use mocked providers.
test-matrix boolean false With test, run tofu test as one job per directory (a matrix, all legs at once, none cancelled by another's failure) instead of every directory one after another in one job. The ci-green job still reports one result and fails on any failed, cancelled or skipped leg. Off by default, so a caller that sets nothing sees the same single test job.
policy-directories string empty Space-separated directories of Rego policies (Spacelift, Conftest, OPA) with their _test.rego files, relative to the repository root. Each gets opa fmt, opa check --strict and opa test; empty skips the job.
opa-version string 1.22.0 OPA release to download (open-policy-agent/opa), without the v.
opa-sha256 string 791790254c598a942eb140959bcec9a47f844535a632fd01d11d870ba2a47047 SHA-256 of the OPA linux_amd64_static binary for that version.
trivy boolean true Run Trivy's configuration scan over the repository.
trivy-severity string CRITICAL,HIGH Severities the configuration scan reports.
trivy-exit-code string 1 Exit code when the scan finds something; 1 fails the job, 0 reports only. A migration aid, not a resting state.
plan-command string empty Shell that plans, e.g. cd infra/live && tofu init -input=false && tofu plan -input=false, run only on a push to the default branch with the Doppler config's secrets in the environment; empty skips the job.
workflow-lint boolean true Run actionlint and zizmor over the caller's own workflow files. Turn off on one caller job when another already runs it.
zizmor-persona string regular zizmor strictness - regular, pedantic or auditor.
egress-policy string audit harden-runner egress policy for every job - audit logs outbound connections, block allows only allowed-endpoints. Jobs that hold no secret follow egress-policy-secretless instead (EX-0001).
egress-policy-secretless string block harden-runner egress policy for the jobs that hold no stored secret, no OIDC token and no write-scoped or publish-capable token (read-only contents, a SARIF upload at most), and whose output no privileged job consumes - exactly audit or block. Defaults to block since security exception EX-0001 (GitHub-hosted runners in westus2 broke harden-runner block) was closed on 2026-11-02 after harden-runner v2.22.3 passed on westus2; see docs/security/exceptions/. Every job still has the default read-only job token. Set audit to opt out if the defect recurs. Any other job follows egress-policy. Any other value fails the job.
allowed-endpoints string api.doppler.com:443 api.github.com:443 check.trivy.dev:443 files.pythonhosted.org:443 get.trivy.dev:443 ghcr.io:443 github.com:443 mirror.gcr.io:443 objects.githubusercontent.com:443 pkg-containers.githubusercontent.com:443 pypi.org:443 raw.githubusercontent.com:443 registry.npmjs.org:443 registry.opentofu.org:443 release-assets.githubusercontent.com:443 harden-runner allow-list for block, space-separated host:port entries. The default is the tool downloads, Trivy's checks bundle and PyPI (actionlint's pipx install pyflakes in workflow lint); provider registries a real module reaches go in extra-allowed-endpoints. Wildcards are not supported and a newline-separated list matches nothing.
extra-allowed-endpoints string empty Appended to allowed-endpoints, for hosts only this repository reaches, such as a provider registry or a cloud API the plan talks to. Same format.
doppler-project string empty Doppler project slug.
doppler-config string empty Doppler config slug holding the plan's credentials.
doppler-identity-id string empty Doppler Service Account Identity UUID for OIDC.
doppler-trusted-refs-only boolean true Fetch secrets only on the default branch, a version tag (vN.N.N) or a schedule; never on a pull request or another branch. false restores fetching on every same-repository run.
timeout-minutes number 16 Per-job timeout.
runs-on string "ubuntu-latest" Runner for every job, as a JSON string, because runs-on takes a string, an array or an object: "ubuntu-latest", ["self-hosted","linux"] or {"group":"my-group","labels":["linux"]}. The default parses to the label this workflow always used. Invalid JSON fails the job at expression time. Jobs that follow egress-policy-secretless reach audit only on a GitHub-hosted runner and a label in the hosted list (EX-0001); see the workflow header for how harden-runner behaves on a self-hosted runner.
skip-docs-only boolean false Leave the test-dirs and test jobs out of a pull request that changes only documentation, with no per-repository edit beyond setting this. Off by default: a workflow cannot read whether its repository is private, so the caller opts in. A new classify job reads the pull request's changed files from the API (never from the checkout) and says docs-only only when every path matches docs-only-patterns and none is under .github/ or never-skip-patterns; every doubt runs the tests. Format, validate, tflint, Trivy, policy, plan and workflow lint are never skipped, and CI green still fails on any failed job. The job holds contents: read and pull-requests: read; every caller grants both, since a reusable job cannot exceed the caller's grant. The second read lets it read the file list; without it the classifier fails closed and every job runs.
docs-only-patterns string *.md docs/**/*.md Newline-separated globs, anchored at the repository root (* stops at /, ** crosses it), for the paths that make the tests unnecessary. The default is markdown only. A pattern that matches every path is refused. Used with skip-docs-only.
never-skip-patterns string **/*.tf **/*.tfvars **/*.hcl **/*.lock Newline-separated globs that always run every job, on top of .github/**. The default keeps every Terraform and lock file (.tf, .tfvars, .hcl, which includes .terraform.lock.hcl, and .lock), so a configuration change is never skipped even when docs-only-patterns names its path. Used with skip-docs-only.
allow-self-hosted boolean false Let these jobs run on a self-hosted runner (default false: on any runner that is not GitHub-hosted the job fails). Setting it true is the caller's attestation that the runner is ephemeral, one job per VM, and isolated from secrets and the network. Even then a self-hosted runner takes only push, schedule, workflow_dispatch, release and same-repository pull_request events. Secret-bearing jobs belong on GitHub-hosted runners or a distinct ephemeral trusted pool.

Secrets a caller may pass, by name:

Secret Required Meaning
DOPPLER_TOKEN no Doppler Service Token fallback when OIDC is not configured.

Choosing the runner

runs-on is a JSON string that every job reads through fromJSON(inputs.runs-on), because runs-on takes a string, an array or an object: '"ubuntu-latest"' (the default), '["self-hosted","linux"]' or '{"group":"my-group","labels":["linux"]}'. Invalid JSON fails the job at expression time. allow-self-hosted (default false) is the caller's attestation that a self-hosted runner is ephemeral, one job per VM, and isolated from secrets and the network. Without it, a step right after harden-runner in every job fails on any runner that is not GitHub-hosted. With it, a self-hosted runner still takes only push, schedule, workflow_dispatch, release and same-repository pull_request events.

Skipping the tests on a docs-only pull request

Set skip-docs-only: true (off by default) and a pull request that changes only *.md or docs/**/*.md leaves the test-dirs and test jobs out; format, validate, tflint, Trivy, policy and workflow lint still run, and CI green still fails on any failed job. The never-skip-patterns default (**/*.tf, **/*.tfvars, **/*.hcl, **/*.lock) keeps every Terraform and lock file on the run list, even when docs-only-patterns names it. Every caller grants pull-requests: read in its permissions:, whether or not it sets this input, because a reusable job cannot exceed the caller's grant. The classifier reads the changed files with it. See Docs-only fast path.

What it refuses to do

It never applies, never plans on a pull request, and never lets a pull request's code run beside the plan's credentials. test is off by default for the same reason: switching it on is the caller saying its tests need nothing the job does not have.

Clone this wiki locally