Skip to content

Commit d229b70

Browse files
committed
ci(workflow): optimise CI runs using path filtering
Previously, the CI workflow ran all Rust checks (fmt, clippy, test, build) on every push or pull request to the main branch, regardless of which files were changed. This led to unnecessary resource consumption when only documentation or CI configuration files were modified. This change introduces path filtering using the `tj-actions/changed-files` action. The workflow now identifies changes to Rust source files (`src/`, `tests/`) or manifest files (`Cargo.toml`, `Cargo.lock`). The `cargo fmt`, `cargo clippy`, `cargo test`, and `cargo build` steps are now configured with `if` conditions to run only when relevant Rust code or manifest files have been modified in the push or pull request. This significantly speeds up CI runs for non-code changes. Requires `pull-requests: read` permission for `tj-actions/changed-files` and `fetch-depth: 0` during checkout to allow the action to compare file changes accurately.
1 parent 701d24b commit d229b70

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

.github/workflows/ci.yml

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

99
permissions:
1010
contents: read
11+
pull-requests: read
1112

1213
env:
1314
CARGO_TERM_COLOR: always
@@ -23,25 +24,65 @@ jobs:
2324
steps:
2425
- name: Checkout repository
2526
uses: actions/checkout@v4
27+
# Fetch depth 0 is needed for changed-files to correctly compare PR base/head or push history
28+
with:
29+
fetch-depth: 0
30+
31+
- name: Get changed files
32+
id: changed_files
33+
uses: tj-actions/changed-files@v44
34+
with:
35+
# Define file sets. Output will be like steps.changed_files.outputs.any_code_changed == 'true'
36+
files: |
37+
rust_code:
38+
- src/**/*.rs
39+
- tests/**/*.rs
40+
manifest:
41+
- Cargo.toml
42+
- Cargo.lock
43+
# Combined group for convenience
44+
any_code:
45+
- src/**/*.rs
46+
- tests/**/*.rs
47+
- Cargo.toml
48+
- Cargo.lock
49+
ci:
50+
- .github/workflows/**
51+
- .pre-commit-config.yaml
52+
docs:
53+
- README.md
54+
- COMMIT.md
55+
- LICENSE
2656
2757
- name: Install Rust toolchain (stable)
2858
uses: actions-rust-lang/setup-rust-toolchain@v1
2959
with:
3060
toolchain: stable
3161
cache: 'cargo'
3262
components: clippy, rustfmt
63+
# This step always runs, as caching makes it fast, and subsequent steps might need it.
3364

3465
- name: Check Formatting (cargo fmt)
66+
# Only run if Rust code or manifest files changed
67+
if: steps.changed_files.outputs.any_code_changed == 'true'
3568
run: cargo fmt --all -- --check
3669

3770
- name: Linting (cargo clippy)
71+
if: steps.changed_files.outputs.any_code_changed == 'true'
3872
run: cargo clippy --all-targets -- -D warnings
3973

4074
- name: Run Tests (cargo test)
75+
if: steps.changed_files.outputs.any_code_changed == 'true'
4176
run: cargo test --all-targets --verbose
4277

4378
- name: Build (Debug)
79+
if: steps.changed_files.outputs.any_code_changed == 'true'
4480
run: cargo build --verbose
4581

4682
- name: Build (Release)
83+
if: steps.changed_files.outputs.any_code_changed == 'true'
4784
run: cargo build --release --verbose
85+
86+
- name: Indicate checks skipped (if applicable)
87+
if: steps.changed_files.outputs.any_code_changed == 'false'
88+
run: echo "Rust checks skipped as no relevant code changes were detected."

0 commit comments

Comments
 (0)