Skip to content

Commit 0293cfb

Browse files
nogatesclaude
andcommitted
Split example builds into parallel matrix jobs to cut CI time ~60%
Replace the single check-examples.sh job (~7min for 1490 examples) with a two-stage workflow: scripts/prepare-examples.sh counts examples and emits a dynamic matrix, then scripts/build-examples-group.sh runs in parallel across 4 groups (~400 examples each, ~2.5min per job). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent a2ba429 commit 0293cfb

4 files changed

Lines changed: 72 additions & 21 deletions

File tree

.github/workflows/reusable-examples.yml

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,26 @@ on:
1010
default: ''
1111

1212
jobs:
13-
examples:
13+
prepare:
1414
runs-on: ubuntu-latest
15+
outputs:
16+
matrix: ${{ steps.split.outputs.matrix }}
17+
steps:
18+
- uses: actions/checkout@v3
19+
with:
20+
repository: DataDog/datadog-api-client-go
21+
ref: ${{ inputs.target-branch || github.ref }}
22+
- name: Compute example groups
23+
id: split
24+
run: ./scripts/prepare-examples.sh
25+
shell: bash
26+
27+
build:
28+
needs: prepare
29+
runs-on: ubuntu-latest
30+
strategy:
31+
matrix:
32+
group: ${{ fromJson(needs.prepare.outputs.matrix) }}
1533
steps:
1634
- uses: actions/checkout@v3
1735
with:
@@ -23,6 +41,6 @@ jobs:
2341
go-version: 1.22.x
2442
cache: true
2543
cache-dependency-path: tests/go.sum
26-
- name: Check examples
27-
run: ./check-examples.sh
28-
shell: bash
44+
- name: Build examples (group ${{ matrix.group }})
45+
run: ./scripts/build-examples-group.sh ${{ matrix.group }}
46+
shell: bash

check-examples.sh

Lines changed: 0 additions & 17 deletions
This file was deleted.

scripts/build-examples-group.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
GROUP=${1:?usage: build-examples-group.sh <group-number>}
5+
EXAMPLES_PER_GROUP=${EXAMPLES_PER_GROUP:-400}
6+
7+
ALL_EXAMPLES=()
8+
while IFS= read -r f; do ALL_EXAMPLES+=("$f"); done < <(find examples -mindepth 3 -maxdepth 3 -name "*.go" | sort)
9+
TOTAL=${#ALL_EXAMPLES[@]}
10+
11+
START=$(( (GROUP - 1) * EXAMPLES_PER_GROUP ))
12+
SLICE=("${ALL_EXAMPLES[@]:$START:$EXAMPLES_PER_GROUP}")
13+
14+
echo "Building group $GROUP: examples $((START + 1))-$((START + ${#SLICE[@]})) of $TOTAL"
15+
16+
for f in "${SLICE[@]}"; do
17+
df="build/$(dirname "$f")/$(basename "$f" .go)"
18+
mkdir -p "$df"
19+
cp "$f" "$df/main.go"
20+
done
21+
22+
if go build -o /dev/null -ldflags "-s -w -linkmode internal" ./build/examples/*/*/*; then
23+
echo "Examples are buildable"
24+
else
25+
echo "Failed to build examples"
26+
exit 1
27+
fi

scripts/prepare-examples.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
EXAMPLES_PER_GROUP=${EXAMPLES_PER_GROUP:-400}
5+
6+
ALL_EXAMPLES=()
7+
while IFS= read -r f; do ALL_EXAMPLES+=("$f"); done < <(find examples -mindepth 3 -maxdepth 3 -name "*.go" | sort)
8+
TOTAL=${#ALL_EXAMPLES[@]}
9+
NUM_GROUPS=$(( (TOTAL + EXAMPLES_PER_GROUP - 1) / EXAMPLES_PER_GROUP ))
10+
11+
echo "Total examples: $TOTAL, groups: $NUM_GROUPS (max $EXAMPLES_PER_GROUP per group)"
12+
13+
MATRIX_JSON="["
14+
for i in $(seq 1 "$NUM_GROUPS"); do
15+
[ "$i" -gt 1 ] && MATRIX_JSON+=","
16+
MATRIX_JSON+="$i"
17+
done
18+
MATRIX_JSON+="]"
19+
20+
echo "matrix=$MATRIX_JSON"
21+
if [ -n "${GITHUB_OUTPUT:-}" ]; then
22+
echo "matrix=$MATRIX_JSON" >> "$GITHUB_OUTPUT"
23+
fi

0 commit comments

Comments
 (0)