@@ -94,11 +94,10 @@ jobs:
9494 needs : build
9595 runs-on : ubuntu-latest
9696 steps :
97- - name : Checkout gh-pages branch
97+ - name : Checkout repository
9898 uses : actions/checkout@v4
9999 with :
100- ref : gh-pages
101- path : gh-pages
100+ fetch-depth : 0
102101
103102 - name : Download artifact
104103 uses : actions/download-artifact@v4
@@ -108,17 +107,31 @@ jobs:
108107
109108 - name : Deploy PR preview
110109 run : |
111- PR_DIR="gh-pages/pr-${{ github.event.pull_request.number }}"
110+ git config user.name "github-actions[bot]"
111+ git config user.email "github-actions[bot]@users.noreply.github.com"
112+
113+ # Check if gh-pages branch exists
114+ if git ls-remote --heads origin gh-pages | grep -q gh-pages; then
115+ git fetch origin gh-pages
116+ git checkout gh-pages
117+ else
118+ # Create orphan gh-pages branch if it doesn't exist
119+ git checkout --orphan gh-pages
120+ git rm -rf .
121+ echo "# GitHub Pages" > README.md
122+ git add README.md
123+ git commit -m "Initialize gh-pages branch"
124+ fi
125+
126+ # Deploy PR preview
127+ PR_DIR="pr-${{ github.event.pull_request.number }}"
112128 rm -rf "$PR_DIR"
113129 mkdir -p "$PR_DIR"
114130 cp -r dist-demo/* "$PR_DIR/"
115131
116- cd gh-pages
117- git config user.name "github-actions[bot]"
118- git config user.email "github-actions[bot]@users.noreply.github.com"
119132 git add .
120133 git diff --staged --quiet || git commit -m "Deploy PR #${{ github.event.pull_request.number }} preview"
121- git push
134+ git push origin gh-pages
122135
123136 - name : Comment on PR
124137 uses : actions/github-script@v7
@@ -161,19 +174,28 @@ jobs:
161174 if : github.event_name == 'pull_request' && github.event.action == 'closed'
162175 runs-on : ubuntu-latest
163176 steps :
164- - name : Checkout gh-pages branch
177+ - name : Checkout repository
165178 uses : actions/checkout@v4
166179 with :
167- ref : gh-pages
180+ fetch-depth : 0
168181
169182 - name : Remove PR preview
170183 run : |
184+ # Check if gh-pages branch exists
185+ if ! git ls-remote --heads origin gh-pages | grep -q gh-pages; then
186+ echo "gh-pages branch does not exist, nothing to clean up"
187+ exit 0
188+ fi
189+
190+ git config user.name "github-actions[bot]"
191+ git config user.email "github-actions[bot]@users.noreply.github.com"
192+ git fetch origin gh-pages
193+ git checkout gh-pages
194+
171195 PR_DIR="pr-${{ github.event.pull_request.number }}"
172196 if [ -d "$PR_DIR" ]; then
173197 rm -rf "$PR_DIR"
174- git config user.name "github-actions[bot]"
175- git config user.email "github-actions[bot]@users.noreply.github.com"
176198 git add .
177199 git diff --staged --quiet || git commit -m "Remove PR #${{ github.event.pull_request.number }} preview"
178- git push
200+ git push origin gh-pages
179201 fi
0 commit comments