diff --git a/.github/actions/resolve-upstream/action.yml b/.github/actions/resolve-upstream/action.yml index 78f87ede2..d4b79da87 100644 --- a/.github/actions/resolve-upstream/action.yml +++ b/.github/actions/resolve-upstream/action.yml @@ -2,6 +2,14 @@ name: Hub resolve-upstream hook description: Resolve the newest released upstream line of each host tool the hub pins to one line. +# Declared so check-upstream-version-task.yml's guarded hook-input step passes actionlint here, and as the declaration a caller's own hook copies. +# The hub calls the task once, so this hook ignores the value. +inputs: + hook-input: + description: The caller's hook-input value, which the task passes only when it is non-empty. + required: false + default: '' + outputs: versions: description: Compact JSON object of pinned tool name -> newest released line, such as python 3.14 and dotnet 10. diff --git a/.github/actions/validate/action.yml b/.github/actions/validate/action.yml index 0e00a5853..3b6072bc2 100644 --- a/.github/actions/validate/action.yml +++ b/.github/actions/validate/action.yml @@ -6,6 +6,14 @@ name: Validate repository (hub) description: Registry and spec validation, the audit helper suite and the self-tests outside the unittest suite, a strict type check of the carried hub_fetch_run.py, the fleet-skills freshness check, and the unclassified-character report. +# Declared so validate-task.yml's guarded hook-input step passes actionlint here, and as the declaration a caller's own hook copies. +# The hub's own caller passes no hook-input, so this hook ignores the value. +inputs: + hook-input: + description: The caller's hook-input value, which the task passes only when it is non-empty. + required: false + default: '' + runs: using: composite steps: diff --git a/.github/workflows/check-upstream-version-task.yml b/.github/workflows/check-upstream-version-task.yml index ddda959d3..86e43aef3 100644 --- a/.github/workflows/check-upstream-version-task.yml +++ b/.github/workflows/check-upstream-version-task.yml @@ -39,6 +39,14 @@ on: required: false type: boolean default: true + hook-input: + description: >- + Free-form string handed to the resolve-upstream hook as its hook-input input, only when non-empty. + A repository calling this task more than once passes each call its own value, so one hook can tell which caller runs it. + A hook receiving a non-empty value declares hook-input as an input. + required: false + type: string + default: '' secrets: CODEGEN_APP_CLIENT_ID: required: true @@ -75,11 +83,20 @@ jobs: token: ${{ steps.app-token.outputs.token }} persist-credentials: false + # The caller's hook-input reaches the hook only from a step of its own, run when the caller set it. + # So a caller passing none runs a hook declaring no such input, such as one written before hook-input existed, exactly as before. - name: Run resolve-upstream hook step id: resolve-upstream - if: ${{ hashFiles('.github/actions/resolve-upstream/action.yml') != '' }} + if: ${{ hashFiles('.github/actions/resolve-upstream/action.yml') != '' && inputs.hook-input == '' }} uses: ./.github/actions/resolve-upstream + - name: Run resolve-upstream hook with hook input step + id: resolve-upstream-with-input + if: ${{ hashFiles('.github/actions/resolve-upstream/action.yml') != '' && inputs.hook-input != '' }} + uses: ./.github/actions/resolve-upstream + with: + hook-input: ${{ inputs.hook-input }} + - name: Missing resolve-upstream hook step if: ${{ hashFiles('.github/actions/resolve-upstream/action.yml') == '' }} run: | @@ -93,7 +110,7 @@ jobs: - name: Compute upstream version diff step id: resolve env: - NEW_VERSIONS: ${{ steps.resolve-upstream.outputs.versions }} + NEW_VERSIONS: ${{ steps.resolve-upstream.outputs.versions || steps.resolve-upstream-with-input.outputs.versions }} STATE_FILE: ${{ inputs.state-file }} BUMP_BRANCH_PREFIX: ${{ inputs.bump-branch-prefix }} AUTO_MERGE: ${{ inputs.auto-merge }} diff --git a/.github/workflows/validate-task.yml b/.github/workflows/validate-task.yml index b1423e1d3..21d1bfd2c 100644 --- a/.github/workflows/validate-task.yml +++ b/.github/workflows/validate-task.yml @@ -44,6 +44,12 @@ on: required: false type: string default: release + # Handed to the caller's validate hook by the validate job's guarded hook step, never to the hub default. + hook-input: + description: Free-form string handed to the caller's validate hook as its hook-input input, only when non-empty, for example a change-gate result so an expensive domain check can skip a change that does not touch it. A hook receiving a non-empty value declares hook-input as an input. A caller carrying no validate hook runs the hub's no-op default, which never receives it. + required: false + type: string + default: '' secrets: CODECOV_TOKEN: required: false @@ -1096,10 +1102,18 @@ jobs: # A local composite action resolves at step time from the workspace, which is what makes this fallback expressible at all. # It runs the caller's own hook when it carries one, else the hub's no-op default. + # The caller's hook-input reaches the hook only from a step of its own, run when the caller set it. + # So a caller passing none runs a hook declaring no such input exactly as before. - name: Run repository validate hook step - if: hashFiles('.github/actions/validate/action.yml') != '' + if: hashFiles('.github/actions/validate/action.yml') != '' && inputs.hook-input == '' uses: ./.github/actions/validate + - name: Run repository validate hook with hook input step + if: hashFiles('.github/actions/validate/action.yml') != '' && inputs.hook-input != '' + uses: ./.github/actions/validate + with: + hook-input: ${{ inputs.hook-input }} + - name: Run default validate hook step if: hashFiles('.github/actions/validate/action.yml') == '' uses: $/.github/actions/validate-default diff --git a/docs/reusable-workflows.md b/docs/reusable-workflows.md index 3890a8add..50716089b 100644 --- a/docs/reusable-workflows.md +++ b/docs/reusable-workflows.md @@ -56,7 +56,7 @@ A workflow whose job graph is identical across repos of a type is reached, not c ### The Hook Contract -A hook receives the fixed inputs [WORKFLOW.md "Reusable-Task Parameter Contract"][workflow-reusable-task-parameter-contract] gives a leaf today. Those are `ref`, `branch`, `smoke` where relevant, and the NBGV version outputs where a build needs them. It reports back through step outputs and, for a release target, through the existing artifact seam, `release-asset--`, which the hub's `github-release` job collects by pattern. Each task finds a hook by its `action.yml` alone, so a hook named `action.yaml` is never run. The `install-test-deps` hook is an exception, taking `leg` and `directories` inputs and reporting nothing back. It may also run a check that needs that leg's environment. A failure there fails the leg's `unit-test` job before its Python tests run. The `pre-test` hook is another, taking no inputs and reporting nothing back. Each `unit-test` leg runs it before the .NET SDK setup, where a .NET test project exists and the caller carries the hook. So a system tool a .NET test shells out to is installed before those tests run. The `build-release-asset` hook is another, writing its files into the `output-directory` it receives, on a smoke run too, and leaving the upload to the task. The task accepts only regular files at that directory's top level and refuses a subdirectory, a symlink, or a dot-prefixed name. A package hook also uploads the artifact its publish job consumes: a NuGet hook uploads `nuget-build-` as well as its `release-asset-*`. A PyPI hook uploads `pypi-build-` and no release asset at all. A NuGet or PyPI hook has to carry that exact package-artifact name or its publish job finds nothing. A hook may use marketplace actions, which is the reason a hook is a composite action rather than a script. A toolchain setup, a Docker build, or a coverage upload is a `uses:` step, and a shell script cannot carry one. Action pins inside a hook follow the SHA-pinning rule like any other workflow content, and Dependabot bumps them in the repo that carries the hook. A hook runs with no persisted git credential, since every checkout ahead of a hook sets `persist-credentials: false`. A hook that runs git against the remote therefore passes its own credential to that call. +A hook receives the fixed inputs [WORKFLOW.md "Reusable-Task Parameter Contract"][workflow-reusable-task-parameter-contract] gives a leaf today. Those are `ref`, `branch`, `smoke` where relevant, and the NBGV version outputs where a build needs them. It reports back through step outputs and, for a release target, through the existing artifact seam, `release-asset--`, which the hub's `github-release` job collects by pattern. Each task finds a hook by its `action.yml` alone, so a hook named `action.yaml` is never run. The `install-test-deps` hook is an exception, taking `leg` and `directories` inputs and reporting nothing back. It may also run a check that needs that leg's environment. A failure there fails the leg's `unit-test` job before its Python tests run. The `pre-test` hook is another, taking no inputs and reporting nothing back. Each `unit-test` leg runs it before the .NET SDK setup, where a .NET test project exists and the caller carries the hook. So a system tool a .NET test shells out to is installed before those tests run. The `build-release-asset` hook is another, writing its files into the `output-directory` it receives, on a smoke run too, and leaving the upload to the task. The task accepts only regular files at that directory's top level and refuses a subdirectory, a symlink, or a dot-prefixed name. A package hook also uploads the artifact its publish job consumes: a NuGet hook uploads `nuget-build-` as well as its `release-asset-*`. A PyPI hook uploads `pypi-build-` and no release asset at all. A NuGet or PyPI hook has to carry that exact package-artifact name or its publish job finds nothing. The `validate` and `resolve-upstream` hooks can also take one string from their caller. `validate-task.yml` and `check-upstream-version-task.yml` each declare an optional `hook-input` input. Each passes a non-empty value on to its hook's own `hook-input` input. The task passes it from a step of its own, run only when the caller sets it. So a caller passing none runs a hook declaring no such input exactly as before. A caller that sets it needs a hook declaring `hook-input` as an optional input. The caller defines what the value means. It can name which of two callers runs the hook, or carry a change-gate result an expensive check skips on. The hub's no-op `validate-default` never receives it. A hook may use marketplace actions, which is the reason a hook is a composite action rather than a script. A toolchain setup, a Docker build, or a coverage upload is a `uses:` step, and a shell script cannot carry one. Action pins inside a hook follow the SHA-pinning rule like any other workflow content, and Dependabot bumps them in the repo that carries the hook. A hook runs with no persisted git credential, since every checkout ahead of a hook sets `persist-credentials: false`. A hook that runs git against the remote therefore passes its own credential to that call. ### Pinning @@ -239,7 +239,7 @@ Hub: `publish-docker-readme-task.yml` with a `docker-readme-transform` hook, `ch - [x] VSCode-Server-DotNetCore (`publish-docker-readme-task.yml`): `publish-release.yml` on `main`, its ground-truth branch, calls `publish-docker-readme-task.yml` by pin and keeps no local copy, at `e571822`. There the `main` audit run 2026-10-16T06:40:34Z reports no `interface` finding. - [x] PlexCleaner (`publish-docker-readme-task.yml`): adopted on `develop` in ptr727/PlexCleaner#940 at `0e9b0b9`, merged 2026-08-30 and carried on `main`, its ground-truth branch. There `publish-release.yml` calls `publish-docker-readme-task.yml` by pin and keeps no local copy. - [ ] NxWitness (`publish-docker-readme-task.yml`, moving the readme job out of `publish-release.yml`, and `run-codegen-pull-request-task.yml` with its scheduler) - - [ ] ESPHome-NonRoot (`check-upstream-version-task.yml`, both trackers, the second with `auto-merge: false`) + - [ ] ESPHome-NonRoot (`check-upstream-version-task.yml`, both trackers, the second with `auto-merge: false` and `hook-input`) - [ ] Blog (`deploy-site-task.yml`, keeping `deploy-site.yml` as its own caller) - [ ] LanguageTags (`run-codegen-pull-request-task.yml` and its scheduler) - [x] KiCadLibrary (deletes `build-datebadge-task.yml` and its caller job outright, per TODO.md's retired-badge cleanup, adopting no new stub): deleted in ptr727/KiCadLibrary#59. The change was promoted to `main` in ptr727/KiCadLibrary#65, where the `main` audit run 2026-10-04T01:15:54Z reports no `interface` finding. @@ -314,6 +314,8 @@ An operational repo also names its model in the `workflow-model` input, which de The value is the registry's `workflowModel`, and any value other than `release` runs the rule on a promotion. A caller reaches the input only from a release carrying it. An operational repo whose pin predates one therefore advances the pin before adding the input. The no-build caller snippet passes it. The audit's `interface.workflow-model` check reports an operational caller that passes no `workflow-model: operational`, and a release caller passing any value other than `release`. +A repo whose `validate` hook runs an expensive domain check, such as a firmware compile, can pass its own change-gate result in the `hook-input` input. The hook then skips a change that does not touch the check. It no longer re-derives the event and the diff, a second copy of logic the caller holds. The hook declares `hook-input` as an optional input and reads it as `inputs.hook-input`. A caller reaches the input only from a release carrying it, so its pin advances first. + **Release repos with a smoke build** carry the standard `pull_request` trigger, a `changes` paths-filter job, and a `smoke-build` job. The paths-filter follows WORKFLOW.md D1.1: each of the repo's own targets gets an entry listing that target's own paths. That leaves both docs and `.github/workflows/**` matching nothing and satisfies D1.4 without naming a workflow path at all. The smoke build reaches the hub's `build-release-task.yml` by pin, the same task the repo's own `publish-release.yml` calls. So the repo deletes its copy of `build-release-task.yml` per the `retire` disposition in `spec/divergences.json`. So the repo does not keep one for the smoke leg, per [Adopting the Release Chain][adopting-the-release-chain]. ```yaml @@ -770,7 +772,7 @@ The caller grants `contents: read` explicitly. This is because the task's own jo CODEGEN_APP_PRIVATE_KEY: ${{ secrets.CODEGEN_APP_PRIVATE_KEY }} ``` -ESPHome-NonRoot carries two trackers today. `check-upstream-version.yml` adopts the stub above as-is. `check-upstream-dependency.yml` adopts a second instance of the same stub with `with: { branches: '["develop"]', bump-branch-prefix: upstream-dependency, auto-merge: false }`. Its bump waits for a human, because its head deliberately does not match a merge-bot rule. `check-upstream-dependency.yml` also adopts a `resolve-upstream` hook shaped around its apt-package snapshot, setting `versions` to `{"docker_base_packages": ""}` rather than a name-to-version map. The generic title and body this produces read less specifically than today's bespoke "packages added/removed" wording. That is the cost of folding a bespoke tracker into the shared task. +ESPHome-NonRoot carries two trackers today. `check-upstream-version.yml` adopts the stub above as-is. `check-upstream-dependency.yml` adopts a second instance of the same stub with `with: { branches: '["develop"]', state-file: upstream-dependency.json, bump-branch-prefix: upstream-dependency, auto-merge: false, hook-input: dependency }`. Its bump waits for a human, because its head deliberately does not match a merge-bot rule. Its own `state-file` keeps each tracker's bump from rewriting the other's state. Both instances run the repository's one `resolve-upstream` hook, since the task finds the hook at that fixed path. So the hook declares `hook-input` and branches on it. Given `dependency`, it sets `versions` from its apt-package snapshot, to `{"docker_base_packages": ""}` rather than a name-to-version map. Given no value, it resolves the upstream version as the first tracker does. The generic title and body this produces read less specifically than today's bespoke "packages added/removed" wording. That is the cost of folding a bespoke tracker into the shared task. **Deploy-site.** A site repo keeps `deploy-site.yml` as a per-repo caller. It has no manifest-wide catalog snippet either. That is because its `uses:` names the hub. It is also because it still carries the dispatch, the ref gate, and the shared validation call. Its `deploy` job nonetheless reaches the hub-hosted `deploy-site-task.yml`. A job calling a reusable workflow cannot itself carry an `environment:` key. [GitHub's supported-keywords list][gh-reusing-workflows] omits it, and GitHub rejects the combination outright. So the caller's `deploy` job carries none. `DEPLOY_SSH_PRIVATE_KEY` still crosses correctly. The task's own `deploy` job binds `environment: ${{ inputs.environment }}` on itself. Per GitHub's own cross-repository behavior, that resolves against the caller's own GitHub Environment store, not the hub's. That behavior is that the `github` context, and an OIDC token's `sub` claim, always attribute the environment to the *caller*. The caller's `secrets: DEPLOY_SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_SSH_PRIVATE_KEY }}` line is still required. The task declares this a required `workflow_call.secrets` input, and GitHub rejects a call omitting a required one. But for a secret the environment defines, the value the task's own job sees is governed by the task's own `environment:` binding. It is not governed by anything the caller's own (environment-less) job context could resolve. Confirmed against GitHub's documented reusable-workflow secrets mechanics. Also confirmed with a live cross-repository run against Blog's own `staging` environment ([issue #942][issue-942]). `secrets: inherit` was never the alternative here regardless. It is not used on a cross-repository call at all, per [Secrets and Permissions][secrets-and-permissions], and it would not carry an environment-scoped secret across one either. A required `deploy` hook, `.github/actions/deploy/action.yml`, is invoked three times (`build`, `prune`, `verify`). That is so the site keeps its own generator, precompression, and URL contract while the upload-then-flip sequence stays hub-owned. The hook declares all four inputs the three invocations use between them, `mode`, `bundle-path`, `release-id`, and `environment`. That is because a composite action rejects an invocation that supplies an input it does not declare. That holds even for one a different mode leaves unset. Each invocation also passes the GitHub Environment variables that mode needs as plain `env:` vars. These are `SITE_BASE_URL` to `build` and `verify`, and `DEPLOY_SSH_USER` and `DEPLOY_SSH_HOST` to `prune`. It passes them that way since a composite action's own steps are not guaranteed to read the caller's `vars` context directly. `verify` additionally receives an optional `SITE_AUTH_TOKEN_ID`/`SITE_AUTH_TOKEN` secret pair the same way, forwarded whenever the caller maps it. A site whose live check sits behind its own token-gated auth is the reason it exists. `verify` also receives the `SITE_EXTRA_BASE_URL` variable and an optional `SITE_EXTRA_AUTH_TOKEN_ID`/`SITE_EXTRA_AUTH_TOKEN` pair. They are for a second site the same deploy serves on its own hostname behind its own gate. diff --git a/tests/test_workflow_steps.py b/tests/test_workflow_steps.py index 94ceb3740..5864aaeda 100755 --- a/tests/test_workflow_steps.py +++ b/tests/test_workflow_steps.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -"""Prove the shared step reader stays inside its step, and the promotion history step reads main right.""" +"""Prove the step reader stays inside its step, the promotion history step reads main right, and hook-input reaches a hook only when set.""" from __future__ import annotations @@ -9,6 +9,7 @@ import tempfile import unittest from pathlib import Path +from typing import NamedTuple from host_capability import bash_or_skip from workflow_steps import step_script, step_script_text, yaml_step, yaml_value @@ -18,6 +19,7 @@ import git_env VALIDATE_TASK_WORKFLOW = REPO / ".github" / "workflows" / "validate-task.yml" +UPSTREAM_TASK_WORKFLOW = REPO / ".github" / "workflows" / "check-upstream-version-task.yml" WORKFLOW = """\ jobs: @@ -56,6 +58,138 @@ def test_run_key_nested_under_another_key_is_not_the_steps(self) -> None: step_script_text(text, "A") +class HookRoute(NamedTuple): + """One task's hook, the two steps that run it, and the hub's own copy of it.""" + + workflow: Path + hook: str + plain_step: str + input_step: str + + +HOOK_ROUTES = ( + HookRoute( + UPSTREAM_TASK_WORKFLOW, + "./.github/actions/resolve-upstream", + "Run resolve-upstream hook step", + "Run resolve-upstream hook with hook input step", + ), + HookRoute( + VALIDATE_TASK_WORKFLOW, + "./.github/actions/validate", + "Run repository validate hook step", + "Run repository validate hook with hook input step", + ), +) + + +def unwrap(expression: str | None) -> str: + """An `if:` value with any `${{ }}` wrapper removed, so both spellings read alike.""" + text = (expression or "").strip() + if text.startswith("${{") and text.endswith("}}"): + text = text[3:-2].strip() + return text + + +def hook_input_violations(text: str, route: HookRoute) -> list[str]: + """What keeps `route`'s task from handing hook-input to its hook only when the caller set it.""" + found = [] + inputs = ("on", "workflow_call", "inputs", "hook-input") + declared = tuple(yaml_value(text, *inputs, key) for key in ("required", "type", "default")) + if declared != ("false", "string", "''"): + found.append("hook-input is not an optional string input defaulting to empty") + present = f"hashFiles('{route.hook.removeprefix('./')}/action.yml') != ''" + for name, guard in ((route.plain_step, "== ''"), (route.input_step, "!= ''")): + step = yaml_step(text, name) + if yaml_value(step, "uses") != route.hook: + found.append(f"{name} does not run {route.hook}") + if unwrap(yaml_value(step, "if")) != f"{present} && inputs.hook-input {guard}": + found.append(f"{name} is not gated on the hook existing and hook-input {guard}") + if yaml_value(yaml_step(text, route.plain_step), "with") is not None: + found.append(f"{route.plain_step} passes the hook an input") + passed = yaml_step(text, route.input_step) + if yaml_value(passed, "with", "hook-input") != "${{ inputs.hook-input }}": + found.append(f"{route.input_step} does not pass hook-input through") + return found + + +class HookInputCase(unittest.TestCase): + """Each task hands hook-input to its hook from a step of its own, so a caller passing none hands its hook nothing.""" + + def test_each_task_is_clean(self) -> None: + for route in HOOK_ROUTES: + with self.subTest(workflow=route.workflow.name): + self.assertEqual([], hook_input_violations(route.workflow.read_text(), route)) + + def test_each_mutation_is_caught(self) -> None: + for route in HOOK_ROUTES: + text = route.workflow.read_text() + passed = " hook-input: ${{ inputs.hook-input }}\n" + uses = f" uses: {route.hook}\n" + start = text.index(f"- name: {route.plain_step}\n") + end = text.index(uses, start) + len(uses) + mutations = { + "input step always passes": ( + text.replace(" && inputs.hook-input != ''", "", 1), + f"{route.input_step} is not gated", + ), + "plain step always runs": ( + text.replace(" && inputs.hook-input == ''", "", 1), + f"{route.plain_step} is not gated", + ), + "value miswired": ( + text.replace(passed, passed.replace("inputs.", "inputs.x"), 1), + f"{route.input_step} does not pass hook-input through", + ), + "input dropped": ( + text.replace(" with:\n" + passed, "", 1), + f"{route.input_step} does not pass hook-input through", + ), + "plain step passes it": ( + text[:end] + " with:\n" + passed + text[end:], + f"{route.plain_step} passes the hook an input", + ), + "input required": ( + text.replace( + " required: false\n type: string\n default: ''\n secrets:", + " required: true\n type: string\n default: ''\n secrets:", + 1, + ), + "hook-input is not an optional string input", + ), + "default dropped": ( + text.replace(" default: ''\n secrets:", " secrets:", 1), + "hook-input is not an optional string input", + ), + } + for label, (mutated, expected) in mutations.items(): + with self.subTest(workflow=route.workflow.name, mutation=label): + self.assertNotEqual(text, mutated) + self.assertTrue( + any(v.startswith(expected) for v in hook_input_violations(mutated, route)) + ) + + def test_the_upstream_task_reads_versions_from_either_step(self) -> None: + script_env = yaml_value( + yaml_step(UPSTREAM_TASK_WORKFLOW.read_text(), "Compute upstream version diff step"), + "env", + "NEW_VERSIONS", + ) + self.assertEqual( + "${{ steps.resolve-upstream.outputs.versions" + " || steps.resolve-upstream-with-input.outputs.versions }}", + script_env, + ) + + def test_each_hub_hook_declares_hook_input_as_optional(self) -> None: + for route in HOOK_ROUTES: + action = REPO / route.hook.removeprefix("./") / "action.yml" + with self.subTest(hook=route.hook): + self.assertEqual( + "false", yaml_value(action.read_text(), "inputs", "hook-input", "required") + ) + + class PromotionHistoryCase(unittest.TestCase): """A single-parent commit on main since its previous promotion fails the next promotion.