-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-matrix.sh
More file actions
68 lines (60 loc) · 2.34 KB
/
Copy pathbuild-matrix.sh
File metadata and controls
68 lines (60 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env bash
set -euo pipefail
# Builds a dynamic GitHub Actions matrix for run-samples.yml.
# Usage: build-matrix.sh <run_mode> [base_sha]
# "all" runs every test; "changed" only runs tests whose watch_folders have modified files
RUN_MODE="${1:-all}"
BASE_SHA="${2:-}"
# Get JSON metadata for all tests from run-samples.sh --list
chmod +x ./run-samples.sh
TEST_META=$(./run-samples.sh --list)
TOTAL=$(echo "$TEST_META" | jq length)
echo "Run mode: $RUN_MODE | Total tests: $TOTAL"
# Changes to these files affect all tests, so any modification triggers a full run
INFRA_FILES="run-samples.sh Makefile .github/workflows/run-samples.yml .github/scripts/build-matrix.sh pyproject.toml requirements-dev.txt requirements-runtime.txt"
if [[ "$RUN_MODE" == "changed" && -n "$BASE_SHA" ]]; then
# Get list of files changed between base branch and current HEAD
CHANGED=$(git diff --name-only "$BASE_SHA"..HEAD || true)
echo "Changed files:"
echo "$CHANGED"
# Safety net: if any infrastructure file changed, run all tests
RUN_ALL=false
for f in $INFRA_FILES; do
if echo "$CHANGED" | grep -qF "$f"; then
echo "Infra changed: $f -> running all"
RUN_ALL=true && break
fi
done
if [[ "$RUN_ALL" == "true" ]]; then
INDICES=$(seq 0 $((TOTAL-1)))
else
# Match changed files against each test's watch_folders using prefix matching
INDICES=""
for (( i=0; i<TOTAL; i++ )); do
mapfile -t folders < <(echo "$TEST_META" | jq -r ".[$i].watch_folders[]")
for wf in "${folders[@]}"; do
if echo "$CHANGED" | grep -q "^${wf}/"; then
INDICES+=" $i" && break
fi
done
done
INDICES=$(echo "$INDICES" | xargs)
fi
else
# "all" mode: select every test
INDICES=$(seq 0 $((TOTAL-1)))
fi
# Output the matrix JSON for GitHub Actions
if [[ -z "${INDICES:-}" ]]; then
echo "No tests to run."
echo "has_tests=false" >> "$GITHUB_OUTPUT"
echo 'matrix={"include":[]}' >> "$GITHUB_OUTPUT"
else
echo "has_tests=true" >> "$GITHUB_OUTPUT"
# Convert space-separated indices to JSON array, then build the matrix object
IDX_JSON=$(echo "$INDICES" | tr ' ' '\n' | jq -R 'tonumber' | jq -s '.')
MATRIX=$(echo "$TEST_META" | jq -c --argjson idx "$IDX_JSON" \
'{include: [$idx[] as $i | .[$i] | {shard, splits, name}]}')
echo "matrix=$MATRIX" >> "$GITHUB_OUTPUT"
echo "Matrix:" && echo "$MATRIX" | jq .
fi