Skip to content

Commit 1ec6bc1

Browse files
committed
ci: enhance pr site preview comment and add concurrency
1 parent f941060 commit 1ec6bc1

1 file changed

Lines changed: 46 additions & 10 deletions

File tree

.github/workflows/pages.yml

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
name: PR Preview & GitHub Pages Publish
2+
23
on:
34
pull_request:
45
types: [opened, synchronize, reopened, closed]
56
push:
67
branches:
78
- main
9+
810
permissions:
9-
contents: write # gh-pages + cleanup
10-
pull-requests: write # PR comments
11-
pages: write # deploy-pages
12-
id-token: write # required by deploy-pages
11+
contents: write
12+
pull-requests: write
13+
pages: write
14+
id-token: write
15+
16+
concurrency:
17+
group: pages-${{ github.ref }}
18+
cancel-in-progress: true
19+
1320
jobs:
1421
build:
1522
if: github.event.action != 'closed'
@@ -39,7 +46,9 @@ jobs:
3946
with:
4047
github_token: ${{ secrets.GITHUB_TOKEN }}
4148
publish_dir: site
49+
publish_branch: gh-pages
4250
destination_dir: pr-${{ github.event.pull_request.number }}
51+
keep_files: true
4352

4453
- name: Comment PR with preview URL
4554
if: github.event_name == 'pull_request'
@@ -48,15 +57,38 @@ jobs:
4857
script: |
4958
const pr = context.payload.pull_request.number;
5059
const { owner, repo } = context.repo;
51-
5260
const url = `https://ff4j.github.io/docs/pr-${pr}/`;
61+
const marker = "<!-- pr-preview-comment -->";
62+
63+
const body = `${marker} \n **Site preview available** 🔗 ${url}`;
5364
54-
await github.rest.issues.createComment({
65+
const { data: comments } = await github.rest.issues.listComments({
5566
owner,
5667
repo,
5768
issue_number: pr,
58-
body: `📘 **Site preview available**:\n\n${url}`
5969
});
70+
71+
const existing = comments.find(comment =>
72+
comment.body?.includes(marker)
73+
);
74+
75+
if (existing) {
76+
// Update existing comment
77+
await github.rest.issues.updateComment({
78+
owner,
79+
repo,
80+
comment_id: existing.id,
81+
body,
82+
});
83+
} else {
84+
// Create new comment
85+
await github.rest.issues.createComment({
86+
owner,
87+
repo,
88+
issue_number: pr,
89+
body,
90+
});
91+
}
6092
6193
- name: Upload Pages artifact
6294
if: github.event_name == 'push'
@@ -76,8 +108,7 @@ jobs:
76108

77109
cleanup:
78110
if: github.event_name == 'pull_request' &&
79-
github.event.action == 'closed' &&
80-
github.event.pull_request.merged == false
111+
github.event.action == 'closed'
81112
runs-on: ubuntu-latest
82113

83114
steps:
@@ -88,7 +119,12 @@ jobs:
88119

89120
- name: Remove PR preview folder
90121
run: |
91-
rm -rf pr-${{ github.event.pull_request.number }}
122+
if [ -d "pr-${{ github.event.pull_request.number }}" ]; then
123+
rm -rf pr-${{ github.event.pull_request.number }}
124+
echo "Preview folder removed."
125+
else
126+
echo "Preview folder does not exist."
127+
fi
92128
93129
- name: Commit cleanup
94130
run: |

0 commit comments

Comments
 (0)