Skip to content

Commit 2ce3752

Browse files
committed
fix(ci): preserve preview directories when deploying main to gh-pages
- Change main deploy to keep_files=true so preview/ is not wiped - Add cleanup step that removes stale root files while preserving preview/ - Fixes 404 on preview URLs after main branch deploys
1 parent 955ef9e commit 2ce3752

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

.github/workflows/nextjs-static-gh-pages.yml

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ jobs:
5555
echo "base_path=/${REPO_NAME}" >> $GITHUB_OUTPUT
5656
fi
5757
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
5961
echo "preview_url=${BASE_URL}/" >> $GITHUB_OUTPUT
6062
else
6163
echo "base_path=/preview/${SAFE_BRANCH}" >> $GITHUB_OUTPUT
@@ -110,6 +112,25 @@ jobs:
110112
pnpm build
111113
touch out/.nojekyll
112114
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+
113134
- name: 🚀 Deploy to GitHub Pages
114135
uses: peaceiris/actions-gh-pages@v4
115136
with:

0 commit comments

Comments
 (0)