@@ -40,27 +40,78 @@ jobs:
4040 env :
4141 PUBLIC_URL : /hathor-explorer/pr-${{ github.event.pull_request.number }}
4242
43- - name : Deploy to gh-pages
44- # https://github.com/peaceiris/actions-gh-pages/releases/tag/v4.0.0
45- uses : peaceiris/actions-gh-pages@4f9cc6602d3f66b9c108549d231e62369c1b2c85
46- with :
47- github_token : ${{ secrets.GITHUB_TOKEN }}
48- publish_dir : ./build
49- destination_dir : pr-${{ github.event.pull_request.number }}
50- keep_files : true
51-
52- - name : Post preview URL comment
53- # https://github.com/thollander/actions-comment-pull-request/releases/tag/v3.0.1
54- uses : thollander/actions-comment-pull-request@24bffb9b452ba05a4f3f77933840a6a841d1b32b
55- with :
56- comment-tag : pr-preview-url
57- message : |
58- ## Preview deployment
43+ - name : Deploy to gh-pages branch
44+ env :
45+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
46+ PR_NUMBER : ${{ github.event.pull_request.number }}
47+ HEAD_SHA : ${{ github.event.pull_request.head.sha }}
48+ run : |
49+ REPO_URL="https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
50+
51+ # Clone the gh-pages branch if it exists, otherwise create a fresh orphan
52+ if git clone --single-branch --branch gh-pages --depth 1 "$REPO_URL" gh-pages-out 2>/dev/null; then
53+ echo "Cloned existing gh-pages branch"
54+ else
55+ echo "Creating new gh-pages branch"
56+ mkdir gh-pages-out
57+ cd gh-pages-out
58+ git init
59+ git remote add origin "$REPO_URL"
60+ git checkout --orphan gh-pages
61+ cd ..
62+ fi
63+
64+ # Replace the PR subfolder with the fresh build
65+ rm -rf "gh-pages-out/pr-${PR_NUMBER}"
66+ cp -r build/ "gh-pages-out/pr-${PR_NUMBER}/"
67+
68+ cd gh-pages-out
69+ git config user.name "github-actions[bot]"
70+ git config user.email "github-actions[bot]@users.noreply.github.com"
71+ git add "pr-${PR_NUMBER}/"
72+ # Only commit when there are actual changes
73+ git diff --cached --quiet || git commit -m "deploy: preview for PR #${PR_NUMBER} @ ${HEAD_SHA}"
74+ git push origin gh-pages
75+
76+ - name : Post or update PR comment
77+ env :
78+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
79+ PR_NUMBER : ${{ github.event.pull_request.number }}
80+ HEAD_SHA : ${{ github.event.pull_request.head.sha }}
81+ run : |
82+ MARKER="<!-- pr-preview-url -->"
83+ REPO_NAME="${GITHUB_REPOSITORY#*/}"
84+ PREVIEW_URL="https://${GITHUB_REPOSITORY_OWNER}.github.io/${REPO_NAME}/pr-${PR_NUMBER}/"
85+
86+ BODY="${MARKER}
87+ ## Preview deployment
88+
89+ | | |
90+ |---|---|
91+ | **URL** | ${PREVIEW_URL} |
92+ | **Commit** | \`${HEAD_SHA}\` |
93+
94+ > This preview is automatically updated on every push and removed when the PR is closed."
95+
96+ # Find an existing comment that starts with our marker
97+ COMMENT_ID=$(curl -sf \
98+ -H "Authorization: Bearer ${GITHUB_TOKEN}" \
99+ -H "Accept: application/vnd.github+json" \
100+ "https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments?per_page=100" \
101+ | jq -r ".[] | select(.body | startswith(\"${MARKER}\")) | .id" | head -1)
59102
60- | | |
61- |---|---|
62- | **URL** | https://HathorNetwork.github.io/hathor-explorer/pr-${{ github.event.pull_request.number }}/ |
63- | **Commit** | ${{ github.event.pull_request.head.sha }} |
64- | **Updated** | ${{ github.event.pull_request.updated_at }} |
103+ PAYLOAD=$(jq -n --arg body "$BODY" '{"body": $body}')
65104
66- > This preview is automatically updated on every push to this PR and removed when the PR is closed.
105+ if [ -n "$COMMENT_ID" ]; then
106+ curl -sf -X PATCH \
107+ -H "Authorization: Bearer ${GITHUB_TOKEN}" \
108+ -H "Accept: application/vnd.github+json" \
109+ "https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/comments/${COMMENT_ID}" \
110+ -d "$PAYLOAD"
111+ else
112+ curl -sf -X POST \
113+ -H "Authorization: Bearer ${GITHUB_TOKEN}" \
114+ -H "Accept: application/vnd.github+json" \
115+ "https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" \
116+ -d "$PAYLOAD"
117+ fi
0 commit comments