You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+158-5Lines changed: 158 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,7 +33,7 @@ jobs:
33
33
permissions:
34
34
pull-requests: write # allow surge-preview to create/update PR comments
35
35
steps:
36
-
- uses: actions/checkout@v2
36
+
- uses: actions/checkout@v4
37
37
- uses: afc163/surge-preview@v1
38
38
id: preview_step
39
39
with:
@@ -62,7 +62,7 @@ jobs:
62
62
preview-job-1:
63
63
runs-on: ubuntu-latest
64
64
steps:
65
-
- uses: actions/checkout@v2
65
+
- uses: actions/checkout@v4
66
66
- uses: afc163/surge-preview@v1
67
67
with:
68
68
surge_token: ${{ secrets.SURGE_TOKEN }}
@@ -73,7 +73,7 @@ jobs:
73
73
preview-job-2:
74
74
runs-on: ubuntu-latest
75
75
steps:
76
-
- uses: actions/checkout@v2
76
+
- uses: actions/checkout@v4
77
77
- uses: afc163/surge-preview@v1
78
78
with:
79
79
surge_token: ${{ secrets.SURGE_TOKEN }}
@@ -97,7 +97,7 @@ name: 🔂 Surge PR Preview
97
97
98
98
on:
99
99
pull_request:
100
-
# when using teardown: 'true', add default event types + closed event type
100
+
# when using teardown: 'true', add default event types + closed event type (for teardown)
101
101
types: [opened, synchronize, reopened, closed]
102
102
push:
103
103
@@ -107,7 +107,7 @@ jobs:
107
107
permissions:
108
108
pull-requests: write # allow surge-preview to create/update PR comments
109
109
steps:
110
-
- uses: actions/checkout@v2
110
+
- uses: actions/checkout@v4
111
111
- uses: afc163/surge-preview@v1
112
112
with:
113
113
surge_token: ${{ secrets.SURGE_TOKEN }}
@@ -118,6 +118,159 @@ jobs:
118
118
npm run build
119
119
```
120
120
121
+
122
+
### Usage to deal with PRs created from forked repositories
123
+
124
+
When someone creates a PR from a forked repository, there is a security challenge: workflows triggered by `pull_request` events do not have access to your to repository secrets (like your surge token) for security reasons.
125
+
126
+
**Why this is a problem:** Without access to the surge token, the preview deployment will fail.
127
+
128
+
**Why not use `pull_request_target`?** While this event does provide access to secrets, it executes code from the PR branch with your secrets, creating a security risk. Attackers could potentially steal your secrets by submitting malicious PRs.
name: pr-build-dist # Must match the name from build workflow
212
+
path: site/
213
+
214
+
- name: Deploy to Surge
215
+
uses: afc163/surge-preview@v1
216
+
with:
217
+
surge_token: ${{ secrets.SURGE_TOKEN }}
218
+
github_token: ${{ secrets.GITHUB_TOKEN }}
219
+
build: echo done
220
+
dist: site
221
+
failOnError: true
222
+
teardown: false # Teardown is handled by the separate workflow
223
+
```
224
+
225
+
**Teardown workflow** (triggered when a PR is closed):
226
+
227
+
```yaml
228
+
name: Surge PR Preview - Teardown Stage
229
+
230
+
on:
231
+
pull_request_target:
232
+
types: [closed]
233
+
234
+
permissions:
235
+
pull-requests: write # Needed to comment on PRs
236
+
237
+
jobs:
238
+
deploy: # Must match the job ID from the deploy workflow
239
+
runs-on: ubuntu-latest
240
+
steps:
241
+
- name: Teardown preview site
242
+
uses: afc163/surge-preview@v1
243
+
with:
244
+
surge_token: ${{ secrets.SURGE_TOKEN }}
245
+
github_token: ${{ secrets.GITHUB_TOKEN }}
246
+
failOnError: true
247
+
teardown: true
248
+
build: echo "Cleaning up preview"
249
+
```
250
+
251
+
252
+
#### Troubleshooting
253
+
254
+
When running the workflow triggered by `workflow_run` event, the surge-preview action retrieves the number of the Pull Request associated with the workflow run by doing API calls.
255
+
256
+
Occasionally, the API call may hit rate limits, as the search API can use many calls internally. In this case, the error is caught and a warning is logged. Re-running the workflow should resolve the issue.
257
+
258
+
As a workaround, you can use a [Personal Access Token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#about-personal-access-tokens) instead of the GITHUB_TOKEN: this PAT has a higher rate limit errors, so the API calls are more likely to succeed.
259
+
260
+
**Note**: Using a PAT as github_token input of the surge-preview action has a side effect: the PR comment created by the action will be created by the account to which the PAT belongs.
261
+
When using GITHUB_TOKEN, the PR comments are created by the GitHub Actions bot.
262
+
263
+
264
+
#### Limitations
265
+
266
+
In some situations, it is hard to know if the surge deployment has been done.
267
+
268
+
When a workflow is triggered by `workflow_run`, it does not appear in the PR checks, so you cannot see whether the workflow has run or if it has failed.
269
+
By default, there is no status on the commit. It is possible to add this manually in the workflow, for example by using [set-commit-status-action](https://github.com/myrotvorets/set-commit-status-action).
270
+
271
+
However, when the workflow runs, the usual comment is updated by the `surge-preview` action to indicate whether the deployment is in progress or if the Surge deployment succeeded or failed.
0 commit comments