Skip to content

Commit a69bb3d

Browse files
committed
Implement matrix sharding
1 parent e985fa1 commit a69bb3d

File tree

2 files changed

+118
-13
lines changed

2 files changed

+118
-13
lines changed

.github/workflows/linux.yml

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ jobs:
125125
generate-matrix:
126126
runs-on: ubuntu-latest
127127
outputs:
128-
matrix: ${{ steps.set-matrix.outputs.matrix }}
128+
matrix-0: ${{ steps.set-matrix.outputs.matrix-0 }}
129+
matrix-1: ${{ steps.set-matrix.outputs.matrix-1 }}
129130
any_builds: ${{ steps.set-matrix.outputs.any_builds }}
130131
pythonbuild_changed: ${{ steps.changed.outputs.pythonbuild_any_changed }}
131132
steps:
@@ -144,11 +145,19 @@ jobs:
144145
- name: Generate build matrix
145146
id: set-matrix
146147
run: |
147-
uv run ci-matrix.py --platform linux --labels '${{ steps.get-labels.outputs.labels }}' > matrix.json && echo "matrix=$(cat matrix.json)" >> $GITHUB_OUTPUT
148+
uv run ci-matrix.py \
149+
--platform linux \
150+
--labels '${{ steps.get-labels.outputs.labels }}' \
151+
--max-shards 2 \
152+
> matrix.json
153+
154+
echo "matrix-0=$(jq -c '.["0"]' matrix.json)" >> $GITHUB_OUTPUT
155+
echo "matrix-1=$(jq -c '.["1"]' matrix.json)" >> $GITHUB_OUTPUT
156+
148157
# Display the matrix for debugging too
149158
cat matrix.json | jq
150159
151-
if jq -e '.include | length > 0' matrix.json > /dev/null; then
160+
if jq -e '.["0"].include | length > 0' matrix.json > /dev/null; then
152161
# Build matrix has entries
153162
echo "any_builds=true" >> $GITHUB_OUTPUT
154163
else
@@ -163,14 +172,88 @@ jobs:
163172
pythonbuild:
164173
- "src/*.rs"
165174
166-
build:
175+
build-0:
176+
needs:
177+
- generate-matrix
178+
- pythonbuild
179+
- image
180+
runs-on: ${{ matrix.runner }}
181+
strategy:
182+
matrix: ${{ fromJson(needs.generate-matrix.outputs.matrix-0) }}
183+
fail-fast: false
184+
name: ${{ matrix.target_triple }} / ${{ matrix.python }} / ${{ matrix.build_options }}
185+
steps:
186+
- uses: actions/checkout@v4
187+
with:
188+
fetch-depth: 0
189+
190+
- name: Install Python
191+
uses: actions/setup-python@v5
192+
with:
193+
python-version: '3.11'
194+
195+
- name: Download pythonbuild
196+
uses: actions/download-artifact@v4
197+
with:
198+
name: pythonbuild
199+
path: build
200+
201+
- name: Download images
202+
uses: actions/download-artifact@v4
203+
with:
204+
pattern: image-*
205+
path: build
206+
merge-multiple: true
207+
208+
- name: Load Docker Images
209+
run: |
210+
for f in build/image-*.tar.zst; do
211+
echo "decompressing $f"
212+
zstd -d --rm ${f}
213+
done
214+
215+
for f in build/image-*.tar; do
216+
echo "loading $f"
217+
docker load --input $f
218+
done
219+
220+
- name: Build
221+
if: ${{ ! matrix.dry-run }}
222+
run: |
223+
# Do empty target so all generated files are touched.
224+
./build-linux.py --make-target empty
225+
226+
# Touch mtimes of all images so they are newer than autogenerated files above.
227+
touch build/image-*
228+
229+
./build-linux.py --target-triple ${{ matrix.target_triple }} --python cpython-${{ matrix.python }} --options ${{ matrix.build_options }}
230+
231+
- name: Validate Distribution
232+
if: ${{ ! matrix.dry-run }}
233+
run: |
234+
chmod +x build/pythonbuild
235+
236+
if [ "${{ matrix.run }}" == "true" ]; then
237+
EXTRA_ARGS="--run"
238+
fi
239+
240+
build/pythonbuild validate-distribution ${EXTRA_ARGS} dist/*.tar.zst
241+
242+
- name: Upload Distribution
243+
if: ${{ ! matrix.dry-run }}
244+
uses: actions/upload-artifact@v4
245+
with:
246+
name: cpython-${{ matrix.python }}-${{ matrix.target_triple }}-${{ matrix.build_options }}
247+
path: dist/*
248+
249+
build-1:
167250
needs:
168251
- generate-matrix
169252
- pythonbuild
170253
- image
171254
runs-on: ${{ matrix.runner }}
172255
strategy:
173-
matrix: ${{ fromJson(needs.generate-matrix.outputs.matrix) }}
256+
matrix: ${{ fromJson(needs.generate-matrix.outputs.matrix-0) }}
174257
fail-fast: false
175258
name: ${{ matrix.target_triple }} / ${{ matrix.python }} / ${{ matrix.build_options }}
176259
steps:

ci-matrix.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import argparse
1010
import json
11+
import sys
1112
from typing import Any, Optional
1213

1314
import yaml
@@ -16,6 +17,7 @@
1617
CI_TARGETS_YAML = "ci-targets.yaml"
1718
CI_RUNNERS_YAML = "ci-runners.yaml"
1819
CI_EXTRA_SKIP_LABELS = ["documentation"]
20+
CI_MATRIX_SIZE_LIMIT = 256 # The maximum size of a matrix in GitHub Actions
1921

2022

2123
def meets_conditional_version(version: str, min_version: str) -> bool:
@@ -216,6 +218,12 @@ def parse_args() -> argparse.Namespace:
216218
choices=["darwin", "linux", "windows"],
217219
help="Filter matrix entries by platform",
218220
)
221+
parser.add_argument(
222+
"--max-shards",
223+
type=int,
224+
default=0,
225+
help="The maximum number of shards allowed; set to zero to disable ",
226+
)
219227
parser.add_argument(
220228
"--labels",
221229
help="Comma-separated list of labels to filter by (e.g., 'platform:darwin,python:3.13,build:debug'), all must match.",
@@ -246,14 +254,28 @@ def main() -> None:
246254
if runner_config.get("free")
247255
}
248256

249-
matrix = {
250-
"include": generate_matrix_entries(
251-
config,
252-
runners,
253-
args.platform,
254-
labels,
255-
)
256-
}
257+
entries = generate_matrix_entries(
258+
config,
259+
runners,
260+
args.platform,
261+
labels,
262+
)
263+
264+
if args.max_shards:
265+
matrix = {}
266+
shards = (len(entries) // CI_MATRIX_SIZE_LIMIT) + 1
267+
if shards > args.max_shards:
268+
print(f"error: matrix of size {len(entries)} requires {shards} shards, but the maximum is {args.max_shards}; consider increasing `--max-shards`", file=sys.stderr)
269+
sys.exit(1)
270+
for shard in range(args.max_shards):
271+
shard_entries = entries[
272+
shard * CI_MATRIX_SIZE_LIMIT : (shard + 1) * CI_MATRIX_SIZE_LIMIT
273+
]
274+
matrix[str(shard)] = {"include": shard_entries}
275+
else:
276+
if len(entries) > CI_MATRIX_SIZE_LIMIT:
277+
print(f"warning: matrix of size {len(entries)} exceeds limit of {CI_MATRIX_SIZE_LIMIT} but sharding is not enabled; consider setting `--max-shards`", file=sys.stderr)
278+
matrix = {"include": entries}
257279

258280
print(json.dumps(matrix))
259281

0 commit comments

Comments
 (0)