|
23 | 23 | # * every `run:` step is under the hard cap (error) and under the soft |
24 | 24 | # warning threshold (warning, so a growing list is noticed with |
25 | 25 | # runway left rather than at the cliff) |
| 26 | +# * a job's worst-case apt setup fits in its timeout-minutes, and every |
| 27 | +# parallel-make-check.py call has a --deadline that fits after it |
26 | 28 | # |
27 | 29 | # Findings are emitted as GitHub workflow commands (::error / ::warning) |
28 | 30 | # so they surface as annotations on the run, and as plain text so the log |
29 | 31 | # is readable when run locally. |
30 | 32 |
|
31 | 33 | import argparse |
| 34 | +import functools |
32 | 35 | import pathlib |
| 36 | +import re |
33 | 37 | import sys |
34 | 38 |
|
35 | 39 | import yaml |
|
41 | 45 | # offending content out of the workflow before it becomes a failure. |
42 | 46 | SOFT_LIMIT = 18000 |
43 | 47 |
|
| 48 | +# Seconds of timeout-minutes left for checkout and the build after setup. |
| 49 | +SETUP_MARGIN = 120 |
| 50 | + |
| 51 | +# Seconds left around a parallel-make-check --deadline for the steps before |
| 52 | +# it (setup-python, brew, autogen.sh) and after it (logs, cache save). |
| 53 | +DEADLINE_MARGIN = 300 |
| 54 | + |
| 55 | +# apt-install.sh's split_budget() limits and kill grace. |
| 56 | +UPDATE_MIN, UPDATE_MAX, INSTALL_MIN, KILL_GRACE = 20, 90, 40, 10 |
| 57 | + |
| 58 | +# The apt-install.sh limits ccache-setup falls back to. |
| 59 | +CCACHE_APT = {"tries": 2, "update": 60, "install": 90} |
| 60 | + |
| 61 | +ACTIONS = pathlib.Path(__file__).resolve().parent.parent / "actions" |
| 62 | +PMC_OPTION = re.compile(r"--(timeout|deadline)[= ]\s*['\"]?([^\s'\"\\]+)") |
| 63 | + |
44 | 64 |
|
45 | 65 | def iter_run_steps(doc: object) -> list[tuple[str, str]]: |
46 | 66 | """Yield (location, script) for every `run:` step in a parsed file. |
@@ -80,6 +100,131 @@ def scan_steps(steps: object, where: str) -> None: |
80 | 100 | return found |
81 | 101 |
|
82 | 102 |
|
| 103 | +@functools.cache |
| 104 | +def apt_defaults() -> dict: |
| 105 | + """The install-apt-deps input defaults, read from the action itself.""" |
| 106 | + doc = yaml.safe_load((ACTIONS / "install-apt-deps" / "action.yml") |
| 107 | + .read_text()) |
| 108 | + return {k: v.get("default", "") for k, v in doc["inputs"].items()} |
| 109 | + |
| 110 | + |
| 111 | +def apt_install_worst(tries: int, budget: int = 0, delay: int = 5, |
| 112 | + update: int = 90, install: int = 300) -> int: |
| 113 | + """Worst-case seconds of apt-install.sh, replaying its budget split.""" |
| 114 | + spent = 0 |
| 115 | + for n in range(1, tries + 1): |
| 116 | + up, inst = update, install |
| 117 | + if budget: |
| 118 | + per = int((budget - spent) / (tries - n + 1)) |
| 119 | + up = min(UPDATE_MAX, max(UPDATE_MIN, int(per / 2))) |
| 120 | + inst = max(INSTALL_MIN, per - up) |
| 121 | + spent += up + inst + 2 * KILL_GRACE |
| 122 | + if n < tries: |
| 123 | + spent += delay |
| 124 | + delay *= 2 |
| 125 | + return spent |
| 126 | + |
| 127 | + |
| 128 | +def apt_worst(inputs: dict) -> int: |
| 129 | + """Worst-case seconds of one install-apt-deps step, or -1 if unknown.""" |
| 130 | + try: |
| 131 | + pull = int(inputs["pull-timeout"]) + KILL_GRACE |
| 132 | + worst = apt_install_worst(int(inputs["retries"]), |
| 133 | + int(inputs["budget-seconds"]), |
| 134 | + int(inputs["retry-delay"])) |
| 135 | + except (KeyError, TypeError, ValueError): |
| 136 | + return -1 |
| 137 | + return worst + (pull if inputs.get("ghcr-debs-tag") else 0) |
| 138 | + |
| 139 | + |
| 140 | +def check_timeouts(path: pathlib.Path, doc: object, defaults: dict) -> int: |
| 141 | + """Check each job's apt setup and run deadlines against timeout-minutes.""" |
| 142 | + errors = 0 |
| 143 | + jobs = doc.get("jobs") if isinstance(doc, dict) else None |
| 144 | + if not isinstance(jobs, dict): |
| 145 | + return 0 |
| 146 | + |
| 147 | + def error(where: str, msg: str) -> None: |
| 148 | + nonlocal errors |
| 149 | + print(f"::error file={path}::{where}: {msg}") |
| 150 | + errors += 1 |
| 151 | + |
| 152 | + for job_id, job in jobs.items(): |
| 153 | + if not isinstance(job, dict) or not isinstance(job.get("steps"), list): |
| 154 | + continue |
| 155 | + always = 0 |
| 156 | + # install-apt-deps steps behind an if: are taken as alternatives. |
| 157 | + branches = [0] |
| 158 | + runs = [] |
| 159 | + for i, step in enumerate(job["steps"]): |
| 160 | + if not isinstance(step, dict): |
| 161 | + continue |
| 162 | + where = f"jobs.{job_id} / {step.get('name') or f'step {i + 1}'}" |
| 163 | + run = step.get("run") |
| 164 | + for part in str(run or "").split("parallel-make-check.py")[1:]: |
| 165 | + cmd = re.split(r"(?<!\\)\n", part, maxsplit=1)[0] |
| 166 | + runs.append((where, dict(PMC_OPTION.findall(cmd)))) |
| 167 | + uses = str(step.get("uses") or "") |
| 168 | + if uses.endswith("/install-apt-deps"): |
| 169 | + with_ = step.get("with") |
| 170 | + cost = apt_worst({**defaults, |
| 171 | + **(with_ if isinstance(with_, dict) |
| 172 | + else {})}) |
| 173 | + if cost < 0: |
| 174 | + error(where, "give retries, retry-delay, budget-seconds " |
| 175 | + "and pull-timeout literal numbers so the " |
| 176 | + "setup budget can be checked") |
| 177 | + elif "if" in step: |
| 178 | + branches.append(cost) |
| 179 | + else: |
| 180 | + always += cost |
| 181 | + elif uses.endswith("/ccache-setup") \ |
| 182 | + and "macos" not in str(job.get("runs-on", "")): |
| 183 | + always += apt_install_worst(**CCACHE_APT) |
| 184 | + setup = always + max(branches) |
| 185 | + |
| 186 | + limit = job.get("timeout-minutes", 360) |
| 187 | + fixed = isinstance(limit, (int, float)) |
| 188 | + if fixed and setup and setup + SETUP_MARGIN > limit * 60: |
| 189 | + need = -(-(setup + SETUP_MARGIN) // 60) |
| 190 | + error(f"jobs.{job_id}", |
| 191 | + f"apt setup can take {setup}s against a slow or dead " |
| 192 | + f"mirror, which leaves no room in timeout-minutes: {limit}. " |
| 193 | + f"The job is then cancelled instead of failing with the " |
| 194 | + f"apt error. Raise timeout-minutes to at least {need}, or " |
| 195 | + f"lower budget-seconds / pull-timeout.") |
| 196 | + |
| 197 | + deadlines = 0.0 |
| 198 | + for where, opts in runs: |
| 199 | + traw, draw = opts.get("timeout", ""), opts.get("deadline", "") |
| 200 | + if "$" in traw + draw: |
| 201 | + print(f"::warning file={path}::{where}: --timeout/--deadline " |
| 202 | + f"is not a literal, so it is not checked") |
| 203 | + continue |
| 204 | + try: |
| 205 | + timeout, deadline = float(traw), float(draw) |
| 206 | + except ValueError: |
| 207 | + timeout = deadline = -1.0 |
| 208 | + if timeout <= 0 or deadline <= 0 or timeout > deadline: |
| 209 | + error(where, f"parallel-make-check.py needs --timeout and " |
| 210 | + f"--deadline above 0 with --timeout no larger, " |
| 211 | + f"got --timeout='{traw}' --deadline='{draw}'. " |
| 212 | + f"Without them a wedged config runs into " |
| 213 | + f"timeout-minutes, which cancels the job and " |
| 214 | + f"loses every config's log.") |
| 215 | + continue |
| 216 | + deadlines += deadline |
| 217 | + need = -(-(setup + deadlines * 60 + DEADLINE_MARGIN) // 60) |
| 218 | + if fixed and need > limit: |
| 219 | + error(where, f"--deadline={draw} plus {setup}s of worst-case " |
| 220 | + f"apt setup does not fit timeout-minutes: " |
| 221 | + f"{limit}, so the job can be cancelled before " |
| 222 | + f"the deadline fires. Raise timeout-minutes to " |
| 223 | + f"at least {int(need)}, or lower --deadline.") |
| 224 | + |
| 225 | + return errors |
| 226 | + |
| 227 | + |
83 | 228 | def check_file(path: pathlib.Path) -> tuple[int, int, int]: |
84 | 229 | """Lint one file. Returns (errors, warnings, largest run: step).""" |
85 | 230 | errors = 0 |
@@ -113,13 +258,15 @@ def check_file(path: pathlib.Path) -> tuple[int, int, int]: |
113 | 258 | f"limit the whole file stops loading.") |
114 | 259 | warnings += 1 |
115 | 260 |
|
| 261 | + errors += check_timeouts(path, doc, apt_defaults()) |
| 262 | + |
116 | 263 | return (errors, warnings, biggest) |
117 | 264 |
|
118 | 265 |
|
119 | 266 | def main() -> int: |
120 | 267 | p = argparse.ArgumentParser( |
121 | 268 | description="Lint GitHub Actions workflow files for the 21000 " |
122 | | - "character per-run-step limit.") |
| 269 | + "character per-run-step limit and job time budgets.") |
123 | 270 | p.add_argument("paths", nargs="*", metavar="FILE", |
124 | 271 | help="files to check (default: all workflows and " |
125 | 272 | "composite actions under .github/)") |
@@ -151,7 +298,7 @@ def main() -> int: |
151 | 298 | print(f"checked {len(paths)} files; largest run: step is {biggest} " |
152 | 299 | f"characters (limit {HARD_LIMIT})") |
153 | 300 | if errors: |
154 | | - print(f"FAILED: {errors} step(s) over the limit") |
| 301 | + print(f"FAILED: {errors} error(s)") |
155 | 302 | return 1 |
156 | 303 | if warnings: |
157 | 304 | print(f"{warnings} step(s) approaching the limit") |
|
0 commit comments