Skip to content

Check drift in CI with tfcensus-action ​

This guide sets up two GitHub Actions workflows for a Terragrunt live repository. The first checks drift on a schedule, uploads the result to code scanning and fails the job when a unit drifted. The second keeps one drift comment on each pull request. Both use tfcensus-action, which installs a verified tfcensus release, and an AWS role with the read-only policy.

How the workflows work ​

terragrunt plans every unit once, with plan -refresh-only -lock=false, and writes the plans to a directory on the runner. One tfcensus run reads those plans with --from-json-out-dir, so terragrunt runs once per unit and tfcensus starts no plan of its own. That run writes JSON, Markdown, SARIF and GitHub annotations through repeatable --out format=path flags, then gates the job with --fail-on.

The plans hold state values, so they stay on the runner and are never uploaded. The reports name attribute paths only.

Before you start ​

  • The live repository's mise.toml pins terragrunt and terraform, so the runner uses the same versions as your laptop.
  • An IAM role trusts GitHub OIDC for this repository and has the read-only policy. Set its ARN as the repository variable DRIFT_ROLE_ARN.
  • .tfcensus.yaml in the repository holds your drift noise rules, if any.

Scheduled drift check ​

Copy this file to .github/workflows/drift.yml in the live repository. It is docs/ci/drift.yml in the tfcensus repository, where CI lints it on every change:

yml
# Drift detection for a Terragrunt live repository: copy this file to .github/workflows/drift.yml
# there. The repo's mise.toml pins terragrunt and terraform, and its .tfcensus.yaml holds the
# drift.noise rules. sassdavid/tfcensus-action installs a verified tfcensus release.
# AWS access is a read-only role assumed with OIDC; set its ARN as the DRIFT_ROLE_ARN variable.
#
# terragrunt plans every unit once into $RUNNER_TEMP/plans. One tfcensus run in the action reads
# those plans, prints annotations, writes SARIF and Markdown, uploads the SARIF (category
# tfcensus-drift), appends the Markdown to the step summary, and gates the job last. The plans
# hold state values, so they stay on the runner and are never uploaded; the reports name attribute
# paths only. docs/ci/drift-pr.yml comments on pull requests instead.
name: drift

on:
  schedule:
    - cron: "17 5 * * 1-5"
  workflow_dispatch:

permissions: {}

concurrency:
  group: drift
  cancel-in-progress: false

jobs:
  drift:
    name: drift
    runs-on: ubuntu-latest
    timeout-minutes: 60
    permissions:
      contents: read
      id-token: write # OIDC token for the AWS role
      security-events: write # SARIF upload
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - uses: jdx/mise-action@90f833d396d0cc3674b631d2d225e306476c303a # v5.2.10
        with:
          version: 2026.11.11
      - uses: aws-actions/configure-aws-credentials@e1253824e5c10ff9df46874f81ed3ec929e19cfd # v6.3.0
        with:
          role-to-assume: ${{ vars.DRIFT_ROLE_ARN }}
          aws-region: eu-west-1
      # Exit 2 means a plan has changes and 1 that a unit failed. The report records both, and
      # tfcensus turns them into findings and errors below.
      - name: terragrunt plan -refresh-only
        env:
          TG_TF_PATH: terraform
        run: |
          terragrunt run --all --tf-path terraform --json-out-dir "$RUNNER_TEMP/plans" --report-file "$RUNNER_TEMP/plans/report.json" \
            --report-format json --log-format json --no-color \
            -- plan -refresh-only -lock=false -detailed-exitcode || true
      # Exit 3: drift or a failed unit. Exit 1 or 2: tfcensus couldn't read the plans. The action
      # uploads the SARIF and writes the step summary before the step fails. It passes
      # <from>/report.json as --report-file.
      - uses: sassdavid/tfcensus-action@f2e424405b36d9a380bf64f3ae4920652decff15 # v1.2.0
        with:
          mode: report
          from: ${{ runner.temp }}/plans
          fail-on: drift,errors

The action runs tfcensus drift in report mode. It passes from as --from-json-out-dir and <from>/report.json as --report-file, so units that failed before writing a plan are reported as failed. fail-on: drift,errors exits 3 when a unit drifted or failed. The action uploads the SARIF and writes the step summary before the step fails, so code scanning shows what failed the job.

The alerts are under Security, Code scanning, in the category tfcensus-drift. Each alert points at the unit's terragrunt.hcl.

Pull request comment ​

Copy this file to .github/workflows/drift-pr.yml. It is docs/ci/drift-pr.yml in the tfcensus repository:

yml
# Drift on pull requests to a Terragrunt live repository: copy this file to
# .github/workflows/drift-pr.yml there. Set up the repository as for docs/ci/drift.yml: mise.toml
# pins terragrunt, terraform and tfcensus, and DRIFT_ROLE_ARN names a read-only role for OIDC.
#
# The job plans every unit of the pull request's head once, then tfcensus writes the report as
# JSON, Markdown and annotations in one run. tfcensus notify keeps one comment on the pull request:
# a re-run edits it only when the report changed, and drift that is gone turns it into "Nothing to
# report". A pull request from a fork gets no OIDC token and a read-only GITHUB_TOKEN, so the job
# runs only for branches of this repository.
name: drift-pr

on:
  pull_request:
    paths:
      - "**/*.hcl"
      - "**/*.tf"
      - ".tfcensus.yaml"

permissions: {}

concurrency:
  group: drift-pr-${{ github.event.pull_request.number }}
  cancel-in-progress: true

jobs:
  drift:
    name: drift
    if: github.event.pull_request.head.repo.full_name == github.repository
    runs-on: ubuntu-latest
    timeout-minutes: 60
    permissions:
      contents: read
      id-token: write # OIDC token for the AWS role
      pull-requests: write # the sticky comment
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - uses: jdx/mise-action@90f833d396d0cc3674b631d2d225e306476c303a # v5.2.10
        with:
          version: 2026.11.11
      - uses: aws-actions/configure-aws-credentials@e1253824e5c10ff9df46874f81ed3ec929e19cfd # v6.3.0
        with:
          role-to-assume: ${{ vars.DRIFT_ROLE_ARN }}
          aws-region: eu-west-1
      - name: terragrunt plan -refresh-only
        env:
          TG_TF_PATH: terraform
        run: |
          terragrunt run --all --tf-path terraform --json-out-dir "$RUNNER_TEMP/plans" --report-file "$RUNNER_TEMP/report.json" \
            --report-format json --log-format json --no-color \
            -- plan -refresh-only -lock=false -detailed-exitcode || true
      # Exit 1 means a unit failed; the report and the comment say which. Only exit 2 (tfcensus
      # couldn't read the plans) stops the job here.
      - name: tfcensus drift
        run: |
          code=0
          tfcensus drift --from-json-out-dir "$RUNNER_TEMP/plans" --report-file "$RUNNER_TEMP/report.json" \
            --format github --out json=drift.json --out md=drift.md || code=$?
          if [ "$code" -eq 2 ]; then exit 2; fi
          cat drift.md >> "$GITHUB_STEP_SUMMARY"
      - name: pull request comment
        env:
          GITHUB_TOKEN: ${{ github.token }}
        run: tfcensus notify --from drift.json --notify github-pr-comment

This workflow calls tfcensus directly, so you see each step. The drift step writes annotations, JSON and Markdown in one run. tfcensus notify --notify github-pr-comment then keeps one comment on the pull request: a re-run edits it only when the report changed, and drift that is gone turns it into "Nothing to report". Publish reports with notify explains the marker and the token.

The action's comment mode does the same in one step:

yaml
- uses: sassdavid/tfcensus-action@f2cc1dccce5a5de868ff2f3f50dbec2603d197c7 # v1.0.1
  with:
    mode: comment
    from: ${{ runner.temp }}/plans
    sarif: false

A pull request from a fork or from Dependabot gets no OIDC token and a read-only GITHUB_TOKEN. Without AWS credentials there is nothing to plan, so drift-pr.yml skips such pull requests with if: github.event.pull_request.head.repo.full_name == github.repository. When comment mode does run without a write token, the action skips the comment, says why in a notice and writes the report to the step summary.

Long reports ​

Markdown is cut at a table row or section once it reaches --md-max-chars (60,000 characters by default, under GitHub's limit of 65,536 characters for a comment), with a footer that says what was cut. The JSON and SARIF files are never cut.

Exit codes in CI ​

CodeIn these workflows
0No unit drifted and no unit failed, or nothing matched --fail-on.
1A unit or a repository failed and no gate matched. The report lists it.
2tfcensus couldn't read the plans, or a flag or the config is wrong.
3A --fail-on gate matched. The files, the SARIF upload and the comment are done first.
4Credentials were refused, such as a read-only token for the comment.

All codes are on the exit codes page, and the gate values on the drift CLI page.

tfcensus-action inputs ​

Pin the action by commit SHA. Each vX.Y.Z release is immutable, and the v1 tag moves to the newest 1.x release. The inputs and outputs below are those of v1.

InputDefaultMeaning
versionthe version the action release pinstfcensus version to install: X.Y.Z, vX.Y.Z or latest.
verifyattestationThe check after sha256 against checksums.txt. attestation runs gh attestation verify pinned to tfcensus's release workflow, cosign runs cosign verify-blob on checksums.txt, and checksum adds nothing.
modeinstallinstall puts tfcensus on PATH. report runs the command once, writes JSON, Markdown, SARIF and annotations, uploads the SARIF and writes the step summary. comment does the same on a pull_request event and keeps one comment on the pull request.
commanddriftThe command report and comment mode run. Both take drift only.
argsMore arguments for the command, one per line.
fromThe --json-out-dir of an earlier terragrunt run --all step, passed as --from-json-out-dir, plus --report-file for its report.json when that file exists and args has none. Without it, tfcensus runs terragrunt itself.
fail-on--fail-on values, separated by commas or newlines. A match exits 3 and fails the step after the files, the SARIF upload and the comment.
sariftrueUpload the SARIF file to code scanning. The job needs security-events: write.
step-summarytrueAppend the Markdown report to the step summary.
github-token${{ github.token }}Token for gh attestation verify, tfcensus's GitHub API calls, the SARIF upload and the comment.
working-directory.Directory the command runs in. SARIF locations are relative to it, and the SARIF category ends with /<directory> when it isn't the repository root.

A bad input exits 2 before the install: an unknown mode or command, or command, args, from or fail-on in install mode. The action never takes AWS credentials, so run aws-actions/configure-aws-credentials before it.

tfcensus-action outputs ​

OutputMeaning
versionThe installed tfcensus version, without a leading v.
pathAbsolute path of the tfcensus binary.
exit-codeExit code of the command in report and comment mode: 0, 1, 2, 3 or 4.
json-fileAbsolute path of the JSON report, empty when the command wrote none.
sarif-fileAbsolute path of the SARIF file, empty when the command writes no SARIF.
md-fileAbsolute path of the Markdown report, empty when the command wrote none.

The step fails with the command's exit code. In comment mode, a refused comment fails it with notify's code when the command passed.

Next steps ​