Skip to content

Commit f53604d

Browse files
authored
Merge pull request #48 from jamezp/pr-preview
Add a way to preview the site build with PR's.
2 parents af9df67 + 6592095 commit f53604d

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

.github/workflows/pr-preview.yml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
name: PR Preview
2+
3+
on:
4+
workflow_run:
5+
workflows: ["Test site build"]
6+
types: [completed]
7+
pull_request_target:
8+
types: [closed]
9+
10+
jobs:
11+
deploy-preview:
12+
if: >
13+
github.event_name == 'workflow_run'
14+
&& github.event.workflow_run.conclusion == 'success'
15+
&& github.event.workflow_run.event == 'pull_request'
16+
runs-on: ubuntu-latest
17+
permissions:
18+
pull-requests: write
19+
actions: read
20+
steps:
21+
- name: Download generated site
22+
uses: actions/download-artifact@v5
23+
with:
24+
name: generated-site
25+
path: ./site
26+
run-id: ${{ github.event.workflow_run.id }}
27+
github-token: ${{ secrets.GITHUB_TOKEN }}
28+
29+
- name: Download PR number
30+
uses: actions/download-artifact@v5
31+
with:
32+
name: pr-number
33+
run-id: ${{ github.event.workflow_run.id }}
34+
github-token: ${{ secrets.GITHUB_TOKEN }}
35+
36+
- name: Read PR number
37+
id: pr
38+
run: echo "number=$(cat pr-number.txt)" >> "$GITHUB_OUTPUT"
39+
40+
- name: Install Surge
41+
run: npm install -g surge
42+
43+
- name: Deploy to Surge
44+
run: surge ./site ${{ env.PREVIEW_URL }} --token ${{ secrets.SURGE_TOKEN }}
45+
env:
46+
PREVIEW_URL: pr-${{ steps.pr.outputs.number }}-resteasy-dev.surge.sh
47+
48+
- name: Comment on PR
49+
uses: actions/github-script@v7
50+
with:
51+
script: |
52+
const prNumber = ${{ steps.pr.outputs.number }};
53+
const previewUrl = `https://pr-${prNumber}-resteasy-dev.surge.sh`;
54+
const body = `A preview of this PR is available at ${previewUrl}`;
55+
const { data: comments } = await github.rest.issues.listComments({
56+
owner: context.repo.owner,
57+
repo: context.repo.repo,
58+
issue_number: prNumber,
59+
});
60+
const existing = comments.find(c =>
61+
c.user.login === 'github-actions[bot]' && c.body.includes('A preview of this PR is available at')
62+
);
63+
if (existing) {
64+
await github.rest.issues.updateComment({
65+
owner: context.repo.owner,
66+
repo: context.repo.repo,
67+
comment_id: existing.id,
68+
body: body,
69+
});
70+
} else {
71+
await github.rest.issues.createComment({
72+
owner: context.repo.owner,
73+
repo: context.repo.repo,
74+
issue_number: prNumber,
75+
body: body,
76+
});
77+
}
78+
79+
teardown-preview:
80+
if: github.event_name == 'pull_request_target' && github.event.action == 'closed'
81+
runs-on: ubuntu-latest
82+
steps:
83+
- name: Install Surge
84+
run: npm install -g surge
85+
86+
- name: Teardown preview
87+
run: surge teardown pr-${{ github.event.number }}-resteasy-dev.surge.sh --token ${{ secrets.SURGE_TOKEN }}

.github/workflows/testsite.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ jobs:
1616
cache: maven
1717
- name: Build with Maven
1818
run: mvn --batch-mode clean test
19+
- name: Save PR number
20+
run: echo ${{ github.event.number }} > pr-number.txt
21+
- name: Upload PR number
22+
uses: actions/upload-artifact@v6
23+
with:
24+
name: pr-number
25+
path: pr-number.txt
1926
- name: Archive generated site
2027
uses: actions/upload-artifact@v6
2128
with:

0 commit comments

Comments
 (0)