Skip to content

Commit 23c6944

Browse files
committed
ci: replace third-party actions with git and curl
Remove peaceiris/actions-gh-pages and thollander/actions-comment-pull-request. Equivalent functionality now implemented using: - plain git commands to push to the gh-pages branch - curl + jq (pre-installed on ubuntu-latest) to post/update PR comments via the GitHub REST API, using a hidden HTML marker to find existing comments
1 parent 9a891ac commit 23c6944

File tree

2 files changed

+123
-47
lines changed

2 files changed

+123
-47
lines changed

.github/workflows/pr-preview-cleanup.yml

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,57 @@ jobs:
1414
cleanup-preview:
1515
runs-on: ubuntu-latest
1616
steps:
17-
# https://github.com/actions/checkout/releases/tag/v4.2.2
18-
- name: Checkout gh-pages branch
19-
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
20-
with:
21-
ref: gh-pages
22-
23-
- name: Remove PR preview directory
17+
- name: Remove PR preview directory from gh-pages
18+
env:
19+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
20+
PR_NUMBER: ${{ github.event.pull_request.number }}
2421
run: |
25-
PR_DIR="pr-${{ github.event.pull_request.number }}"
26-
if [ -d "$PR_DIR" ]; then
27-
git config user.name "github-actions[bot]"
28-
git config user.email "github-actions[bot]@users.noreply.github.com"
29-
git rm -rf "$PR_DIR"
30-
git commit -m "chore: remove preview for PR #${{ github.event.pull_request.number }}"
31-
git push
32-
else
33-
echo "No preview directory found for PR #${{ github.event.pull_request.number }}, nothing to clean up."
22+
REPO_URL="https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
23+
24+
if ! git clone --single-branch --branch gh-pages --depth 1 "$REPO_URL" gh-pages-out 2>/dev/null; then
25+
echo "No gh-pages branch found, nothing to clean up."
26+
exit 0
27+
fi
28+
29+
PR_DIR="gh-pages-out/pr-${PR_NUMBER}"
30+
if [ ! -d "$PR_DIR" ]; then
31+
echo "No preview directory found for PR #${PR_NUMBER}, nothing to clean up."
32+
exit 0
3433
fi
3534
35+
cd gh-pages-out
36+
git config user.name "github-actions[bot]"
37+
git config user.email "github-actions[bot]@users.noreply.github.com"
38+
git rm -rf "pr-${PR_NUMBER}"
39+
git commit -m "chore: remove preview for PR #${PR_NUMBER}"
40+
git push origin gh-pages
41+
3642
- name: Update PR comment
37-
# https://github.com/thollander/actions-comment-pull-request/releases/tag/v3.0.1
38-
uses: thollander/actions-comment-pull-request@24bffb9b452ba05a4f3f77933840a6a841d1b32b
39-
with:
40-
comment-tag: pr-preview-url
41-
mode: upsert
42-
message: |
43-
## Preview deployment
44-
45-
Preview has been removed as this PR is now closed.
43+
env:
44+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
45+
PR_NUMBER: ${{ github.event.pull_request.number }}
46+
run: |
47+
MARKER="<!-- pr-preview-url -->"
48+
49+
# Find the existing preview comment
50+
COMMENT_ID=$(curl -sf \
51+
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
52+
-H "Accept: application/vnd.github+json" \
53+
"https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments?per_page=100" \
54+
| jq -r ".[] | select(.body | startswith(\"${MARKER}\")) | .id" | head -1)
55+
56+
if [ -z "$COMMENT_ID" ]; then
57+
echo "No preview comment found, nothing to update."
58+
exit 0
59+
fi
60+
61+
BODY="${MARKER}
62+
## Preview deployment
63+
64+
Preview has been removed as this PR is now closed."
65+
66+
curl -sf -X PATCH \
67+
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
68+
-H "Accept: application/vnd.github+json" \
69+
"https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/comments/${COMMENT_ID}" \
70+
-d "$(jq -n --arg body "$BODY" '{"body": $body}')"

.github/workflows/pr-preview.yml

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)