-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
182 lines (169 loc) · 6.68 KB
/
surge-preview-deploy.yml
File metadata and controls
182 lines (169 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
name: Surge Preview Deploy
on:
workflow_run:
workflows: ["Surge Preview Build"]
types:
- completed
jobs:
deploy:
runs-on: ubuntu-latest
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' && github.repository == 'doocs/md'
steps:
- name: Download PR artifact
uses: dawidd6/action-download-artifact@v14 # renovate: ignore
with:
workflow: ${{ github.event.workflow_run.workflow_id }}
run_id: ${{ github.event.workflow_run.id }}
name: pr
- name: Save PR id
id: pr
run: |
pr_id=$(<pr-id.txt)
if ! [[ "$pr_id" =~ ^[0-9]+$ ]]; then
echo "Error: pr-id.txt does not contain a valid numeric PR id. Please check."
exit 1
fi
echo "id=$pr_id" >> $GITHUB_OUTPUT
- name: Download dist artifact
uses: dawidd6/action-download-artifact@v14 # renovate: ignore
with:
workflow: ${{ github.event.workflow_run.workflow_id }}
run_id: ${{ github.event.workflow_run.id }}
workflow_conclusion: success
name: dist
- name: Upload surge service
id: deploy
run: |
export DEPLOY_DOMAIN=https://doocs-md-preview-pr-${{ steps.pr.outputs.id }}.surge.sh
npx surge --project ./ --domain $DEPLOY_DOMAIN --token ${{ secrets.SURGE_TOKEN }}
- name: Comment PR with preview link
uses: actions/github-script@v9
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const prNumber = ${{ steps.pr.outputs.id }};
const deployUrl = 'https://doocs-md-preview-pr-${{ steps.pr.outputs.id }}.surge.sh';
const commitSha = '${{ github.event.workflow_run.head_sha }}';
const body = [
'🚀 Surge Preview has been successfully deployed!',
'',
`**Preview URL:** ${deployUrl}`,
'',
`<sub>Built with commit ${commitSha}</sub>`,
'',
'<!-- Surge Preview Comment -->',
].join('\n');
// Upsert: find existing comment and update, or create new
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const existing = comments.find(c => c.body.includes('<!-- Surge Preview Comment -->'));
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body,
});
}
- name: Deploy failed
if: ${{ failure() }}
uses: actions/github-script@v9
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const prNumber = ${{ steps.pr.outputs.id }};
const workflowRunUrl = '${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}';
const body = [
'😭 Surge Preview deployment failed.',
'',
`Please check the [workflow run](${workflowRunUrl}) for details.`,
'',
'<!-- Surge Preview Comment -->',
].join('\n');
// Upsert: find existing comment and update, or create new
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const existing = comments.find(c => c.body.includes('<!-- Surge Preview Comment -->'));
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body,
});
}
failed:
runs-on: ubuntu-latest
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'failure' && github.repository == 'doocs/md'
steps:
- name: Download PR artifact
uses: dawidd6/action-download-artifact@v14 # renovate: ignore
with:
workflow: ${{ github.event.workflow_run.workflow_id }}
run_id: ${{ github.event.workflow_run.id }}
name: pr
- name: Save PR id
id: pr
run: |
pr_id=$(<pr-id.txt)
if ! [[ "$pr_id" =~ ^[0-9]+$ ]]; then
echo "Error: pr-id.txt does not contain a valid numeric PR id. Please check."
exit 1
fi
echo "id=$pr_id" >> $GITHUB_OUTPUT
- name: Comment PR with build failure
uses: actions/github-script@v9
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const prNumber = ${{ steps.pr.outputs.id }};
const workflowRunUrl = '${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}';
const body = [
'😭 Surge Preview build failed.',
'',
`Please check the [workflow run](${workflowRunUrl}) for details.`,
'',
'<!-- Surge Preview Comment -->',
].join('\n');
// Upsert: find existing comment and update, or create new
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const existing = comments.find(c => c.body.includes('<!-- Surge Preview Comment -->'));
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body,
});
}