diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 010872c980f..670a2854fed 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -45,8 +45,6 @@ jobs: - name: Checkout the source code uses: actions/checkout@v4 - name: Calculate the CI job matrix - env: - GITHUB_CTX: "${{ toJSON(github) }}" run: python3 src/ci/github-actions/calculate-job-matrix.py >> $GITHUB_OUTPUT id: jobs job: diff --git a/src/ci/github-actions/calculate-job-matrix.py b/src/ci/github-actions/calculate-job-matrix.py index 935b1bb56c3..a844b201c16 100755 --- a/src/ci/github-actions/calculate-job-matrix.py +++ b/src/ci/github-actions/calculate-job-matrix.py @@ -7,6 +7,7 @@ be executed on CI. It reads job definitions from `src/ci/github-actions/jobs.yml` and filters them based on the event that happened on CI. """ +import dataclasses import enum import json import logging @@ -46,28 +47,31 @@ class JobType(enum.Enum): Auto = enum.auto() -def find_job_type(github_ctx: Dict[str, Any]) -> Optional[JobType]: - event_name = github_ctx["event_name"] - ref = github_ctx["ref"] - repository = github_ctx["repository"] +@dataclasses.dataclass +class GitHubCtx: + event_name: str + ref: str + repository: str - if event_name == "pull_request": + +def find_job_type(ctx: GitHubCtx) -> Optional[JobType]: + if ctx.event_name == "pull_request": return JobType.PR - elif event_name == "push": + elif ctx.event_name == "push": old_bors_try_build = ( - ref in ("refs/heads/try", "refs/heads/try-perf") and - repository == "rust-lang-ci/rust" + ctx.ref in ("refs/heads/try", "refs/heads/try-perf") and + ctx.repository == "rust-lang-ci/rust" ) new_bors_try_build = ( - ref == "refs/heads/automation/bors/try" and - repository == "rust-lang/rust" + ctx.ref == "refs/heads/automation/bors/try" and + ctx.repository == "rust-lang/rust" ) try_build = old_bors_try_build or new_bors_try_build if try_build: return JobType.Try - if ref == "refs/heads/auto" and repository == "rust-lang-ci/rust": + if ctx.ref == "refs/heads/auto" and ctx.repository == "rust-lang-ci/rust": return JobType.Auto return None @@ -84,13 +88,21 @@ def calculate_jobs(job_type: JobType, job_data: Dict[str, Any]) -> List[Dict[str return [] +def get_github_ctx() -> GitHubCtx: + return GitHubCtx( + event_name=os.environ["GITHUB_EVENT_NAME"], + ref=os.environ["GITHUB_REF"], + repository=os.environ["GITHUB_REPOSITORY"] + ) + + if __name__ == "__main__": logging.basicConfig(level=logging.INFO) with open(JOBS_YAML_PATH) as f: data = yaml.safe_load(f) - github_ctx = json.loads(os.environ["GITHUB_CTX"]) + github_ctx = get_github_ctx() job_type = find_job_type(github_ctx) logging.info(f"Job type: {job_type}") diff --git a/src/ci/github-actions/ci.yml b/src/ci/github-actions/ci.yml index 2a271b43a8a..1efdd7e22e9 100644 --- a/src/ci/github-actions/ci.yml +++ b/src/ci/github-actions/ci.yml @@ -353,8 +353,6 @@ jobs: - name: Checkout the source code uses: actions/checkout@v4 - name: Calculate the CI job matrix - env: - GITHUB_CTX: ${{ toJSON(github) }} run: python3 src/ci/github-actions/calculate-job-matrix.py >> $GITHUB_OUTPUT id: jobs job: