@@ -165,7 +165,7 @@ jobs:
165165 needs : release
166166 runs-on : ubuntu-latest
167167 permissions :
168- contents : read
168+ contents : write
169169 pages : write
170170 id-token : write
171171 environment :
@@ -232,6 +232,39 @@ jobs:
232232
233233 - name : Setup Pages
234234 uses : actions/configure-pages@v4
235+ with :
236+ enablement : true
237+
238+ - name : Ensure `gh-pages` branch exists (create orphan branch if missing)
239+ env :
240+ GH_TOKEN : ${{ github.token }}
241+ REPO : ${{ github.repository }}
242+ run : |
243+ set -euo pipefail
244+ echo "Checking for gh-pages branch in $REPO"
245+ if gh api repos/${REPO}/git/ref/heads/gh-pages >/dev/null 2>&1; then
246+ echo "gh-pages branch already exists"
247+ else
248+ echo "gh-pages branch not found — creating orphan gh-pages branch"
249+ git config --global user.name "github-actions[bot]"
250+ git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
251+
252+ # Clone the repository using the GITHUB_TOKEN and create an orphan branch
253+ CLONE_URL="https://x-access-token:${GH_TOKEN}@github.com/${REPO}.git"
254+ rm -rf repo-ghpages || true
255+ git clone --depth 1 "$CLONE_URL" repo-ghpages
256+ cd repo-ghpages
257+
258+ # Create an empty orphan branch and commit a placeholder file
259+ git checkout --orphan gh-pages
260+ git rm -rf . || true
261+ echo "<!-- GitHub Pages placeholder -->" > index.html
262+ git add index.html
263+ git commit -m "chore: create gh-pages orphan branch"
264+ git push --set-upstream origin gh-pages
265+ echo "Created orphan gh-pages branch and pushed to origin"
266+ fi
267+
235268
236269 - name : Upload artifact
237270 uses : actions/upload-pages-artifact@v3
@@ -241,3 +274,16 @@ jobs:
241274 - name : Deploy to GitHub Pages
242275 id : deployment
243276 uses : actions/deploy-pages@v4
277+
278+ - name : Configure Pages site to use `gh-pages` branch
279+ env :
280+ GH_TOKEN : ${{ github.token }}
281+ REPO : ${{ github.repository }}
282+ run : |
283+ set -e
284+ echo "Configuring Pages to use gh-pages branch for $REPO"
285+ # Use the REST API to set Pages source to gh-pages
286+ gh api --method PUT /repos/${REPO}/pages -f source='{"branch":"gh-pages","path":"/"}' || {
287+ echo "Failed to configure Pages via API (may already be configured or require admin rights)." >&2
288+ exit 0
289+ }
0 commit comments