Skip to content

Commit ae0eca1

Browse files
committed
ci: detect rust changes on push events before running rust checks
1 parent 09d6f62 commit ae0eca1

1 file changed

Lines changed: 23 additions & 3 deletions

File tree

.github/workflows/ci.yml

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,33 @@ jobs:
3232
- name: Detect Rust-relevant changes
3333
id: rust_changes
3434
run: |
35-
if [ "${{ github.event_name }}" != "pull_request" ]; then
35+
RUST_FILE_REGEX='(^|/)(Cargo\.toml|Cargo\.lock|rust-toolchain(\.toml)?)$|(^|/)\.cargo/|\.rs$'
36+
37+
if [ "${{ github.event_name }}" = "pull_request" ]; then
38+
git fetch --no-tags --depth=1 origin "${{ github.base_ref }}"
39+
CHANGED_FILES="$(git diff --name-only "origin/${{ github.base_ref }}" HEAD)"
40+
elif [ "${{ github.event_name }}" = "push" ]; then
41+
if [ "${{ github.event.before }}" = "0000000000000000000000000000000000000000" ]; then
42+
echo "changed=true" >> "$GITHUB_OUTPUT"
43+
exit 0
44+
fi
45+
46+
if ! git cat-file -e "${{ github.event.before }}^{commit}" 2>/dev/null; then
47+
git fetch --no-tags --depth=1 origin "${{ github.event.before }}" || true
48+
fi
49+
50+
if ! git cat-file -e "${{ github.event.before }}^{commit}" 2>/dev/null; then
51+
echo "changed=true" >> "$GITHUB_OUTPUT"
52+
exit 0
53+
fi
54+
55+
CHANGED_FILES="$(git diff --name-only "${{ github.event.before }}" "${{ github.sha }}")"
56+
else
3657
echo "changed=true" >> "$GITHUB_OUTPUT"
3758
exit 0
3859
fi
3960
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
61+
if printf '%s\n' "$CHANGED_FILES" | grep -Eq "$RUST_FILE_REGEX"; then
4262
echo "changed=true" >> "$GITHUB_OUTPUT"
4363
else
4464
echo "changed=false" >> "$GITHUB_OUTPUT"

0 commit comments

Comments
 (0)