Skip to content

Commit 91166df

Browse files
committed
Implement previews for GitHub pull requests
When a contributor submits a PR, we always perform a build. This takes it a step further and uploads that a custom surge.sh domain. It adds a sticky comment to link to that preview while also generating some diffs. This means reviews easier.
1 parent 439dda4 commit 91166df

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

.github/workflows/test.yml

+76
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,79 @@ jobs:
1515
run: |
1616
npm install
1717
npx honkit build
18+
19+
- name: Upload artifact
20+
uses: actions/upload-artifact@v4
21+
with:
22+
name: github-pages-pr-${{ github.event.pull_request.number }}
23+
path: _book/**
24+
25+
preview:
26+
runs-on: ubuntu-latest
27+
needs: build
28+
steps:
29+
- name: Set preview domain
30+
run: echo "PREVIEW_DOMAIN=$(echo ${{ github.repository }} | tr / - )-${{ github.job }}-pr-${{ github.event.pull_request.number }}.surge.sh" >> $GITHUB_ENV
31+
32+
- name: Install surge
33+
run: npm install surge
34+
35+
- name: Install diffstat and pygments
36+
run: |
37+
sudo apt-get update
38+
sudo apt-get install -y diffstat python3-pygments
39+
40+
# TODO: can this download from the existing pages or a cache?
41+
- name: Checkout
42+
uses: actions/checkout@v4
43+
with:
44+
persist-credentials: false
45+
46+
- name: Build current pages
47+
run: |
48+
npm install
49+
npx honkit build
50+
mv _book current
51+
52+
- name: Download preview pages
53+
uses: actions/download-artifact@v4
54+
with:
55+
# TODO: store output in upload step?
56+
name: github-pages-pr-${{ github.event.pull_request.number }}
57+
path: preview
58+
59+
- name: Create diff to current
60+
run: |
61+
ls -R current
62+
ls -R preview
63+
diff -Nrwu current/ preview/ > preview/diff.patch
64+
pygmentize -o preview/diff.html -l diff -f html -O full preview/diff.patch
65+
diffstat -l -p2 preview/diff.patch > diff.txt
66+
67+
- name: Deploy to surge.sh
68+
run: npx surge ./preview $PREVIEW_DOMAIN --token ${{ secrets.SURGE_TOKEN }}
69+
70+
- name: Generate summary
71+
run: |
72+
echo "The PR preview for ${{ github.event.pull_request.head.sha }} is available at [${{ env.PREVIEW_DOMAIN }}](https://${{ env.PREVIEW_DOMAIN }})" > pr.md
73+
74+
if [[ -f diff.txt ]] ; then
75+
echo "" >> pr.md
76+
echo "The following output files are affected by this PR:" >> pr.md
77+
sed -E "s#(.*)#- [\1](https://${{ env.PREVIEW_DOMAIN }}/\1)#" diff.txt >> pr.md
78+
fi
79+
80+
if [[ -f diff.patch ]] ; then
81+
echo "" >> pr.md
82+
echo "[show diff](https://${{ env.PREVIEW_DOMAIN }}/diff.patch)" >> pr.md
83+
fi
84+
85+
if [[ -f diff.html ]] ; then
86+
echo "" >> pr.md
87+
echo "[show diff as HTML](https://${{ env.PREVIEW_DOMAIN }}/diff.html)" >> pr.md
88+
fi
89+
90+
- name: Comment on PR
91+
uses: marocchino/sticky-pull-request-comment@v2
92+
with:
93+
path: pr.md

0 commit comments

Comments
 (0)