forked from opentiny/tiny-robot
-
Notifications
You must be signed in to change notification settings - Fork 0
239 lines (215 loc) · 8.74 KB
/
Copy pathpr-deploy-preview.yml
File metadata and controls
239 lines (215 loc) · 8.74 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
name: 'CI: Deploy Preview'
on:
workflow_run:
workflows: ["ci"]
types:
- completed
permissions:
pull-requests: write
actions: read
contents: read
jobs:
deploy:
# 只在 PR 触发且 CI 成功时运行
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
steps:
- name: Download build artifacts
uses: actions/download-artifact@v7
with:
name: build-${{ github.event.workflow_run.head_sha }}
path: artifacts
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ github.event.workflow_run.id }}
- name: Read PR Number
id: pr
run: |
if [ -f artifacts/pr_number.txt ]; then
echo "number=$(cat artifacts/pr_number.txt)" >> $GITHUB_OUTPUT
else
echo "PR number file not found"
exit 1
fi
- name: Download pkg.pr.new output
uses: actions/download-artifact@v7
with:
name: pkg-pr-new-output-${{ github.event.workflow_run.head_sha }}
path: pkg-artifacts
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ github.event.workflow_run.id }}
continue-on-error: true
- name: Comment build generating
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const marker = process.env.COMMENT_MARKER;
const body = process.env.COMMENT_BODY;
const issue_number = Number(process.env.PR_NUMBER);
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number,
per_page: 100,
});
const existingComment = comments.find((comment) =>
comment.user?.login === 'github-actions[bot]' &&
comment.body?.includes(marker)
);
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body,
});
} else {
await github.rest.issues.createComment({
issue_number,
owner: context.repo.owner,
repo: context.repo.repo,
body,
});
}
env:
PR_NUMBER: ${{ steps.pr.outputs.number }}
COMMENT_MARKER: '<!-- TINY_ROBOT_BUILD -->'
COMMENT_BODY: |
[<img width="400" src="https://github.com/user-attachments/assets/c25bbbe4-0cf5-4aca-b4b3-db49e7a264d9">](#)
🔨 Deploying preview...
<!-- TINY_ROBOT_BUILD -->
- name: Post or update pkg.pr.new comment
if: hashFiles('pkg-artifacts/pkg-pr-new-output.json') != ''
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const output = JSON.parse(fs.readFileSync('pkg-artifacts/pkg-pr-new-output.json', 'utf8'));
const sha = '${{ github.event.workflow_run.head_sha }}'.substring(0, 7);
const packages = output.packages.map((p) => {
const shortUrl = p.url.replace(/@[a-f0-9]{40}/, '@' + sha);
return 'pnpm add ' + shortUrl;
}).join('\n\n');
const commitUrl = 'https://github.com/${{ github.repository }}/commit/' + sha;
const body = '## 📦 Package Preview\n\n' + packages + '\n\n**commit:** [' + sha + '](' + commitUrl + ')\n\n<!-- PKG_PR_NEW_COMMENT -->';
const botCommentIdentifier = '<!-- PKG_PR_NEW_COMMENT -->';
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: ${{ steps.pr.outputs.number }},
});
const existingComment = comments.data.find((comment) =>
comment.user?.login === 'github-actions[bot]' &&
comment.body?.includes(botCommentIdentifier)
);
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body: body,
});
} else {
await github.rest.issues.createComment({
issue_number: ${{ steps.pr.outputs.number }},
owner: context.repo.owner,
repo: context.repo.repo,
body: body,
});
}
- name: Deploy Site to Surge
id: deploy
run: |
DEPLOY_DOMAIN=preview-${{ steps.pr.outputs.number }}-tiny-robot.surge.sh
echo "Deploying to: https://$DEPLOY_DOMAIN"
npx surge --project ./artifacts/docs/dist --domain $DEPLOY_DOMAIN --token $SURGE_TOKEN
echo "url=https://$DEPLOY_DOMAIN" >> $GITHUB_OUTPUT
env:
SURGE_TOKEN: ${{ secrets.SURGE_TOKEN }}
- name: Comment build success
if: success()
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const marker = process.env.COMMENT_MARKER;
const body = process.env.COMMENT_BODY;
const issue_number = Number(process.env.PR_NUMBER);
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number,
per_page: 100,
});
const existingComment = comments.find((comment) =>
comment.user?.login === 'github-actions[bot]' &&
comment.body?.includes(marker)
);
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body,
});
} else {
await github.rest.issues.createComment({
issue_number,
owner: context.repo.owner,
repo: context.repo.repo,
body,
});
}
env:
PR_NUMBER: ${{ steps.pr.outputs.number }}
COMMENT_MARKER: '<!-- TINY_ROBOT_BUILD -->'
COMMENT_BODY: |
[<img width="400" src="https://github.com/user-attachments/assets/2a0bb639-dfaf-4384-8d73-cec616ea4b97">](https://preview-${{ steps.pr.outputs.number }}-tiny-robot.surge.sh)
✅ Preview build completed successfully!
> Click the image above to preview.
> Preview will be automatically removed when this PR is closed.
<!-- TINY_ROBOT_BUILD -->
- name: Comment build failed
if: failure()
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const marker = process.env.COMMENT_MARKER;
const body = process.env.COMMENT_BODY;
const issue_number = Number(process.env.PR_NUMBER);
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number,
per_page: 100,
});
const existingComment = comments.find((comment) =>
comment.user?.login === 'github-actions[bot]' &&
comment.body?.includes(marker)
);
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body,
});
} else {
await github.rest.issues.createComment({
issue_number,
owner: context.repo.owner,
repo: context.repo.repo,
body,
});
}
env:
PR_NUMBER: ${{ steps.pr.outputs.number }}
COMMENT_MARKER: '<!-- TINY_ROBOT_BUILD -->'
COMMENT_BODY: |
[<img width="400" src="https://github.com/user-attachments/assets/acfe799d-40be-4ffb-8190-da33f84056dc">](#)
❌ Deploy failed. Please check the logs for details.
<!-- TINY_ROBOT_BUILD -->