Load GitHub context from environment variables

This commit is contained in:
Jakub Beránek 2024-04-23 08:52:28 +02:00
parent d2059aef27
commit c251abc7ec
No known key found for this signature in database
GPG Key ID: 909CD0D26483516B
3 changed files with 24 additions and 16 deletions

View File

@ -45,8 +45,6 @@ jobs:
- name: Checkout the source code - name: Checkout the source code
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Calculate the CI job matrix - name: Calculate the CI job matrix
env:
GITHUB_CTX: "${{ toJSON(github) }}"
run: python3 src/ci/github-actions/calculate-job-matrix.py >> $GITHUB_OUTPUT run: python3 src/ci/github-actions/calculate-job-matrix.py >> $GITHUB_OUTPUT
id: jobs id: jobs
job: job:

View File

@ -7,6 +7,7 @@ be executed on CI.
It reads job definitions from `src/ci/github-actions/jobs.yml` It reads job definitions from `src/ci/github-actions/jobs.yml`
and filters them based on the event that happened on CI. and filters them based on the event that happened on CI.
""" """
import dataclasses
import enum import enum
import json import json
import logging import logging
@ -46,28 +47,31 @@ class JobType(enum.Enum):
Auto = enum.auto() Auto = enum.auto()
def find_job_type(github_ctx: Dict[str, Any]) -> Optional[JobType]: @dataclasses.dataclass
event_name = github_ctx["event_name"] class GitHubCtx:
ref = github_ctx["ref"] event_name: str
repository = github_ctx["repository"] 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 return JobType.PR
elif event_name == "push": elif ctx.event_name == "push":
old_bors_try_build = ( old_bors_try_build = (
ref in ("refs/heads/try", "refs/heads/try-perf") and ctx.ref in ("refs/heads/try", "refs/heads/try-perf") and
repository == "rust-lang-ci/rust" ctx.repository == "rust-lang-ci/rust"
) )
new_bors_try_build = ( new_bors_try_build = (
ref == "refs/heads/automation/bors/try" and ctx.ref == "refs/heads/automation/bors/try" and
repository == "rust-lang/rust" ctx.repository == "rust-lang/rust"
) )
try_build = old_bors_try_build or new_bors_try_build try_build = old_bors_try_build or new_bors_try_build
if try_build: if try_build:
return JobType.Try 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 JobType.Auto
return None return None
@ -84,13 +88,21 @@ def calculate_jobs(job_type: JobType, job_data: Dict[str, Any]) -> List[Dict[str
return [] 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__": if __name__ == "__main__":
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
with open(JOBS_YAML_PATH) as f: with open(JOBS_YAML_PATH) as f:
data = yaml.safe_load(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) job_type = find_job_type(github_ctx)
logging.info(f"Job type: {job_type}") logging.info(f"Job type: {job_type}")

View File

@ -353,8 +353,6 @@ jobs:
- name: Checkout the source code - name: Checkout the source code
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Calculate the CI job matrix - name: Calculate the CI job matrix
env:
GITHUB_CTX: ${{ toJSON(github) }}
run: python3 src/ci/github-actions/calculate-job-matrix.py >> $GITHUB_OUTPUT run: python3 src/ci/github-actions/calculate-job-matrix.py >> $GITHUB_OUTPUT
id: jobs id: jobs
job: job: