Skip to content

Let the workflow give the backend type - #445

Open
benlangfeld wants to merge 1 commit into
dflook:mainfrom
benlangfeld:backend-type-input
Open

benlangfeld wants to merge 1 commit into
dflook:mainfrom
benlangfeld:backend-type-input

Conversation

@benlangfeld

@benlangfeld benlangfeld commented Sep 23, 2026 •

Copy link
Copy Markdown

One of the two approaches offered in #443. The other is #444, which makes the inspection itself cheap. They are independent — this one needs the caller to opt in, #444 helps everyone — and either stands alone.

The change

setup() works out the backend type by parsing every file in the root module. Most workflows know the answer already, and it rarely changes.

- uses: dflook/terraform-plan@v3
  with:
    backend_type: s3

When set, the inspection is skipped:

if [[ -n "${INPUT_BACKEND_TYPE:-}" ]]; then
    # Given by the workflow, so the root module does not need inspecting.
    TERRAFORM_BACKEND_TYPE="$INPUT_BACKEND_TYPE"
    echo "Using the $TERRAFORM_BACKEND_TYPE backend given by the backend_type input"
else
    TERRAFORM_BACKEND_TYPE=$(terraform-backend)
    if [[ "$TERRAFORM_BACKEND_TYPE" != "" ]]; then
        echo "Detected $TERRAFORM_BACKEND_TYPE backend"
    fi
fi

The log distinguishes the two cases, since a type the caller gave was not detected.

Notes on the approach

backend_type is already defined in docs-gen/inputs/ and used by remote-state, so this reuses that input with required=False rather than adding a second one with the same name. It sorts immediately after path, which is where Action.assert_ordering already expects it.

Added to plan, apply and check — the three actions named in #443. actions.sh is shared, so extending it to the others is only a matter of listing the input on them; I have left that out rather than widen the diff, and am happy to add them if you would prefer it everywhere at once.

Generated files are regenerated with docs-gen/generate.py; the only change in them is the new input.

Effect

On the root module profiled in #443 — ~260 .tf files — this removes a 46 second step. For comparison, #444 takes the same work from 34.07s to 0.20s on a generated module of that shape, without needing the input.

Tests

pytest tests --ignore=tests/github_pr_comment gives 147 passed with 2 failures and 7 errors, identical to main — those need S3 and some environment variables I do not have. tests/github_pr_comment fails to collect identically before and after, on a missing TERRAFORM_ACTIONS_* variable. bash -n image/actions.sh is clean under bash 5.

A larger related saving

The same root module is parsed twice per step. terraform-version calls load_module() on it in its own process, immediately before terraform-backend does.

That the first one is a parse rather than the archive download is now measured rather than inferred: the reporter cached the archive with actions/cache and the time did not move at all, with gpg verification landing at the identical offset on a cold and a warm run. See #443 (comment).

Together the two parses are around 80 seconds of a ~5m30s plan.

#446 removes the first one for workflows that pin an exact version, by taking the existing argument fast path in main() when TERRAFORM_VERSION names a version rather than a constraint — which also skips the release index fetch. With this PR and #446 together, a large root module with a pinned version parses nothing at all before terraform runs.

🤖 Generated with Claude Code

Working out the backend type means parsing every file in the root module, which
costs most of a minute on a large one. Most workflows know the answer already
and it rarely changes.

Offer the existing backend_type input on plan, apply and check, and skip the
inspection when it is set. The input is already defined for remote-state, so
this reuses it rather than adding another.

The log distinguishes the two cases, since a type that was given by the caller
was not detected.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant