|
3 | 3 | # Distributed under the terms of the Modified BSD License.
|
4 | 4 | import json
|
5 | 5 | from pathlib import Path
|
6 |
| -from typing import Any |
7 | 6 |
|
8 | 7 | THIS_DIR = Path(__file__).parent.resolve()
|
9 | 8 |
|
| 9 | +RUNS_ON = ["ubuntu-24.04", "ubuntu-24.04-arm"] |
| 10 | +ARM_INCOMPATIBLE_IMAGES = {"oracledb.dockerfile"} |
| 11 | +BASE_IMAGE_PREFIX = "ARG BASE_IMAGE=" |
10 | 12 |
|
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""" |
12 | 31 | dockerfiles = sorted(THIS_DIR.glob("*.dockerfile"))
|
13 |
| - runs_on = ["ubuntu-24.04", "ubuntu-24.04-arm"] |
| 32 | + configurations: list[dict[str, str]] = [] |
14 | 33 |
|
15 |
| - configurations = [] |
16 | 34 | for dockerfile in dockerfiles:
|
17 | 35 | 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": |
20 | 40 | 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 | + |
29 | 42 | configurations.append(
|
30 | 43 | {
|
31 | 44 | "dockerfile": dockerfile_name,
|
32 | 45 | "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), |
35 | 48 | }
|
36 | 49 | )
|
| 50 | + |
37 | 51 | return {"include": configurations}
|
38 | 52 |
|
39 | 53 |
|
40 | 54 | if __name__ == "__main__":
|
41 |
| - print("matrix=" + json.dumps(generate_matrix())) |
| 55 | + print(f"matrix={json.dumps(generate_matrix())}") |
0 commit comments