Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/publish-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,6 @@ jobs:
contents: read
env:
ARCH: ${{ matrix.arch }}
XDG_STATE_HOME: ${{ runner.temp }}/state
AGENT_ENV_DOCUMENT_STORE: local
AGENT_ENV_OBJECT_STORE: local
steps:
Expand Down Expand Up @@ -414,7 +413,10 @@ jobs:
docker run --detach --name registry --publish 5000:5000 \
--volume "$RUNNER_TEMP/computers/registry:/var/lib/registry" mirror.gcr.io/library/registry:2
timeout 30 sh -c 'until curl -sf http://localhost:5000/v2/ >/dev/null; do sleep 1; done'
# The runner context is a step's, not a job's.
- name: Compose and probe
env:
XDG_STATE_HOME: ${{ runner.temp }}/state
run: |
"$RUNNER_TEMP/venv/bin/python" .github/scripts/release_computers.py compose "$ARCH" \
"$RUNNER_TEMP/computers/composed"
Expand Down
27 changes: 27 additions & 0 deletions tst/unit/test_release_infra_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,33 @@ def _finished(job: str) -> str:
return f"""contains(fromJSON('["success", "skipped"]'), needs.{job}.result)"""


# What a job's own env can read; a step's can read the runner too (docs.github.com, "Context availability").
_JOB_ENV_CONTEXTS = {"github", "inputs", "matrix", "needs", "secrets", "strategy", "vars"}


def _job_env_contexts(jobs: dict) -> set[tuple[str, str]]:
"""Each (job, context) a job's own env reads: in each of its expressions, with their quoted strings left out, every
name that opens a property path, not one that follows a dot or is part of a hyphened id (``needs.infra-plan``)."""
return {(name, context) for name, body in jobs.items() for value in (body.get("env") or {}).values()
for expression in re.findall(r"\$\{\{(.*?)\}\}", str(value))
for context in re.findall(r"(?<![\w.-])([A-Za-z_][\w-]*)\.", re.sub(r"'[^']*'", "''", expression))}


def test_every_context_an_expression_reads_is_found_however_it_is_written():
jobs = {"j": {"env": {"A": "${{ needs.build.outputs.path || runner.temp }}", "B": "${{ format('{0}.x', vars.y) }}",
"C": "plain.text ${{ matrix.arch }}", "D": "${{ needs.infra-plan.outputs.missing }}"}}}

assert _job_env_contexts(jobs) == {("j", "needs"), ("j", "runner"), ("j", "vars"), ("j", "matrix")}


@pytest.mark.parametrize("path", sorted((REPO / ".github" / "workflows").glob("*.yml")), ids=lambda path: path.name)
def test_a_jobs_own_env_reads_only_the_contexts_a_job_has(path):
"""GitHub refuses the whole workflow file otherwise, so a release tag starts no run at all."""
read = _job_env_contexts(yaml.safe_load(path.read_text()).get("jobs", {}))

assert {(name, context) for name, context in read if context not in _JOB_ENV_CONTEXTS} == set()


@pytest.mark.parametrize("job", sorted(JOBS))
def test_a_job_that_installs_agent_env_holds_no_token_that_writes(job):
"""Then a dependency's code never runs where a token can push an image, publish to PyPI or edit the repository."""
Expand Down
Loading