Skip to content

CI: the secrets context is not allowed in a step if; test for it - #8

Merged
brettheap merged 1 commit into
mainfrom
fix/secrets-in-if
Sep 3, 2026
Merged

brettheap merged 1 commit into
mainfrom
fix/secrets-in-if

Conversation

@brettheap

@brettheap brettheap commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Observed

The copied .github/workflows/validate.yml was REJECTED by GitHub on the
first real projects: MedxSoft/MedxEHR PR #8 and MedxSoft/MedxGlass PR #1.
GitHub reported "This run likely failed because of a workflow file issue"
and the check appears as a push-event run with zero jobs — the workflow
file could not be parsed at all, not merely one failing step.

Cause

In templates/assembly-root/.github/workflows/validate.yml, the step
fail — SHAPE_LEGS_TOKEN is set but the legs still could not be fetched had:

if: steps.submodules.outputs.legs_available != 'true' && secrets.SHAPE_LEGS_TOKEN != ''

The secrets context is not available in a step-level if: expression
("unrecognized named-value 'secrets'"). GitHub's response to that is not a
red step — it is a workflow file GitHub cannot parse, so it never runs any
job for the event, which surfaces as a required check that never appears
rather than one that fails.

Fix

  • Added a job-level env: SHAPE_LEGS_TOKEN_SET: ${{ secrets.SHAPE_LEGS_TOKEN != '' }}
    — the secrets context is allowed in env:.
  • Changed the step condition to
    if: steps.submodules.outputs.legs_available != 'true' && env.SHAPE_LEGS_TOKEN_SET == 'true'.
  • Left the checkout step's token: ${{ secrets.SHAPE_LEGS_TOKEN || github.token }}
    unchanged — secrets is allowed in with:.

Tests

  • Updated test_the_ci_workflow_checkout_uses_shape_legs_token for the new
    step condition.
  • Added test_no_if_expression_reads_the_secrets_context, which walks
    every if: line in the rendered workflow (not just the one step that
    broke) asserting none contains secrets., and that the job env carries
    SHAPE_LEGS_TOKEN_SET.
  • Added an actionlint step to .github/workflows/tests.yml (pinned
    release binary v1.7.12, sha256-verified — no pip/go needed) that lints
    templates/assembly-root/.github/workflows/validate.yml. Verified locally
    that it flags exactly this defect on the pre-fix file and passes clean on
    the fix.
  • Noted in both READMEs (aggregation root and the scaffolded-project
    template) that the presence check is a job-level env value, not the
    secrets context directly.

python3 -m pytest tests -q: 232 passed.

🤖 Generated with Claude Code

https://claude.ai/code/session_01HGKwdQsmvRZx4fGQESdFGZ

Summary by Sourcery

Prevent GitHub from rejecting generated validation workflows by moving secret presence checks out of step-level conditions and linting the workflow in CI.

Bug Fixes:

  • Fix the generated validation workflow so configured SHAPE_LEGS_TOKEN failures are detected without using the unsupported secrets context in a step condition.

Enhancements:

  • Add regression coverage ensuring rendered workflow step conditions do not directly read secrets and preserve the job-level token presence check.

CI:

  • Add checksum-verified actionlint validation for the generated GitHub Actions workflow.

Documentation:

  • Document the job-level environment-based secret presence check and the workflow parsing failure it prevents.

Tests:

  • Update scaffold end-to-end assertions for the revised workflow condition and add coverage for all rendered if expressions.

The copied `.github/workflows/validate.yml` was REJECTED by GitHub on the
first real projects (MedxSoft/MedxEHR PR #8, MedxSoft/MedxGlass PR #1):
"This run likely failed because of a workflow file issue", a push-event
run with zero jobs. Cause: the step `fail — SHAPE_LEGS_TOKEN is set but
the legs still could not be fetched` had
`if: ... && secrets.SHAPE_LEGS_TOKEN != ''` — the `secrets` context is not
available in a step-level `if:` expression ("unrecognized named-value
'secrets'"), so GitHub cannot parse the workflow file at all.

Fix: add a job-level `env: SHAPE_LEGS_TOKEN_SET: ${{ secrets.SHAPE_LEGS_TOKEN
!= '' }}` (the `secrets` context IS allowed in `env:`) and change the step
condition to `if: ... && env.SHAPE_LEGS_TOKEN_SET == 'true'`. The checkout
step's `token: ${{ secrets.SHAPE_LEGS_TOKEN || github.token }}` is unchanged
(allowed in `with:`).

- Updated the existing rendered-workflow assertion in
  tests/test_scaffold_e2e.py for the new step condition, and added
  test_no_if_expression_reads_the_secrets_context, which walks every `if:`
  line in the rendered workflow (not just the one step) asserting none
  contains `secrets.`, and that the job `env` carries
  `SHAPE_LEGS_TOKEN_SET`.
- Added an actionlint step to .github/workflows/tests.yml (pinned release
  binary v1.7.12, sha256-verified, no pip/go required) that lints
  templates/assembly-root/.github/workflows/validate.yml — confirmed
  locally that it flags exactly this defect on the pre-fix file and passes
  clean on the fix.
- Noted in both the aggregation README and the scaffolded-project template
  README that the SHAPE_LEGS_TOKEN presence check reads a job-level `env`
  value rather than the `secrets` context directly; bumped the README line
  cap 265 -> 267 (test_readme_is_short_enough_to_be_read) for the added
  sentence.

python3 -m pytest tests -q: 232 passed.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01HGKwdQsmvRZx4fGQESdFGZ
Copilot AI lite review requested due to automatic review settings September 3, 2026 05:01

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @brettheap, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 12 hours and 3 minutes by commenting @sourcery-ai review. Upgrade to get a review now.

@sourcery-ai

sourcery-ai Bot commented Sep 3, 2026

Copy link
Copy Markdown

Reviewer's Guide

Fixes the invalid use of the secrets context in a step-level GitHub Actions if expression by exposing a boolean through job-level env, adds broad regression tests and actionlint validation, and documents the constraint and workaround.

Sequence diagram for the safe secret-presence check in workflow validation

sequenceDiagram
    participant GitHub as GitHub Actions
    participant Job as validate job
    participant Submodules as submodules step
    participant Guard as failure guard

    GitHub->>Job: Evaluate job env
    Job->>Job: Read secrets.SHAPE_LEGS_TOKEN
    Job-->>Job: Set env.SHAPE_LEGS_TOKEN_SET
    Job->>Submodules: Fetch submodules
    Submodules-->>Job: Set legs_available
    Job->>Guard: Evaluate if using env.SHAPE_LEGS_TOKEN_SET
    alt legs unavailable and token is set
        Guard->>Guard: exit 1
    end
Loading

Flow diagram for validating secrets usage in the rendered workflow

flowchart TD
    A[Render validate workflow] --> B[Walk every if expression]
    B --> C{Contains secrets. context?}
    C -- Yes --> D[Test fails]
    C -- No --> E[Check job env has SHAPE_LEGS_TOKEN_SET]
    E --> F{actionlint validates workflow}
    F -- Pass --> G[Workflow is accepted by GitHub]
    F -- Fail --> D
Loading

File-Level Changes

Change Details Files
Move the secret-presence check out of step-level conditions into a permitted job-level environment variable.
  • Define SHAPE_LEGS_TOKEN_SET from the secret in job-level env.
  • Use env.SHAPE_LEGS_TOKEN_SET in the guarded failure step while retaining the checkout token expression.
templates/assembly-root/.github/workflows/validate.yml
Add regression coverage for invalid secrets-context usage in rendered workflows.
  • Update the scaffold end-to-end assertion for the new condition.
  • Scan every rendered if: line to reject secrets. references and verify the job-level env declaration.
tests/test_scaffold_e2e.py
Add static workflow validation to CI using a pinned, checksum-verified actionlint binary.
  • Download and verify actionlint v1.7.12.
  • Lint the scaffolded validate workflow during the tests workflow.
.github/workflows/tests.yml
Document the GitHub Actions context restriction and its failure mode.
  • Explain the job-level env workaround and zero-job workflow symptom.
  • Update README line-count hygiene expectations.
README.md
templates/assembly-root/README.md
tests/test_repo_hygiene.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sonarqubecloud

sonarqubecloud Bot commented Sep 3, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
C Security Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

@brettheap
brettheap merged commit 3790674 into main Sep 3, 2026
3 of 4 checks passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The workflow fix is targeted and regression-protected (tests + actionlint), with only minor follow-up nits identified.

Pull request overview

Fixes a GitHub Actions parsing failure in the scaffolded validate.yml by removing direct secrets.* access from step-level if: expressions, replacing it with a job-level env boolean, and adds regression checks + CI linting to prevent reintroduction.

Changes:

  • Update the validate workflow template to compute SHAPE_LEGS_TOKEN_SET at job env: scope and reference env.SHAPE_LEGS_TOKEN_SET in the guarded failure step.
  • Add end-to-end regression coverage ensuring rendered workflow if: expressions don’t read secrets.*, and adjust existing assertions accordingly.
  • Add checksum-verified actionlint linting in CI and update READMEs / hygiene caps to reflect the new rule.
File summaries
File Description
templates/assembly-root/.github/workflows/validate.yml Moves the secret presence check to job-level env and updates the failing-step if: to use env.* instead of secrets.*.
tests/test_scaffold_e2e.py Updates the token-guard assertion and adds a regression test scanning rendered workflow if: expressions for forbidden secrets.*.
.github/workflows/tests.yml Adds an actionlint install + lint step to catch workflow-expression/context issues in CI.
templates/assembly-root/README.md Documents the job-level env presence check and why step-level secrets in if: breaks parsing.
README.md Updates the SHAPE_LEGS_TOKEN section to note the env-based presence check and the zero-job symptom.
tests/test_repo_hygiene.py Updates the README line cap to accommodate the additional explanation.
Review details
  • Files reviewed: 6/6 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +399 to +413
workflow = (project / ".github" / "workflows" / "validate.yml").read_text()

if_lines = [line for line in workflow.splitlines()
if line.lstrip().startswith("if:")]
assert if_lines, "expected at least one `if:` step condition"
for line in if_lines:
assert "secrets." not in line, (
f"step-level `if:` must not read the `secrets` context: {line!r}")

assert "SHAPE_LEGS_TOKEN_SET" in workflow
# The job-level `env:` sits above `steps:` in the job block.
env_block = workflow.split("steps:", 1)[0]
assert "env:" in env_block
assert ("SHAPE_LEGS_TOKEN_SET: ${{ secrets.SHAPE_LEGS_TOKEN != '' }}"
in env_block)
Comment on lines +52 to +56
# PINNED, checksum-verified, no `go install`/`pip` package exists for
# this: a static release binary downloaded straight from the tag this
# repository pins. This is what would have caught the `secrets`-in-a-
# step-`if:` defect (a rendered workflow file GitHub itself rejects as
# unparseable) before it ever reached a real project's pull request.
brettheap pushed a commit that referenced this pull request Sep 3, 2026
Main gained the `secrets`-in-a-step-`if:` fix (#8) and the actionlint download
hardening (#9) while this branch was written. The only conflict is the README
line cap in tests/test_repo_hygiene.py, where both changes move it: 265 -> 267
for the SHAPE_LEGS_TOKEN paragraph's `env:` note, then 267 -> 324 for
"Keeping a project's shape current". Both rationales are kept, because a cap
whose reasons are dropped as it moves is not a cap.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01HGKwdQsmvRZx4fGQESdFGZ
@brettheap
brettheap deleted the fix/secrets-in-if branch September 11, 2026 20:06
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.

2 participants