Skip to content

Commit 92a3052

Browse files
committed
Parse try build CI job name from commit message
1 parent c8e2336 commit 92a3052

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

.github/workflows/ci.yml

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ jobs:
5252
- name: Checkout the source code
5353
uses: actions/checkout@v4
5454
- name: Calculate the CI job matrix
55+
env:
56+
COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
5557
run: python3 src/ci/github-actions/calculate-job-matrix.py >> $GITHUB_OUTPUT
5658
id: jobs
5759
job:

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

+46-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import json
1212
import logging
1313
import os
14+
import re
1415
import typing
1516
from pathlib import Path
1617
from typing import List, Dict, Any, Optional
@@ -67,6 +68,23 @@ class GitHubCtx:
6768
event_name: str
6869
ref: str
6970
repository: str
71+
commit_message: Optional[str]
72+
73+
74+
def get_custom_jobs(ctx: GitHubCtx) -> List[str]:
75+
"""
76+
Tries to parse names of specific CI jobs that should be executed in the form of
77+
ci-job: <job-name>
78+
from the commit message of the passed GitHub context.
79+
"""
80+
if ctx.commit_message is None:
81+
return []
82+
83+
regex = re.compile(r"ci-job: (.*)")
84+
jobs = []
85+
for match in regex.finditer(ctx.commit_message):
86+
jobs.append(match.group(1))
87+
return jobs
7088

7189

7290
def find_run_type(ctx: GitHubCtx) -> Optional[WorkflowRunType]:
@@ -84,7 +102,8 @@ def find_run_type(ctx: GitHubCtx) -> Optional[WorkflowRunType]:
84102
try_build = old_bors_try_build or new_bors_try_build
85103

86104
if try_build:
87-
return TryRunType()
105+
jobs = get_custom_jobs(ctx)
106+
return TryRunType(custom_jobs=jobs)
88107

89108
if ctx.ref == "refs/heads/auto" and ctx.repository == "rust-lang-ci/rust":
90109
return AutoRunType()
@@ -96,8 +115,24 @@ def calculate_jobs(run_type: WorkflowRunType, job_data: Dict[str, Any]) -> List[
96115
if isinstance(run_type, PRRunType):
97116
return add_base_env(name_jobs(job_data["pr"], "PR"), job_data["envs"]["pr"])
98117
elif isinstance(run_type, TryRunType):
99-
return add_base_env(name_jobs(job_data["try"], "try"), job_data["envs"]["try"])
100-
elif isinstance(run_type, AutoRunType):
118+
jobs = job_data["try"]
119+
custom_jobs = run_type.custom_jobs
120+
if custom_jobs:
121+
if len(custom_jobs) > 10:
122+
raise Exception(
123+
f"It is only possible to schedule up to 10 custom jobs,"
124+
f"received {len(custom_jobs)} jobs"
125+
)
126+
127+
jobs = []
128+
for custom_job in custom_jobs:
129+
job = [j for j in job_data["auto"] if j["image"] == custom_job]
130+
if not job:
131+
raise Exception(f"Custom job `{custom_job}` not found in auto jobs")
132+
jobs.append(job[0])
133+
134+
return add_base_env(name_jobs(jobs, "try"), job_data["envs"]["try"])
135+
elif run_type is AutoRunType:
101136
return add_base_env(name_jobs(job_data["auto"], "auto"), job_data["envs"]["auto"])
102137

103138
return []
@@ -111,10 +146,16 @@ def skip_jobs(jobs: List[Dict[str, Any]], channel: str) -> List[Job]:
111146

112147

113148
def get_github_ctx() -> GitHubCtx:
149+
event_name = os.environ["GITHUB_EVENT_NAME"]
150+
151+
commit_message = None
152+
if event_name == "push":
153+
commit_message = os.environ["COMMIT_MESSAGE"]
114154
return GitHubCtx(
115-
event_name=os.environ["GITHUB_EVENT_NAME"],
155+
event_name=event_name,
116156
ref=os.environ["GITHUB_REF"],
117-
repository=os.environ["GITHUB_REPOSITORY"]
157+
repository=os.environ["GITHUB_REPOSITORY"],
158+
commit_message=commit_message
118159
)
119160

120161

0 commit comments

Comments
 (0)