Skip to content

Commit f81cd9d

Browse files
committed
Auto merge of #124631 - Kobzol:arbitrary-try-build, r=<try>
CI: enable arbitrary try build This PR should enable running arbitrary try builds with ``@bors` try`. So far there is no support for this in bors! You would need to manually add a special line (see below) containing to the PR description (this will later be automated with new bors). ci-job: aarch64-gnu r? `@ghost`
2 parents a8773d5 + 334c6ef commit f81cd9d

File tree

2 files changed

+64
-17
lines changed

2 files changed

+64
-17
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

+62-17
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
and filters them based on the event that happened on CI.
99
"""
1010
import dataclasses
11-
import enum
1211
import json
1312
import logging
1413
import os
14+
import re
15+
import typing
1516
from pathlib import Path
1617
from typing import List, Dict, Any, Optional
1718

@@ -44,22 +45,51 @@ def add_base_env(jobs: List[Job], environment: Dict[str, str]) -> List[Job]:
4445
return jobs
4546

4647

47-
class WorkflowRunType(enum.Enum):
48-
PR = enum.auto()
49-
Try = enum.auto()
50-
Auto = enum.auto()
48+
@dataclasses.dataclass
49+
class PRRunType:
50+
pass
51+
52+
53+
@dataclasses.dataclass
54+
class TryRunType:
55+
job: Optional[str] = None
56+
57+
58+
@dataclasses.dataclass
59+
class AutoRunType:
60+
pass
61+
62+
63+
WorkflowRunType = typing.Union[PRRunType, TryRunType, AutoRunType]
5164

5265

5366
@dataclasses.dataclass
5467
class GitHubCtx:
5568
event_name: str
5669
ref: str
5770
repository: str
71+
commit_message: Optional[str]
72+
73+
74+
def get_job_from_commit(ctx: GitHubCtx) -> Optional[str]:
75+
"""
76+
Tries to parse a name of a CI job 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 None
82+
83+
regex = re.compile(r"ci-job: (.*)")
84+
match = regex.search(ctx.commit_message)
85+
if match is None:
86+
return None
87+
return match.group(1)
5888

5989

6090
def find_run_type(ctx: GitHubCtx) -> Optional[WorkflowRunType]:
6191
if ctx.event_name == "pull_request":
62-
return WorkflowRunType.PR
92+
return PRRunType()
6393
elif ctx.event_name == "push":
6494
old_bors_try_build = (
6595
ctx.ref in ("refs/heads/try", "refs/heads/try-perf") and
@@ -72,20 +102,29 @@ def find_run_type(ctx: GitHubCtx) -> Optional[WorkflowRunType]:
72102
try_build = old_bors_try_build or new_bors_try_build
73103

74104
if try_build:
75-
return WorkflowRunType.Try
105+
job_name = get_job_from_commit(ctx)
106+
return TryRunType(job=job_name)
76107

77108
if ctx.ref == "refs/heads/auto" and ctx.repository == "rust-lang-ci/rust":
78-
return WorkflowRunType.Auto
109+
return AutoRunType()
79110

80111
return None
81112

82113

83114
def calculate_jobs(run_type: WorkflowRunType, job_data: Dict[str, Any]) -> List[Job]:
84-
if run_type == WorkflowRunType.PR:
115+
if isinstance(run_type, PRRunType):
85116
return add_base_env(name_jobs(job_data["pr"], "PR"), job_data["envs"]["pr"])
86-
elif run_type == WorkflowRunType.Try:
87-
return add_base_env(name_jobs(job_data["try"], "try"), job_data["envs"]["try"])
88-
elif run_type == WorkflowRunType.Auto:
117+
elif isinstance(run_type, TryRunType):
118+
jobs = job_data["try"]
119+
if run_type.job is not None:
120+
jobs = [job for job in job_data["auto"] if job["image"] == run_type.job]
121+
if not jobs:
122+
raise Exception(
123+
f"CI job `{run_type.job}` asked for in the try build does not exist"
124+
)
125+
126+
return add_base_env(name_jobs(jobs, "try"), job_data["envs"]["try"])
127+
elif run_type is AutoRunType:
89128
return add_base_env(name_jobs(job_data["auto"], "auto"), job_data["envs"]["auto"])
90129

91130
return []
@@ -99,19 +138,25 @@ def skip_jobs(jobs: List[Dict[str, Any]], channel: str) -> List[Job]:
99138

100139

101140
def get_github_ctx() -> GitHubCtx:
141+
event_name = os.environ["GITHUB_EVENT_NAME"]
142+
143+
commit_message = None
144+
if event_name == "push":
145+
commit_message = os.environ["COMMIT_MESSAGE"]
102146
return GitHubCtx(
103-
event_name=os.environ["GITHUB_EVENT_NAME"],
147+
event_name=event_name,
104148
ref=os.environ["GITHUB_REF"],
105-
repository=os.environ["GITHUB_REPOSITORY"]
149+
repository=os.environ["GITHUB_REPOSITORY"],
150+
commit_message=commit_message
106151
)
107152

108153

109154
def format_run_type(run_type: WorkflowRunType) -> str:
110-
if run_type == WorkflowRunType.PR:
155+
if isinstance(run_type, PRRunType):
111156
return "pr"
112-
elif run_type == WorkflowRunType.Auto:
157+
elif isinstance(run_type, AutoRunType):
113158
return "auto"
114-
elif run_type == WorkflowRunType.Try:
159+
elif isinstance(run_type, TryRunType):
115160
return "try"
116161
else:
117162
raise AssertionError()

0 commit comments

Comments
 (0)