@@ -32,9 +32,12 @@ PUBLISH_DIR="$(cd "$PUBLISH_DIR" && pwd)"
3232: " ${GITHUB_TOKEN:? GITHUB_TOKEN is required (set by GitHub Actions)} "
3333: " ${GITHUB_REPOSITORY:? GITHUB_REPOSITORY is required (set by GitHub Actions)} "
3434
35- MAX_RETRIES=3
36- RETRY_DELAYS=(0 27 133)
35+ # ── Diagnostics: log PUBLISH_DIR contents before deploying ──
36+ echo " PUBLISH_DIR: $PUBLISH_DIR "
37+ echo " Top-level entries in PUBLISH_DIR:"
38+ find " $PUBLISH_DIR " -maxdepth 1 -not -path " $PUBLISH_DIR " -printf ' %f\n'
3739
40+ MAX_RETRIES=3
3841DEPLOY_DIR=" $( mktemp -d) "
3942trap ' rm -rf "$DEPLOY_DIR"' EXIT
4043
@@ -44,43 +47,52 @@ git config user.name "github-actions[bot]"
4447git config user.email " github-actions[bot]@users.noreply.github.com"
4548git remote add origin " https://x-access-token:${GITHUB_TOKEN} @github.com/${GITHUB_REPOSITORY} .git"
4649
50+ # ── Fetch gh-pages and prepare working tree ──
51+ fetch_output=$( git fetch origin gh-pages --depth=1 2>&1 ) && fetch_ok=true || fetch_ok=false
52+ if [[ " $fetch_ok " == " true" ]]; then
53+ git checkout -B gh-pages FETCH_HEAD
54+ elif echo " $fetch_output " | grep -qi " not found\|couldn't find\|no such remote ref" ; then
55+ echo " gh-pages branch does not exist, creating orphan"
56+ git checkout --orphan gh-pages
57+ git rm -rf . 2> /dev/null || true
58+ else
59+ echo " ERROR: Failed to fetch gh-pages: $fetch_output " >&2
60+ exit 1
61+ fi
62+
63+ # ── Copy content and stage ──
64+ cp -a " $PUBLISH_DIR " /. .
65+
66+ # Force-add only the files from PUBLISH_DIR (bypasses .gitignore)
67+ publish_entries=()
68+ while IFS= read -r entry; do
69+ publish_entries+=(" $entry " )
70+ done < <( find " $PUBLISH_DIR " -maxdepth 1 -not -path " $PUBLISH_DIR " -printf ' %f\n' )
71+
72+ echo " Staging ${# publish_entries[@]} entries from PUBLISH_DIR..."
73+ git add --force -- " ${publish_entries[@]} "
74+
75+ if git diff --cached --quiet; then
76+ echo " No changes to deploy"
77+ exit 0
78+ fi
79+
80+ echo " Staged files:"
81+ git diff --cached --stat
82+
83+ git commit -q -m " $COMMIT_MSG "
84+
85+ # ── Push with pull-before-push retry ──
4786for attempt in $( seq 1 " $MAX_RETRIES " ) ; do
48- echo " Deploy attempt $attempt /$MAX_RETRIES "
49-
50- fetch_output=$( git fetch origin gh-pages --depth=1 2>&1 ) && fetch_ok=true || fetch_ok=false
51- if [[ " $fetch_ok " == " true" ]]; then
52- git checkout -B gh-pages FETCH_HEAD
53- elif echo " $fetch_output " | grep -qi " not found\|couldn't find\|no such remote ref" ; then
54- echo " gh-pages branch does not exist, creating orphan"
55- git checkout --orphan gh-pages
56- git rm -rf . 2> /dev/null || true
57- else
58- echo " ERROR: Failed to fetch gh-pages: $fetch_output " >&2
59- exit 1
60- fi
61-
62- cp -a " $PUBLISH_DIR " /. .
63- git add -A
64-
65- if git diff --cached --quiet; then
66- echo " No changes to deploy"
67- exit 0
68- fi
69-
70- git commit -q -m " $COMMIT_MSG "
71-
7287 if git push origin gh-pages; then
7388 echo " Deployed successfully (attempt $attempt )"
7489 exit 0
7590 fi
7691
7792 echo " Push rejected (attempt $attempt /$MAX_RETRIES )"
78-
7993 if [[ $attempt -lt $MAX_RETRIES ]]; then
80- jitter=$(( RANDOM % 10 ))
81- wait=$(( RETRY_DELAYS[attempt] + jitter ))
82- echo " Retrying in ${wait} s..."
83- sleep " $wait "
94+ echo " Pulling remote changes before retrying..."
95+ git pull --rebase origin gh-pages
8496 fi
8597done
8698
0 commit comments