Set `cache-seed` in GH Actions outputs.
(ctx: Context, static_cache_seed: str, randomize: bool = False)
| 376 | }, |
| 377 | ) |
| 378 | def define_cache_seed(ctx: Context, static_cache_seed: str, randomize: bool = False): |
| 379 | """ |
| 380 | Set `cache-seed` in GH Actions outputs. |
| 381 | """ |
| 382 | github_output = os.environ.get("GITHUB_OUTPUT") |
| 383 | if github_output is None: |
| 384 | ctx.warn("The 'GITHUB_OUTPUT' variable is not set.") |
| 385 | ctx.exit(1) |
| 386 | |
| 387 | if TYPE_CHECKING: |
| 388 | assert github_output is not None |
| 389 | |
| 390 | github_step_summary = os.environ.get("GITHUB_STEP_SUMMARY") |
| 391 | if github_step_summary is None: |
| 392 | ctx.warn("The 'GITHUB_STEP_SUMMARY' variable is not set.") |
| 393 | ctx.exit(1) |
| 394 | |
| 395 | if TYPE_CHECKING: |
| 396 | assert github_step_summary is not None |
| 397 | |
| 398 | labels: list[str] = [] |
| 399 | gh_event_path = os.environ.get("GITHUB_EVENT_PATH") or None |
| 400 | if gh_event_path is not None: |
| 401 | try: |
| 402 | gh_event = json.loads(open(gh_event_path, encoding="utf-8").read()) |
| 403 | except Exception as exc: |
| 404 | ctx.error( |
| 405 | f"Could not load the GH Event payload from {gh_event_path!r}:\n", exc # type: ignore[arg-type] |
| 406 | ) |
| 407 | ctx.exit(1) |
| 408 | |
| 409 | labels.extend( |
| 410 | label[0] for label in _get_pr_test_labels_from_event_payload(gh_event) |
| 411 | ) |
| 412 | |
| 413 | if randomize is True: |
| 414 | cache_seed = f"SEED-{random.randint(100, 1000)}" |
| 415 | with open(github_step_summary, "a", encoding="utf-8") as wfh: |
| 416 | wfh.write( |
| 417 | f"The cache seed has been randomized to `{cache_seed}` because " |
| 418 | "`--randomize` was passed to `tools ci define-cache-seed`." |
| 419 | ) |
| 420 | elif "test:random-cache-seed" in labels: |
| 421 | cache_seed = f"SEED-{random.randint(100, 1000)}" |
| 422 | with open(github_step_summary, "a", encoding="utf-8") as wfh: |
| 423 | wfh.write( |
| 424 | f"The cache seed has been randomized to `{cache_seed}` because " |
| 425 | "the label `test:random-cache-seed` was set." |
| 426 | ) |
| 427 | else: |
| 428 | cache_seed = static_cache_seed |
| 429 | |
| 430 | ctx.info("Writing 'cache-seed' to the github outputs file") |
| 431 | with open(github_output, "a", encoding="utf-8") as wfh: |
| 432 | wfh.write(f"cache-seed={cache_seed}\n") |
| 433 | |
| 434 | |
| 435 | @ci.command( |