Skip to content

Commit 1b4c939

Browse files
committed
Add support for a list of builders per platform
1 parent 77254c0 commit 1b4c939

File tree

8 files changed

+114
-2
lines changed

8 files changed

+114
-2
lines changed

buildrunner/config/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class GlobalConfig(BaseModel, extra="forbid"):
124124
build_registry: Optional[str] = Field(
125125
alias="build-registry", default=MP_LOCAL_REGISTRY
126126
)
127-
platform_builders: Optional[Dict[str, str]] = Field(
127+
platform_builders: Optional[Dict[str, str] | Dict[str, List[str]]] = Field(
128128
alias="platform-builders", default=None
129129
)
130130
security_scan: GlobalSecurityScanConfig = Field(

buildrunner/docker/multiplatform_image_builder.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import logging
99
import os
1010
import platform as python_platform
11+
import random
1112
import re
1213
import shutil
1314
import tempfile
@@ -311,9 +312,23 @@ def _build_single_image(
311312
f"'{dockerfile}' ({os.path.exists(dockerfile)}) does not exist!"
312313
)
313314

314-
builder = (
315+
# Get the builder for the platform
316+
builders = (
315317
self._platform_builders.get(platform) if self._platform_builders else None
316318
)
319+
builder = None
320+
321+
if builders:
322+
if isinstance(builders, str):
323+
builder = builders
324+
elif isinstance(builders, list):
325+
# Randomly select a builder from the list
326+
builder = random.choice(builders)
327+
else:
328+
raise BuildRunnerConfigurationError(
329+
f"Invalid platform builders configuration for platform {platform}"
330+
)
331+
317332
LOGGER.debug(f"Building image {image_ref} for platform {platform}")
318333
LOGGER.info(
319334
f"Building image for platform {platform} with {builder or 'default'} builder"

examples/build/builders/README.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
==================
2+
Builders Example
3+
==================
4+
5+
This example demonstrates how to use Buildrunner with custom builders.
6+
7+
How to Run
8+
==========
9+
10+
1. **Create builders**
11+
12+
From the base directory, run:
13+
14+
.. code-block:: sh
15+
16+
python examples/build/builders/create_builders.sh
17+
18+
2. **Run build with example configuration file**
19+
20+
From the base directory, run the following command:
21+
22+
.. code-block:: sh
23+
24+
./run-buildrunner.sh -f examples/build/builders/buildrunner.yaml -c examples/build/builders/global-config.yaml
25+
26+
or
27+
28+
.. code-block:: sh
29+
30+
./run-buildrunner.sh -f examples/build/builders/buildrunner.yaml -c examples/build/builders/global-config-list.yaml
31+
32+
33+
3. **Remove builders**
34+
35+
From the base directory, run:
36+
37+
.. code-block:: sh
38+
39+
python examples/build/builders/remove_builders.sh
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Example of a buildrunner.yaml file that specifies a multi-platform build step in order to test the builders.
2+
3+
# Execute using:
4+
# ./run-buildrunner.sh -f examples/build/builders/buildrunner.yaml -c examples/build/builders/global-config-list.yaml
5+
6+
steps:
7+
multi-platform-build-step:
8+
build:
9+
dockerfile: |
10+
FROM alpine:latest
11+
LABEL custom_label="Buildrunner example label"
12+
platforms:
13+
- linux/arm/v6
14+
- linux/arm/v7
15+
- linux/arm/v8
16+
- linux/arm64
17+
- linux/arm64/v8
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
docker buildx create --name builder1 --driver docker-container --bootstrap
4+
docker buildx create --name builder2 --driver docker-container --bootstrap
5+
docker buildx create --name builder3 --driver docker-container --bootstrap
6+
7+
docker buildx ls --debug
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This is a global configuration file that lists the builders for each platform.
2+
platform-builders:
3+
linux/arm/v6:
4+
- builder1
5+
- builder2
6+
- builder3
7+
linux/arm/v7:
8+
- builder1
9+
- builder2
10+
- builder3
11+
linux/arm/v8:
12+
- builder1
13+
- builder2
14+
- builder3
15+
linux/arm64:
16+
- builder1
17+
- builder2
18+
- builder3
19+
linux/arm64/v8:
20+
- builder1
21+
- builder2
22+
- builder3
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
platform-builders:
3+
linux/arm/v6: builder1
4+
linux/arm/v7: builder2
5+
linux/arm/v8: builder3
6+
linux/arm64: builder1
7+
linux/arm64/v8: builder2
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
docker buildx rm builder1
4+
docker buildx rm builder2
5+
docker buildx rm builder3

0 commit comments

Comments
 (0)