Skip to content

Commit 13f85eb

Browse files
Copilotcommjoen
andcommitted
Improve GitHub Pages cleanup process and add workflow validation
Co-authored-by: commjoen <[email protected]>
1 parent f672e29 commit 13f85eb

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

.github/workflows/github-pages-preview.yml

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -271,31 +271,49 @@ jobs:
271271
runs-on: ubuntu-latest
272272
if: github.event.action == 'closed'
273273
steps:
274-
- name: Checkout gh-pages branch
274+
- name: Checkout repository
275275
uses: actions/checkout@v4
276276
with:
277-
ref: gh-pages
278277
fetch-depth: 0
279278

280-
- name: Remove PR preview directory
279+
- name: Check for gh-pages branch and clean up
281280
run: |
282281
PR_NUMBER=${{ github.event.number }}
283-
PR_DIR="pr-${PR_NUMBER}"
282+
echo "Cleaning up preview for PR #${PR_NUMBER}..."
284283
285-
if [ -d "$PR_DIR" ]; then
286-
echo "Removing preview directory: $PR_DIR"
287-
rm -rf "$PR_DIR"
284+
# Check if gh-pages branch exists
285+
if git ls-remote --exit-code --heads origin gh-pages >/dev/null 2>&1; then
286+
echo "gh-pages branch exists, proceeding with cleanup..."
288287
289-
# Configure git
290-
git config user.name github-actions[bot]
291-
git config user.email 41898282+github-actions[bot]@users.noreply.github.com
288+
# Checkout gh-pages branch
289+
git checkout gh-pages || {
290+
echo "Failed to checkout gh-pages branch, it may not exist yet"
291+
exit 0
292+
}
292293
293-
# Commit and push the removal
294-
git add .
295-
git commit -m "Clean up preview for closed PR #${PR_NUMBER}" || echo "Nothing to commit"
296-
git push
294+
PR_DIR="pr-${PR_NUMBER}"
295+
296+
if [ -d "$PR_DIR" ]; then
297+
echo "Removing preview directory: $PR_DIR"
298+
rm -rf "$PR_DIR"
299+
300+
# Configure git
301+
git config user.name github-actions[bot]
302+
git config user.email 41898282+github-actions[bot]@users.noreply.github.com
303+
304+
# Commit and push the removal
305+
git add .
306+
if git commit -m "Clean up preview for closed PR #${PR_NUMBER}"; then
307+
git push origin gh-pages
308+
echo "✅ Successfully cleaned up preview for PR #${PR_NUMBER}"
309+
else
310+
echo "Nothing to commit for cleanup"
311+
fi
312+
else
313+
echo "Preview directory $PR_DIR not found, nothing to clean up"
314+
fi
297315
else
298-
echo "Preview directory $PR_DIR not found, nothing to clean up"
316+
echo "gh-pages branch doesn't exist yet, nothing to clean up"
299317
fi
300318
301319
- name: Comment PR cleanup completion

0 commit comments

Comments
 (0)