File tree 2 files changed +96
-2
lines changed
2 files changed +96
-2
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
- name : " Deploy site "
1
+ name : " Build preview from PR "
2
2
3
3
on :
4
4
pull_request :
5
5
6
6
jobs :
7
7
build :
8
8
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 }}
9
14
steps :
10
15
- uses : actions/checkout@v4
11
16
- name : " Fetch gh-pages branch"
20
25
python build.py master"
21
26
- name : " Fix ownership"
22
27
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))
You can’t perform that action at this time.
0 commit comments