Skip to content

Commit 5bc6e5f

Browse files
committed
Fix GitHub Actions workflow commit comparison errors
- Add proper error handling for missing commit SHAs - Handle force pushes and shallow clones gracefully - Check commit existence before attempting git diff operations - Fallback to treating all examples as changed when commits are inaccessible - Improve error messages for debugging workflow issues
1 parent c6da20d commit 5bc6e5f

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

.github/workflows/generate-examples.yml

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,21 @@ jobs:
7575
AFTER_SHA="${{ github.sha }}"
7676
echo "Comparing push: $BEFORE_SHA..$AFTER_SHA"
7777
78-
# Check if any monitored paths have changes
79-
for path in "${PATHS[@]}"; do
80-
if git diff --name-only "$BEFORE_SHA..$AFTER_SHA" | grep -E "^${path//\*/.*}" > /dev/null; then
81-
echo "Changes detected in: $path"
82-
EXAMPLES_CHANGED="true"
83-
break
84-
fi
85-
done
78+
# Check if both commits exist before trying to diff
79+
if git cat-file -e "$BEFORE_SHA" 2>/dev/null && git cat-file -e "$AFTER_SHA" 2>/dev/null; then
80+
# Check if any monitored paths have changes
81+
for path in "${PATHS[@]}"; do
82+
if git diff --name-only "$BEFORE_SHA..$AFTER_SHA" | grep -E "^${path//\*/.*}" > /dev/null; then
83+
echo "Changes detected in: $path"
84+
EXAMPLES_CHANGED="true"
85+
break
86+
fi
87+
done
88+
else
89+
echo "⚠️ Previous commit $BEFORE_SHA not accessible (likely force push or shallow clone)"
90+
echo "Treating as changed to ensure examples are updated"
91+
EXAMPLES_CHANGED="true"
92+
fi
8693
else
8794
# First commit or force push, consider as changed
8895
echo "First commit or force push detected"
@@ -127,6 +134,8 @@ jobs:
127134

128135
steps:
129136
- uses: actions/checkout@v4
137+
with:
138+
fetch-depth: 0 # Fetch full history to ensure we have all commits for comparison
130139

131140
- name: Log build reason
132141
run: |
@@ -182,13 +191,33 @@ jobs:
182191
# For PRs, compare against the base branch
183192
base_sha="${{ github.event.pull_request.base.sha }}"
184193
head_sha="${{ github.event.pull_request.head.sha }}"
185-
changed_files=$(git diff --name-only $base_sha...$head_sha | grep '^examples/.*\.py$' || true)
194+
echo "Comparing PR: $base_sha..$head_sha"
195+
196+
# Check if both commits exist before trying to diff
197+
if git cat-file -e "$base_sha" 2>/dev/null && git cat-file -e "$head_sha" 2>/dev/null; then
198+
changed_files=$(git diff --name-only $base_sha...$head_sha | grep '^examples/.*\.py$' || true)
199+
else
200+
echo "⚠️ PR base or head commit not accessible, treating all examples as changed"
201+
changed_files=$(find examples -name "*.py" -type f)
202+
fi
186203
else
187204
# For pushes, compare against previous commit
188205
if [ "${{ github.event.before }}" != "0000000000000000000000000000000000000000" ]; then
189-
changed_files=$(git diff --name-only ${{ github.event.before }}..${{ github.sha }} | grep '^examples/.*\.py$' || true)
206+
before_sha="${{ github.event.before }}"
207+
after_sha="${{ github.sha }}"
208+
echo "Comparing push: $before_sha..$after_sha"
209+
210+
# Check if both commits exist before trying to diff
211+
if git cat-file -e "$before_sha" 2>/dev/null && git cat-file -e "$after_sha" 2>/dev/null; then
212+
changed_files=$(git diff --name-only "$before_sha..$after_sha" | grep '^examples/.*\.py$' || true)
213+
else
214+
echo "⚠️ Previous commit $before_sha not accessible (likely force push or shallow clone)"
215+
echo "Treating all example files as changed to ensure consistency"
216+
changed_files=$(find examples -name "*.py" -type f)
217+
fi
190218
else
191219
# First commit or force push, consider all files changed
220+
echo "First commit detected, treating all example files as changed"
192221
changed_files=$(find examples -name "*.py" -type f)
193222
fi
194223
fi

0 commit comments

Comments
 (0)