Skip to content

Commit c251abc

Browse files
committed
Load GitHub context from environment variables
1 parent d2059ae commit c251abc

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ jobs:
4545
- name: Checkout the source code
4646
uses: actions/checkout@v4
4747
- name: Calculate the CI job matrix
48-
env:
49-
GITHUB_CTX: "${{ toJSON(github) }}"
5048
run: python3 src/ci/github-actions/calculate-job-matrix.py >> $GITHUB_OUTPUT
5149
id: jobs
5250
job:

src/ci/github-actions/calculate-job-matrix.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
It reads job definitions from `src/ci/github-actions/jobs.yml`
88
and filters them based on the event that happened on CI.
99
"""
10+
import dataclasses
1011
import enum
1112
import json
1213
import logging
@@ -46,28 +47,31 @@ class JobType(enum.Enum):
4647
Auto = enum.auto()
4748

4849

49-
def find_job_type(github_ctx: Dict[str, Any]) -> Optional[JobType]:
50-
event_name = github_ctx["event_name"]
51-
ref = github_ctx["ref"]
52-
repository = github_ctx["repository"]
50+
@dataclasses.dataclass
51+
class GitHubCtx:
52+
event_name: str
53+
ref: str
54+
repository: str
5355

54-
if event_name == "pull_request":
56+
57+
def find_job_type(ctx: GitHubCtx) -> Optional[JobType]:
58+
if ctx.event_name == "pull_request":
5559
return JobType.PR
56-
elif event_name == "push":
60+
elif ctx.event_name == "push":
5761
old_bors_try_build = (
58-
ref in ("refs/heads/try", "refs/heads/try-perf") and
59-
repository == "rust-lang-ci/rust"
62+
ctx.ref in ("refs/heads/try", "refs/heads/try-perf") and
63+
ctx.repository == "rust-lang-ci/rust"
6064
)
6165
new_bors_try_build = (
62-
ref == "refs/heads/automation/bors/try" and
63-
repository == "rust-lang/rust"
66+
ctx.ref == "refs/heads/automation/bors/try" and
67+
ctx.repository == "rust-lang/rust"
6468
)
6569
try_build = old_bors_try_build or new_bors_try_build
6670

6771
if try_build:
6872
return JobType.Try
6973

70-
if ref == "refs/heads/auto" and repository == "rust-lang-ci/rust":
74+
if ctx.ref == "refs/heads/auto" and ctx.repository == "rust-lang-ci/rust":
7175
return JobType.Auto
7276

7377
return None
@@ -84,13 +88,21 @@ def calculate_jobs(job_type: JobType, job_data: Dict[str, Any]) -> List[Dict[str
8488
return []
8589

8690

91+
def get_github_ctx() -> GitHubCtx:
92+
return GitHubCtx(
93+
event_name=os.environ["GITHUB_EVENT_NAME"],
94+
ref=os.environ["GITHUB_REF"],
95+
repository=os.environ["GITHUB_REPOSITORY"]
96+
)
97+
98+
8799
if __name__ == "__main__":
88100
logging.basicConfig(level=logging.INFO)
89101

90102
with open(JOBS_YAML_PATH) as f:
91103
data = yaml.safe_load(f)
92104

93-
github_ctx = json.loads(os.environ["GITHUB_CTX"])
105+
github_ctx = get_github_ctx()
94106

95107
job_type = find_job_type(github_ctx)
96108
logging.info(f"Job type: {job_type}")

src/ci/github-actions/ci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,6 @@ jobs:
353353
- name: Checkout the source code
354354
uses: actions/checkout@v4
355355
- name: Calculate the CI job matrix
356-
env:
357-
GITHUB_CTX: ${{ toJSON(github) }}
358356
run: python3 src/ci/github-actions/calculate-job-matrix.py >> $GITHUB_OUTPUT
359357
id: jobs
360358
job:

0 commit comments

Comments
 (0)