Skip to content

Commit 94c0bb1

Browse files
committed
upload preview to surge.sh
1 parent 88e5244 commit 94c0bb1

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

.github/upload-preview.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { Octokit } from '@octokit/rest';
2+
const { default: surge } = await import('surge');
3+
const octokit = new Octokit({ auth: process.env.GH_PR_TOKEN });
4+
const publishFn = surge().publish();
5+
6+
// From github actions
7+
const ghrepo = process.env.GITHUB_REPOSITORY || '';
8+
const [owner, repo] = ghrepo.split('/');
9+
const prnum = process.env.GH_PR_NUM;
10+
const prbranch = process.env.GITHUB_REF.split('/').pop();
11+
12+
const uploadFolder = process.argv[2];
13+
if (!uploadFolder) {
14+
process.exit(1);
15+
}
16+
17+
let uploadURL = `${repo}-${prnum ? `pr-${prnum}` : prbranch}`.replace(/[/|.]/g, '-');
18+
uploadURL += ".surge.sh"
19+
20+
21+
publishFn({
22+
project: uploadFolder,
23+
p: uploadFolder,
24+
domain: uploadURL,
25+
d: uploadURL,
26+
e: 'https://surge.surge.sh',
27+
endpoint: 'https://surge.surge.sh'
28+
});
29+
30+
function tryAddComment(comment, commentBody) {
31+
if (!commentBody.includes(comment)) {
32+
return comment;
33+
}
34+
return '';
35+
}
36+
37+
if (prnum) {
38+
octokit.issues
39+
.listComments({
40+
owner,
41+
repo,
42+
issue_number: prnum
43+
})
44+
.then((res) => res.data)
45+
.then((comments) => {
46+
let commentBody = '';
47+
const existingComment = comments.find((comment) => comment.user.login === 'openssl-machine');
48+
if (existingComment) {
49+
commentBody += existingComment.body.trim();
50+
commentBody += '\n\n';
51+
}
52+
53+
commentBody += tryAddComment(`Preview: https://${uploadURL}`, commentBody);
54+
55+
if (existingComment) {
56+
octokit.issues
57+
.updateComment({
58+
owner,
59+
repo,
60+
comment_id: existingComment.id,
61+
body: commentBody
62+
})
63+
.then(() => console.log('Updated comment!'));
64+
} else {
65+
octokit.issues
66+
.createComment({
67+
owner,
68+
repo,
69+
issue_number: prnum,
70+
body: commentBody
71+
})
72+
.then(() => console.log('Created comment!'));
73+
}
74+
});
75+
}

.github/workflows/preview-pr.yml

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
name: "Deploy site"
1+
name: "Build preview from PR"
22

33
on:
44
pull_request:
55

66
jobs:
77
build:
88
runs-on: ubuntu-latest
9+
env:
10+
SURGE_LOGIN: ${{ secrets.SURGE_LOGIN }}
11+
SURGE_TOKEN: ${{ secrets.SURGE_TOKEN }}
12+
GH_PR_TOKEN: ${{ secrets.GH_PR_TOKEN }}
13+
GH_PR_NUM: ${{ github.event.number }}
914
steps:
1015
- uses: actions/checkout@v4
1116
- name: "Fetch gh-pages branch"
@@ -20,4 +25,18 @@ jobs:
2025
python build.py master"
2126
- name: "Fix ownership"
2227
run: sudo chown -R "${USER:-$(id -un)}" $(pwd)
23-
28+
- name: "Switch to gh-pages"
29+
run: git switch gh-pages
30+
- name: "Copy files"
31+
run: rsync -r $(pwd) /tmp
32+
- name: "Switch back"
33+
run: git switch --detach -
34+
- uses: actions/setup-node@v4
35+
with:
36+
node-version: "18"
37+
- name: "Install dependencies"
38+
run: |
39+
echo '{"type": "module"}' > package.json
40+
npm install @types/node @octokit/rest surge
41+
- name: "Upload site"
42+
run: node .github/upload-preview.js /tmp/$(basename $(pwd))

0 commit comments

Comments
 (0)