Skip to content

Commit c586f67

Browse files
committed
ci: improve the efficiency of test jobs
1 parent 7875044 commit c586f67

3 files changed

Lines changed: 109 additions & 19 deletions

File tree

.github/scripts/detect-changes.sh

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
mode="${1:-code}"
5+
base_sha="${2:-${BASE_SHA:-}}"
6+
head_sha="${3:-${HEAD_SHA:-}}"
7+
8+
if [ -z "${GITHUB_OUTPUT:-}" ]; then
9+
echo "GITHUB_OUTPUT is not set." >&2
10+
exit 1
11+
fi
12+
13+
if [ -z "$head_sha" ]; then
14+
head_sha="$(git rev-parse HEAD 2>/dev/null || true)"
15+
fi
16+
17+
if [ -z "$base_sha" ]; then
18+
if ! git rev-parse --verify origin/main >/dev/null 2>&1; then
19+
git fetch --no-tags --depth=1 origin main || true
20+
fi
21+
if git rev-parse --verify origin/main >/dev/null 2>&1 && [ -n "$head_sha" ]; then
22+
base_sha="$(git merge-base origin/main "$head_sha" 2>/dev/null || true)"
23+
fi
24+
fi
25+
26+
if [ -z "$base_sha" ] || [ -z "$head_sha" ]; then
27+
echo "run=true" >> "$GITHUB_OUTPUT"
28+
exit 0
29+
fi
30+
31+
if [ "$base_sha" = "0000000000000000000000000000000000000000" ]; then
32+
echo "run=true" >> "$GITHUB_OUTPUT"
33+
exit 0
34+
fi
35+
36+
if ! git cat-file -e "$base_sha" 2>/dev/null; then
37+
git fetch --no-tags --depth=1 origin "$base_sha" || true
38+
fi
39+
40+
if ! git cat-file -e "$base_sha" 2>/dev/null; then
41+
echo "run=true" >> "$GITHUB_OUTPUT"
42+
exit 0
43+
fi
44+
45+
changed_files=$(git diff --name-only "$base_sha" "$head_sha" || true)
46+
47+
case "$mode" in
48+
code)
49+
pattern='^(src/|tests/|examples/|pyproject.toml$|uv.lock$|Makefile$)'
50+
;;
51+
docs)
52+
pattern='^(docs/|mkdocs.yml$)'
53+
;;
54+
*)
55+
pattern="$mode"
56+
;;
57+
esac
58+
59+
if echo "$changed_files" | grep -Eq "$pattern"; then
60+
echo "run=true" >> "$GITHUB_OUTPUT"
61+
else
62+
echo "run=false" >> "$GITHUB_OUTPUT"
63+
fi

.github/workflows/docs.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,12 @@ on:
77
paths:
88
- "docs/**"
99
- "mkdocs.yml"
10-
workflow_run:
11-
workflows: ["Tests"]
12-
types:
13-
- completed
1410

1511
permissions:
1612
contents: write # This allows pushing to gh-pages
1713

1814
jobs:
1915
deploy_docs:
20-
if: ${{ github.event_name == 'push' || github.event.workflow_run.conclusion == 'success' }}
2116
runs-on: ubuntu-latest
2217
steps:
2318
- name: Checkout repository

.github/workflows/tests.yml

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,8 @@ on:
44
push:
55
branches:
66
- main
7-
paths-ignore:
8-
- "docs/**"
9-
- ".codex/**"
10-
- ".github/ISSUE_TEMPLATE/**"
11-
- ".github/PULL_REQUEST_TEMPLATE/**"
12-
- ".github/codex/**"
13-
- "*.md"
147
pull_request:
158
# All PRs, including stacked PRs
16-
paths-ignore:
17-
- "docs/**"
18-
- ".codex/**"
19-
- ".github/ISSUE_TEMPLATE/**"
20-
- ".github/PULL_REQUEST_TEMPLATE/**"
21-
- ".github/codex/**"
22-
- "*.md"
239

2410
permissions:
2511
contents: read
@@ -33,30 +19,49 @@ jobs:
3319
steps:
3420
- name: Checkout repository
3521
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8
22+
- name: Detect code changes
23+
id: changes
24+
run: ./.github/scripts/detect-changes.sh code "${{ github.event.pull_request.base.sha || github.event.before }}" "${{ github.sha }}"
3625
- name: Setup uv
26+
if: steps.changes.outputs.run == 'true'
3727
uses: astral-sh/setup-uv@d4b2f3b6ecc6e67c4457f6d3e41ec42d3d0fcb86
3828
with:
3929
enable-cache: true
4030
- name: Install dependencies
31+
if: steps.changes.outputs.run == 'true'
4132
run: make sync
4233
- name: Verify formatting
34+
if: steps.changes.outputs.run == 'true'
4335
run: make format-check
4436
- name: Run lint
37+
if: steps.changes.outputs.run == 'true'
4538
run: make lint
39+
- name: Skip lint
40+
if: steps.changes.outputs.run != 'true'
41+
run: echo "Skipping lint for non-code changes."
4642

4743
typecheck:
4844
runs-on: ubuntu-latest
4945
steps:
5046
- name: Checkout repository
5147
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8
48+
- name: Detect code changes
49+
id: changes
50+
run: ./.github/scripts/detect-changes.sh code "${{ github.event.pull_request.base.sha || github.event.before }}" "${{ github.sha }}"
5251
- name: Setup uv
52+
if: steps.changes.outputs.run == 'true'
5353
uses: astral-sh/setup-uv@d4b2f3b6ecc6e67c4457f6d3e41ec42d3d0fcb86
5454
with:
5555
enable-cache: true
5656
- name: Install dependencies
57+
if: steps.changes.outputs.run == 'true'
5758
run: make sync
5859
- name: Run typecheck
60+
if: steps.changes.outputs.run == 'true'
5961
run: make mypy
62+
- name: Skip typecheck
63+
if: steps.changes.outputs.run != 'true'
64+
run: echo "Skipping typecheck for non-code changes."
6065

6166
tests:
6267
runs-on: ubuntu-latest
@@ -74,15 +79,24 @@ jobs:
7479
steps:
7580
- name: Checkout repository
7681
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8
82+
- name: Detect code changes
83+
id: changes
84+
run: ./.github/scripts/detect-changes.sh code "${{ github.event.pull_request.base.sha || github.event.before }}" "${{ github.sha }}"
7785
- name: Setup uv
86+
if: steps.changes.outputs.run == 'true'
7887
uses: astral-sh/setup-uv@d4b2f3b6ecc6e67c4457f6d3e41ec42d3d0fcb86
7988
with:
8089
enable-cache: true
8190
python-version: ${{ matrix.python-version }}
8291
- name: Install dependencies
92+
if: steps.changes.outputs.run == 'true'
8393
run: make sync
8494
- name: Run tests with coverage
95+
if: steps.changes.outputs.run == 'true'
8596
run: make coverage
97+
- name: Skip tests
98+
if: steps.changes.outputs.run != 'true'
99+
run: echo "Skipping tests for non-code changes."
86100

87101
build-docs:
88102
runs-on: ubuntu-latest
@@ -91,14 +105,23 @@ jobs:
91105
steps:
92106
- name: Checkout repository
93107
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8
108+
- name: Detect docs changes
109+
id: changes
110+
run: ./.github/scripts/detect-changes.sh docs "${{ github.event.pull_request.base.sha || github.event.before }}" "${{ github.sha }}"
94111
- name: Setup uv
112+
if: steps.changes.outputs.run == 'true'
95113
uses: astral-sh/setup-uv@d4b2f3b6ecc6e67c4457f6d3e41ec42d3d0fcb86
96114
with:
97115
enable-cache: true
98116
- name: Install dependencies
117+
if: steps.changes.outputs.run == 'true'
99118
run: make sync
100119
- name: Build docs
120+
if: steps.changes.outputs.run == 'true'
101121
run: make build-docs
122+
- name: Skip docs build
123+
if: steps.changes.outputs.run != 'true'
124+
run: echo "Skipping docs build for non-docs changes."
102125

103126
old_version_tests:
104127
runs-on: ubuntu-latest
@@ -107,11 +130,20 @@ jobs:
107130
steps:
108131
- name: Checkout repository
109132
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8
133+
- name: Detect code changes
134+
id: changes
135+
run: ./.github/scripts/detect-changes.sh code "${{ github.event.pull_request.base.sha || github.event.before }}" "${{ github.sha }}"
110136
- name: Setup uv
137+
if: steps.changes.outputs.run == 'true'
111138
uses: astral-sh/setup-uv@d4b2f3b6ecc6e67c4457f6d3e41ec42d3d0fcb86
112139
with:
113140
enable-cache: true
114141
- name: Install dependencies
142+
if: steps.changes.outputs.run == 'true'
115143
run: make sync
116144
- name: Run tests
145+
if: steps.changes.outputs.run == 'true'
117146
run: make old_version_tests
147+
- name: Skip old version tests
148+
if: steps.changes.outputs.run != 'true'
149+
run: echo "Skipping old version tests for non-code changes."

0 commit comments

Comments
 (0)