Skip to content

Commit 35b4401

Browse files
committed
Rewrite generate_matrix.py
1 parent c9cdf44 commit 35b4401

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

docs/using/recipe_code/generate_matrix.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,53 @@
33
# Distributed under the terms of the Modified BSD License.
44
import json
55
from pathlib import Path
6-
from typing import Any
76

87
THIS_DIR = Path(__file__).parent.resolve()
98

9+
RUNS_ON = ["ubuntu-24.04", "ubuntu-24.04-arm"]
10+
ARM_INCOMPATIBLE_IMAGES = {"oracledb.dockerfile"}
11+
BASE_IMAGE_PREFIX = "ARG BASE_IMAGE="
1012

11-
def generate_matrix() -> Any:
13+
14+
def extract_base_image(dockerfile: Path) -> str:
15+
"""Extract base image from dockerfile"""
16+
for line in dockerfile.read_text().splitlines():
17+
if line.startswith(BASE_IMAGE_PREFIX):
18+
full_image = line[len(BASE_IMAGE_PREFIX) :]
19+
image_name = full_image[full_image.rfind("/") + 1 :]
20+
return "" if ":" in image_name else image_name
21+
raise RuntimeError(f"Base image not found in {dockerfile}")
22+
23+
24+
def get_platform(runs_on: str) -> str:
25+
"""Get platform architecture based on runner"""
26+
return "x86_64" if runs_on == "ubuntu-24.04" else "aarch64"
27+
28+
29+
def generate_matrix() -> dict[str, list[dict[str, str]]]:
30+
"""Generate build matrix for GitHub Actions"""
1231
dockerfiles = sorted(THIS_DIR.glob("*.dockerfile"))
13-
runs_on = ["ubuntu-24.04", "ubuntu-24.04-arm"]
32+
configurations: list[dict[str, str]] = []
1433

15-
configurations = []
1634
for dockerfile in dockerfiles:
1735
dockerfile_name = dockerfile.name
18-
for run in runs_on:
19-
if dockerfile_name == "oracledb.dockerfile" and run == "ubuntu-24.04-arm":
36+
37+
for run in RUNS_ON:
38+
# Skip ARM builds for incompatible images
39+
if dockerfile_name in ARM_INCOMPATIBLE_IMAGES and run == "ubuntu-24.04-arm":
2040
continue
21-
dockerfile_lines = dockerfile.read_text().splitlines()
22-
base_image = [
23-
line for line in dockerfile_lines if line.startswith("ARG BASE_IMAGE=")
24-
][0][15:]
25-
base_image_short = base_image[base_image.rfind("/") + 1 :]
26-
# Handling a case of `docker.io/jupyter/base-notebook:notebook-6.5.4` image
27-
if ":" in base_image_short:
28-
base_image_short = ""
41+
2942
configurations.append(
3043
{
3144
"dockerfile": dockerfile_name,
3245
"runs-on": run,
33-
"platform": "x86_64" if run == "ubuntu-24.04" else "aarch64",
34-
"parent-image": base_image_short,
46+
"platform": get_platform(run),
47+
"parent-image": extract_base_image(dockerfile),
3548
}
3649
)
50+
3751
return {"include": configurations}
3852

3953

4054
if __name__ == "__main__":
41-
print("matrix=" + json.dumps(generate_matrix()))
55+
print(f"matrix={json.dumps(generate_matrix())}")

0 commit comments

Comments
 (0)