Skip to content

Commit 1fd17ac

Browse files
committed
added comments
1 parent cd67031 commit 1fd17ac

3 files changed

Lines changed: 21 additions & 6 deletions

File tree

.github/scripts/build-matrix.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,26 @@ set -euo pipefail
44
# Builds a dynamic GitHub Actions matrix for run-samples.yml.
55
# Usage: build-matrix.sh <run_mode> [base_sha]
66

7+
# "all" runs every test; "changed" only runs tests whose watch_folders have modified files
78
RUN_MODE="${1:-all}"
89
BASE_SHA="${2:-}"
910

11+
# Get JSON metadata for all tests from run-samples.sh --list
1012
chmod +x ./run-samples.sh
1113
TEST_META=$(./run-samples.sh --list)
1214
TOTAL=$(echo "$TEST_META" | jq length)
1315
echo "Run mode: $RUN_MODE | Total tests: $TOTAL"
1416

17+
# Changes to these files affect all tests, so any modification triggers a full run
1518
INFRA_FILES="run-samples.sh Makefile .github/workflows/run-samples.yml .github/scripts/build-matrix.sh pyproject.toml requirements-dev.txt requirements-runtime.txt"
1619

1720
if [[ "$RUN_MODE" == "changed" && -n "$BASE_SHA" ]]; then
21+
# Get list of files changed between base branch and current HEAD
1822
CHANGED=$(git diff --name-only "$BASE_SHA"..HEAD || true)
1923
echo "Changed files:"
2024
echo "$CHANGED"
2125

22-
# If any infrastructure file changed, run all tests
26+
# Safety net: if any infrastructure file changed, run all tests
2327
RUN_ALL=false
2428
for f in $INFRA_FILES; do
2529
if echo "$CHANGED" | grep -qF "$f"; then
@@ -31,6 +35,7 @@ if [[ "$RUN_MODE" == "changed" && -n "$BASE_SHA" ]]; then
3135
if [[ "$RUN_ALL" == "true" ]]; then
3236
INDICES=$(seq 0 $((TOTAL-1)))
3337
else
38+
# Match changed files against each test's watch_folders using prefix matching
3439
INDICES=""
3540
for (( i=0; i<TOTAL; i++ )); do
3641
mapfile -t folders < <(echo "$TEST_META" | jq -r ".[$i].watch_folders[]")
@@ -43,15 +48,18 @@ if [[ "$RUN_MODE" == "changed" && -n "$BASE_SHA" ]]; then
4348
INDICES=$(echo "$INDICES" | xargs)
4449
fi
4550
else
51+
# "all" mode: select every test
4652
INDICES=$(seq 0 $((TOTAL-1)))
4753
fi
4854

55+
# Output the matrix JSON for GitHub Actions
4956
if [[ -z "${INDICES:-}" ]]; then
5057
echo "No tests to run."
5158
echo "has_tests=false" >> "$GITHUB_OUTPUT"
5259
echo 'matrix={"include":[]}' >> "$GITHUB_OUTPUT"
5360
else
5461
echo "has_tests=true" >> "$GITHUB_OUTPUT"
62+
# Convert space-separated indices to JSON array, then build the matrix object
5563
IDX_JSON=$(echo "$INDICES" | tr ' ' '\n' | jq -R 'tonumber' | jq -s '.')
5664
MATRIX=$(echo "$TEST_META" | jq -c --argjson idx "$IDX_JSON" \
5765
'{include: [$idx[] as $i | .[$i] | {shard, splits, name}]}')

.github/workflows/run-samples.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ env:
2626
DEFAULT_RUN_MODE: changed
2727

2828
jobs:
29+
# Lightweight job that builds the dynamic test matrix for the main test jobs
2930
setup:
3031
name: "Build Test Matrix"
3132
runs-on: ubuntu-latest
@@ -36,18 +37,19 @@ jobs:
3637
- name: Checkout repo
3738
uses: actions/checkout@v4
3839
with:
39-
fetch-depth: 0
40+
fetch-depth: 0 # Full history needed for git diff in "changed" mode
4041

4142
- name: Build dynamic matrix
4243
id: build-matrix
4344
run: |
44-
# Determine run mode and base SHA, then delegate to the build-matrix script
45+
# Pick run mode: manual dispatch uses the dropdown, PRs use the env default
4546
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
4647
RUN_MODE="${{ github.event.inputs.run_mode }}"
4748
else
4849
RUN_MODE="${{ env.DEFAULT_RUN_MODE }}"
4950
fi
5051
52+
# In "changed" mode, determine the base commit to diff against
5153
BASE_SHA=""
5254
if [[ "$RUN_MODE" == "changed" ]]; then
5355
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
@@ -60,10 +62,11 @@ jobs:
6062
chmod +x .github/scripts/build-matrix.sh
6163
.github/scripts/build-matrix.sh "$RUN_MODE" "$BASE_SHA"
6264
65+
# Each test runs in its own job — one matrix entry per test from the setup job
6366
scripts:
6467
name: "Test: ${{ matrix.name }}"
6568
needs: setup
66-
if: needs.setup.outputs.has_tests == 'true'
69+
if: needs.setup.outputs.has_tests == 'true' # Skip entirely when no tests match
6770
environment: AZURE
6871
strategy:
6972
fail-fast: false

run-samples.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ ALL_SAMPLES=("${SAMPLES[@]}" "${TERRAFORM_SAMPLES[@]}" "${BICEP_SAMPLES[@]}")
6060
TOTAL=${#ALL_SAMPLES[@]}
6161

6262
# 2. Handle --list flag: output JSON metadata for CI matrix generation (no tools required)
63+
# Each entry has: shard (1-based index), splits (total count), name, and watch_folders.
64+
# CI uses watch_folders to detect which tests are affected by changed files.
6365
if [[ "${1:-}" == "--list" ]]; then
6466
echo "["
6567
for (( i=0; i<TOTAL; i++ )); do
@@ -126,21 +128,23 @@ else
126128
az account show --query "{Environment:environmentName, Subscription:id}" --output json 2>&1 || echo "[DEBUG] az account show failed"
127129
fi
128130

129-
# 6. Calculate Shard
131+
# 6. Calculate Shard — determines which slice of ALL_SAMPLES to run.
132+
# When SPLITS=TOTAL, each shard runs exactly 1 test (COUNT=1).
130133
SHARD=${1:-1}
131134
SPLITS=${2:-1}
132135

133136
COUNT=$(( TOTAL / SPLITS ))
134137
START=$(( (SHARD - 1) * COUNT ))
135138

139+
# Last shard picks up any remainder from integer division
136140
if [ "$SHARD" -eq "$SPLITS" ]; then
137141
COUNT=$(( TOTAL - START ))
138142
fi
139143

140144
echo "Running samples shard $SHARD of $SPLITS (index $START, count $COUNT)"
141145
echo "Total samples (scripts + terraform + bicep): $TOTAL"
142146

143-
# 7. Run Samples
147+
# 7. Run Samples — deploy each test, then run its validation if defined
144148
for (( i=START; i<START+COUNT; i++ )); do
145149
item="${ALL_SAMPLES[$i]}"
146150
IFS='|' read -r path deploy test <<< "$item"

0 commit comments

Comments
 (0)