11name : Cleanup Storybooks
22
33on :
4- pull_request :
5- types : [closed]
4+ schedule :
5+ - cron : ' 0 8 * * *'
6+ workflow_dispatch :
67
78permissions :
89 contents : write
10+ pull-requests : read
911
1012concurrency :
1113 group : gh-pages-deploy
@@ -24,15 +26,27 @@ jobs:
2426 path : gh-pages
2527 fetch-depth : 1
2628
27- - name : Remove PR directory
29+ - name : Remove storybooks for closed/merged PRs
30+ env :
31+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
32+ REPO : ${{ github.repository }}
2833 run : |
29- PR_DIR="storybooks/pr-${{ github.event.pull_request.number }}"
30- if [ -d "gh-pages/$PR_DIR" ]; then
31- rm -rf "gh-pages/$PR_DIR"
32- echo "Removed $PR_DIR"
33- else
34- echo "Directory $PR_DIR not found, nothing to clean up"
35- fi
34+ set -euo pipefail
35+ REMOVED=0
36+ for dir in gh-pages/storybooks/pr-*/; do
37+ [ -d "$dir" ] || continue
38+ PR_NUMBER=$(basename "$dir" | sed 's/^pr-//')
39+ STATE=$(gh api "repos/${REPO}/pulls/${PR_NUMBER}" --jq '.state')
40+ if [ "$STATE" = "closed" ]; then
41+ rm -rf "$dir"
42+ echo "Removed storybook for PR #${PR_NUMBER} (state: ${STATE})"
43+ REMOVED=$((REMOVED + 1))
44+ else
45+ echo "Keeping storybook for PR #${PR_NUMBER} (state: ${STATE})"
46+ fi
47+ done
48+ echo "Total removed: ${REMOVED}"
49+ echo "REMOVED=${REMOVED}" >> "$GITHUB_ENV"
3650
3751 - name : Checkout main branch for scripts
3852 uses : actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
4357 - name : Regenerate root index.html
4458 run : node main-repo/_scripts/generate-storybook-index.js
4559 env :
46- GH_PAGES_DIR : gh-pages
60+ STORYBOOKS_DIR : gh-pages/storybooks
4761 REPO_NAME : ${{ github.event.repository.name }}
4862 REPO_OWNER : ${{ github.repository_owner }}
4963
@@ -53,10 +67,15 @@ jobs:
5367 git config user.name "github-actions[bot]"
5468 git config user.email "github-actions[bot]@users.noreply.github.com"
5569
70+ if [ -z "$(git status --porcelain)" ]; then
71+ echo "Nothing changed, skipping push"
72+ exit 0
73+ fi
74+
5675 # Create orphan branch (no history) with current content
5776 git checkout --orphan gh-pages-new
5877 git add -A
59- git commit -m "Remove storybooks for closed PR ${{ github.event.pull_request.number }} "
78+ git commit -m "chore: remove storybooks for ${REMOVED} closed PR(s) "
6079
6180 # Replace gh-pages with the new orphan branch
6281 git push --force origin gh-pages-new:gh-pages
0 commit comments