Skip to content

Commit 13e044f

Browse files
ci: add guard to fail builds on tracked generated/system files (#163)
* ci: fail build if generated/system junk files are tracked * ci: extract junk file guard into reusable script and improve matching * ci: skip rust compile checks when no rust files changed * ci: cover rust toolchain changes in rust-step gating --------- Co-authored-by: aviralgarg05 <gargaviral99@gmail.com>
1 parent 51db147 commit 13e044f

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

.github/workflows/ci.yml

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,34 @@ jobs:
2626
- name: Checkout
2727
uses: actions/checkout@v4
2828

29+
- name: Fail if junk files are tracked
30+
run: ./scripts/check_junk_files.sh
31+
32+
- name: Detect Rust-relevant changes
33+
id: rust_changes
34+
run: |
35+
if [ "${{ github.event_name }}" != "pull_request" ]; then
36+
echo "changed=true" >> "$GITHUB_OUTPUT"
37+
exit 0
38+
fi
39+
40+
git fetch --no-tags --depth=1 origin "${{ github.base_ref }}"
41+
if git diff --name-only "origin/${{ github.base_ref }}...HEAD" | grep -Eq '(^|/)(Cargo\.toml|Cargo\.lock|rust-toolchain(\.toml)?)$|(^|/)\.cargo/|\.rs$'; then
42+
echo "changed=true" >> "$GITHUB_OUTPUT"
43+
else
44+
echo "changed=false" >> "$GITHUB_OUTPUT"
45+
fi
46+
47+
- name: Skip Rust build/test for non-Rust changes
48+
if: steps.rust_changes.outputs.changed != 'true'
49+
run: echo "No Rust-relevant file changes detected; skipping Rust compile/test steps."
50+
2951
- name: Set up Rust
52+
if: steps.rust_changes.outputs.changed == 'true'
3053
uses: dtolnay/rust-toolchain@stable
3154

3255
- name: Cache cargo
56+
if: steps.rust_changes.outputs.changed == 'true'
3357
uses: actions/cache@v4
3458
with:
3559
path: |
@@ -41,39 +65,47 @@ jobs:
4165
restore-keys: ${{ runner.os }}-cargo-
4266

4367
- name: Install cargo-audit
68+
if: steps.rust_changes.outputs.changed == 'true'
4469
run: |
4570
if ! command -v cargo-audit &> /dev/null; then
4671
cargo install cargo-audit --locked
4772
fi
4873
4974
- name: fmt
75+
if: steps.rust_changes.outputs.changed == 'true'
5076
run: cargo fmt --all -- --check
5177

5278
- name: clippy
79+
if: steps.rust_changes.outputs.changed == 'true'
5380
run: cargo clippy --workspace --all-targets -- -D warnings
5481

5582
- name: tests
83+
if: steps.rust_changes.outputs.changed == 'true'
5684
run: cargo test --workspace -- --test-threads=1
5785

5886
- name: Install cargo-llvm-cov
87+
if: steps.rust_changes.outputs.changed == 'true'
5988
uses: taiki-e/install-action@cargo-llvm-cov
6089

6190
- name: Run tests with coverage
91+
if: steps.rust_changes.outputs.changed == 'true'
6292
run: cargo llvm-cov --workspace --lcov --output-path lcov.info
6393

6494
- name: Upload coverage to Codecov
95+
if: ${{ always() && steps.rust_changes.outputs.changed == 'true' }}
6596
uses: codecov/codecov-action@v4
6697
with:
6798
files: lcov.info
6899
token: ${{ secrets.CODECOV_TOKEN }}
69-
if: always()
70100

71101
- name: Build documentation
102+
if: steps.rust_changes.outputs.changed == 'true'
72103
run: cargo doc --workspace --no-deps
73104
env:
74105
RUSTDOCFLAGS: -D warnings
75106

76107
- name: Security audit
108+
if: steps.rust_changes.outputs.changed == 'true'
77109
run: cargo audit
78110

79111
benchmarks:
@@ -86,6 +118,9 @@ jobs:
86118
with:
87119
fetch-depth: 0
88120

121+
- name: Fail if junk files are tracked
122+
run: ./scripts/check_junk_files.sh
123+
89124
- name: Set up Rust
90125
uses: dtolnay/rust-toolchain@stable
91126

@@ -169,6 +204,9 @@ jobs:
169204
- name: Checkout
170205
uses: actions/checkout@v4
171206

207+
- name: Fail if junk files are tracked
208+
run: ./scripts/check_junk_files.sh
209+
172210
- name: Set up Python
173211
uses: actions/setup-python@v5
174212
with:

scripts/check_junk_files.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
echo "Checking for forbidden tracked files..."
5+
6+
# Exact match patterns
7+
FORBIDDEN_FILES_REGEX='(^|/)\.DS_Store$|(^|/)\.coverage$|(^|/)htmlcov/'
8+
9+
if git ls-files | grep -E "$FORBIDDEN_FILES_REGEX"; then
10+
echo "❌ Forbidden tracked files detected!"
11+
exit 1
12+
fi
13+
14+
echo "✅ Repository clean."

0 commit comments

Comments
 (0)