OPT31 — Conditional Step With Unconditional Setup
Anti-pattern: A step (typically a tool install, browser install, or large download) exists ONLY to support a downstream step that has an if: condition gating it. The downstream step is conditionally skipped (e.g., when a secret is missing, when a feature is disabled, on certain branches), but the upstream setup step runs unconditionally — paying the full setup cost for runs where the consumer never executes.
A second form occurs in an aggregate job: a shell-only step reads
needs.<job>.result values and fails when a producer was unsuccessful, but it
runs after checkout or a setup-* action. A failed dependency therefore pays
for setup before the result gate ends the job.
Why this is missed by OPT1 (Unnecessary Tool Install): OPT1 catches the case where the tool is NEVER invoked in the job. OPT31 catches the case where the tool IS invoked, but only conditionally — and the install step is missing the same condition.
Detection heuristic:
- For each job, walk steps in order. For every step that has an
if:condition (especially conditions referencingenv.*,secrets.*, branch names, or labels): - Look at the immediately preceding setup steps (within the same job, no
needs:boundary). Identify the setup steps whose only consumer is this conditional step. - Examples of "setup-and-consumer" pairings:
bunx playwright install(setup) →playwright testore2e(consumer)apt-get install <pkg>→<pkg> --versionor invocationpip install <test-only-package>→ only used in test invocationgh auth setup-git→ only used in conditionalghcalls
- Flag if the setup is unconditional but the consumer's
if:would skip in some fraction of runs. - The savings =
setup_step_p50_seconds × P(consumer skips).
For the aggregate-job form, require a result step that reads only literal
needs.*.result values, can exit non-zero, and has no continue-on-error.
Every simple command in it (split on ;, &&, || and |) must be a shell
control, output or test builtin, and it must use bash or sh with no
working-directory at step, job or workflow level. Flag only checkout or
setup-* actions before the first such result gate. Withhold the finding when
the gate runs a repository script or tool, uses command substitution or an
input redirect, tests a file, reads a variable that is not in step, job or
workflow env (an earlier step may export it), reads another GitHub context or
an unreadable expression, or when setup already has a needs-result condition.
Fix: Copy the if: condition from the consumer step onto the setup step (or wrap both in a guard step that exits early). Example:
# Before
- name: Install Playwright Chromium
run: cd apps/web && bunx playwright install --with-deps chromium # always runs (~16s)
- name: Web smoke e2e
if: env.CLERK_SECRET_KEY != ''
run: cd apps/web && bunx playwright test smoke
# After
- name: Install Playwright Chromium
if: env.CLERK_SECRET_KEY != '' # add same condition
run: cd apps/web && bunx playwright install --with-deps chromium
- name: Web smoke e2e
if: env.CLERK_SECRET_KEY != ''
run: cd apps/web && bunx playwright test smokeFor a needs-result gate, move that unchanged gate to the first step, before checkout and toolchain setup. Preserve its complete success predicate and test successful, failed, cancelled, skipped and missing dependency results.
Sizing: Runner-minute effect is a saving: the affected setup steps' measured billed compute on runs where the conditional consumer is skipped or a dependency result is unsuccessful. Do not state a number until both the setup time and that run share are measured. A result gate that ends a failed run earlier normally does not reduce successful pull-request merge wait.
Real-world example (blen-starter-kit): ci.yml web-quality job has Install Playwright Chromium (line 92, ~16s) followed by Web smoke e2e (line 95) gated on if: env.CLERK_SECRET_KEY != '' && env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY != ''. PRs from forks (no Clerk secrets) pay 16s for setup that's never consumed.
Public evidence: openclaw/crabbox pull request #2868: the coverage gate fails at its first step and skips checkout and Go setup when a producer fails.
Risk: LOW. The conditional-consumer fix is defensive when both steps keep the same condition. Moving a needs-result gate is safe only under the narrower shell-only detection above; changing its predicate can instead hide a producer failure, so preserve it byte-for-byte.