|
55 | 55 | echo "base_path=/${REPO_NAME}" >> $GITHUB_OUTPUT |
56 | 56 | fi |
57 | 57 | echo "dest_dir=" >> $GITHUB_OUTPUT |
58 | | - echo "keep_files=false" >> $GITHUB_OUTPUT |
| 58 | + # keep_files must be true so preview/ directories are preserved |
| 59 | + echo "keep_files=true" >> $GITHUB_OUTPUT |
| 60 | + echo "is_main=true" >> $GITHUB_OUTPUT |
59 | 61 | echo "preview_url=${BASE_URL}/" >> $GITHUB_OUTPUT |
60 | 62 | else |
61 | 63 | echo "base_path=/preview/${SAFE_BRANCH}" >> $GITHUB_OUTPUT |
@@ -110,6 +112,25 @@ jobs: |
110 | 112 | pnpm build |
111 | 113 | touch out/.nojekyll |
112 | 114 |
|
| 115 | + - name: 🧹 Clean stale root files from gh-pages (main only) |
| 116 | + if: steps.vars.outputs.is_main == 'true' |
| 117 | + run: | |
| 118 | + # Checkout the gh-pages branch into a temp directory |
| 119 | + git fetch origin gh-pages || true |
| 120 | + mkdir -p /tmp/gh-pages-current |
| 121 | + cd /tmp/gh-pages-current |
| 122 | + git init |
| 123 | + git remote add origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git |
| 124 | + git fetch origin gh-pages --depth=1 || exit 0 |
| 125 | + git checkout gh-pages || exit 0 |
| 126 | +
|
| 127 | + # Delete everything EXCEPT the preview/ directory and CNAME |
| 128 | + find . -maxdepth 1 ! -name '.' ! -name '.git' ! -name 'preview' ! -name 'CNAME' -exec rm -rf {} + |
| 129 | +
|
| 130 | + git add -A |
| 131 | + git diff --cached --quiet || git -c user.name="github-actions" -c user.email="github-actions@github.com" commit -m "clean stale root files before main deploy" |
| 132 | + git push origin gh-pages || true |
| 133 | +
|
113 | 134 | - name: 🚀 Deploy to GitHub Pages |
114 | 135 | uses: peaceiris/actions-gh-pages@v4 |
115 | 136 | with: |
|
0 commit comments