|
| 1 | +"""Resolve run context for GitHub Actions workflows.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import os |
| 6 | +import sys |
| 7 | +from pathlib import Path |
| 8 | +from typing import Mapping |
| 9 | + |
| 10 | +_REPO_ROOT = Path(__file__).resolve().parents[2] |
| 11 | +if str(_REPO_ROOT) not in sys.path: |
| 12 | + sys.path.insert(0, str(_REPO_ROOT)) |
| 13 | + |
| 14 | +from policyengine_us_data.utils.run_context import ( # noqa: E402 |
| 15 | + DEFAULT_MODAL_APP_PREFIX, |
| 16 | + RUN_ID_ENV, |
| 17 | + RunContext, |
| 18 | + build_modal_resource_name, |
| 19 | + build_run_id, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +def _append_key_values(path_env: str, values: dict[str, str]) -> None: |
| 24 | + output_path = os.environ.get(path_env) |
| 25 | + if not output_path: |
| 26 | + return |
| 27 | + with Path(output_path).open("a") as handle: |
| 28 | + for key, value in values.items(): |
| 29 | + handle.write(f"{key}={value}\n") |
| 30 | + |
| 31 | + |
| 32 | +def _github_actions_run_id(env: Mapping[str, str]) -> str: |
| 33 | + if not env.get("GITHUB_RUN_ID"): |
| 34 | + return "" |
| 35 | + return build_run_id( |
| 36 | + github_run_id=env.get("GITHUB_RUN_ID", ""), |
| 37 | + github_run_attempt=env.get("GITHUB_RUN_ATTEMPT", "1"), |
| 38 | + github_sha=env.get("GITHUB_SHA", ""), |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +def main() -> None: |
| 43 | + env = os.environ |
| 44 | + app_prefix = env.get("US_DATA_MODAL_APP_PREFIX", DEFAULT_MODAL_APP_PREFIX) |
| 45 | + run_id = env.get(RUN_ID_ENV, "") |
| 46 | + context = RunContext.from_env( |
| 47 | + run_id=run_id or _github_actions_run_id(env), |
| 48 | + modal_app_prefix=app_prefix, |
| 49 | + ) |
| 50 | + if not context.run_id: |
| 51 | + raise RuntimeError( |
| 52 | + "Could not resolve run ID. Set US_DATA_RUN_ID or run " |
| 53 | + "inside GitHub Actions with GITHUB_RUN_ID." |
| 54 | + ) |
| 55 | + |
| 56 | + pipeline_volume_name = os.environ.get( |
| 57 | + "US_DATA_PIPELINE_VOLUME_NAME", |
| 58 | + build_modal_resource_name( |
| 59 | + context.run_id, |
| 60 | + prefix="pipeline-artifacts", |
| 61 | + ), |
| 62 | + ) |
| 63 | + staging_volume_name = os.environ.get( |
| 64 | + "US_DATA_STAGING_VOLUME_NAME", |
| 65 | + build_modal_resource_name( |
| 66 | + context.run_id, |
| 67 | + prefix="local-area-staging", |
| 68 | + ), |
| 69 | + ) |
| 70 | + checkpoint_volume_name = os.environ.get( |
| 71 | + "US_DATA_CHECKPOINT_VOLUME_NAME", |
| 72 | + build_modal_resource_name( |
| 73 | + context.run_id, |
| 74 | + prefix="data-build-checkpoints", |
| 75 | + ), |
| 76 | + ) |
| 77 | + context = RunContext.from_mapping( |
| 78 | + { |
| 79 | + **context.to_dict(), |
| 80 | + "pipeline_volume_name": pipeline_volume_name, |
| 81 | + "staging_volume_name": staging_volume_name, |
| 82 | + "checkpoint_volume_name": checkpoint_volume_name, |
| 83 | + }, |
| 84 | + modal_app_name=context.modal_app_name, |
| 85 | + modal_environment=context.modal_environment, |
| 86 | + ) |
| 87 | + |
| 88 | + outputs = { |
| 89 | + "run_id": context.run_id, |
| 90 | + "modal_app_name": context.modal_app_name, |
| 91 | + "modal_environment": context.modal_environment, |
| 92 | + "hf_staging_prefix": context.hf_staging_prefix, |
| 93 | + "github_run_url": context.github_run_url, |
| 94 | + "pipeline_volume_name": context.pipeline_volume_name, |
| 95 | + "staging_volume_name": context.staging_volume_name, |
| 96 | + "checkpoint_volume_name": context.checkpoint_volume_name, |
| 97 | + } |
| 98 | + _append_key_values("GITHUB_OUTPUT", outputs) |
| 99 | + _append_key_values("GITHUB_ENV", context.export_env()) |
| 100 | + print(context.to_json()) |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + main() |
0 commit comments