Files
Open_Ultra_SAST/dagger/ci.py
T
Marius Ciepluch 75233fdf14 ci(dagger): give the mounted tree a git index
The dagger job mounts the checkout without `.git` (excluded from the synced
directory), and eleven tests need it. The detection-core final gate and
ten tests in the migration files list tracked Python files with
`git ls-files` under check=True, so in run 45352669096 each failed for
want of that index soon after it started, while the job's 52 performance
tests passed.

The container now trusts the worktree, which can be owned by another user
than the container's, and builds an index of the mounted tree before uv
runs. In python:3.11-bookworm the final gate then passes (1 passed in
358 s) and the index holds the same 1031 Python files as on the host;
without the index the final gate fails in 12 s.
2026-11-24 20:40:48 +01:00

89 lines
3.3 KiB
Python

from __future__ import annotations
import sys
import anyio
import dagger
# Substrings that mark a transient registry / image-pull failure (e.g. a Docker
# Hub 5xx on the python:3.11-bookworm pull, or a registry.dagger.io timeout) that
# is worth retrying rather than failing the whole pipeline.
_TRANSIENT_MARKERS = (
"pull image",
"failed to authorize",
"failed to fetch oauth",
"TransportQueryError",
"i/o timeout",
"connection reset",
"context deadline exceeded",
" 502",
" 503",
" 520",
" 429",
)
async def _run_checks() -> None:
config = dagger.Config(log_output=sys.stderr)
async with dagger.Connection(config) as client:
source = client.host().directory(
".",
exclude=[
".git",
".mypy_cache",
".pytest_cache",
".ruff_cache",
".venv",
"**/.openultrasast",
"dist",
],
)
checks = (
client.container()
.from_("python:3.11-bookworm")
.with_directory("/src", source)
.with_workdir("/src")
# The mounted tree carries no `.git` (excluded above), and tests that enumerate tracked
# files, such as the detection-core migration gate, need an index: give them this tree's.
# The tree can be owned by another user than the container's, so git must trust it first.
.with_exec(["git", "config", "--system", "--add", "safe.directory", "/src"])
.with_exec(["git", "init", "-q"])
.with_exec(["git", "add", "-A"])
.with_exec(["python", "-m", "pip", "install", "uv"])
# uv run re-syncs the environment to the lockfile's default extras unless told not to, which silently
# removed the extras below before pytest ran.
.with_env_variable("UV_NO_SYNC", "1")
.with_exec(["uv", "sync", "--group", "dev", "--extra", "hunt", "--extra", "semantic"])
.with_exec(["uv", "run", "ruff", "format", "--check", "."])
.with_exec(["uv", "run", "ruff", "check", "."])
.with_exec(["uv", "run", "mypy", "src/openultrasast"])
.with_exec(["uv", "run", "pytest"])
.with_exec(["uv", "run", "python", "-m", "openultrasast.gate"])
.with_exec(["uv", "run", "python", "-m", "openultrasast.map_gate"])
.with_exec(["uv", "run", "python", "-m", "openultrasast.pair_gate"])
.with_exec(["uv", "run", "python", "-m", "compileall", "src", "tests"])
.with_exec(["uv", "build"])
)
await checks.sync()
async def main() -> None:
attempts = 3
for attempt in range(1, attempts + 1):
try:
await _run_checks()
return
except Exception as exc: # noqa: BLE001 — retry only transient registry/pull failures
message = str(exc)
transient = any(marker in message for marker in _TRANSIENT_MARKERS)
if attempt < attempts and transient:
print(f"dagger: transient registry/pull failure on attempt {attempt}; retrying in 20s", file=sys.stderr)
await anyio.sleep(20)
continue
raise
if __name__ == "__main__":
anyio.run(main)