-
Notifications
You must be signed in to change notification settings - Fork 27
188 lines (165 loc) · 6.43 KB
/
preview-publish.yml
File metadata and controls
188 lines (165 loc) · 6.43 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
name: PREVIEW_PUBLISH
on:
workflow_run:
workflows: ['MAIN_PULL_REQUEST']
types:
- completed
concurrency:
group: preview-publish-${{ github.event.workflow_run.head_branch }}-${{ github.event.workflow_run.head_repository.full_name }}
cancel-in-progress: true
permissions:
contents: read
actions: read
pull-requests: write
jobs:
preflight:
runs-on: ubuntu-latest
if: >-
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.event == 'pull_request'
outputs:
pr_id: ${{ steps.pr.outputs.id }}
web_components_domain: ${{ steps.domains.outputs.web_components_domain }}
chat_domain: ${{ steps.domains.outputs.chat_domain }}
steps:
- name: Download PR metadata
uses: dawidd6/action-download-artifact@v21
with:
workflow: ${{ github.event.workflow_run.name }}
run_id: ${{ github.event.workflow_run.id }}
name: pr-id
path: ./pr-id
- name: Extract PR number
id: pr
run: |
PR_ID=$(cat ./pr-id/pr-id.txt)
if [ -z "$PR_ID" ]; then
echo "::error::Failed to extract PR id from artifact"
exit 1
fi
echo "id=$PR_ID" >> "$GITHUB_OUTPUT"
- name: Compute preview domains
id: domains
env:
PR_ID: ${{ steps.pr.outputs.id }}
OWNER: ${{ github.repository_owner }}
run: |
OWNER_LOWER=$(echo "$OWNER" | tr '[:upper:]' '[:lower:]')
{
echo "web_components_domain=https://preview-pr-${PR_ID}-${OWNER_LOWER}-tdesign-web-components.surge.sh"
echo "chat_domain=https://preview-pr-${PR_ID}-${OWNER_LOWER}-tdesign-vue-next-chat.surge.sh"
} >> "$GITHUB_OUTPUT"
tdesign-web-components:
needs: preflight
runs-on: ubuntu-latest
steps:
- name: Download artifact
uses: dawidd6/action-download-artifact@v21
with:
workflow: ${{ github.event.workflow_run.name }}
run_id: ${{ github.event.workflow_run.id }}
name: tdesign-web-components-site
path: ./site
- name: Prepare SPA fallback
run: cp ./site/index.html ./site/200.html
- name: Deploy to Surge.sh
run: |
npx surge --project ./site \
--domain ${{ needs.preflight.outputs.web_components_domain }} \
--token ${{ secrets.TDESIGN_SURGE_TOKEN }}
tdesign-vue-next-chat:
needs: preflight
runs-on: ubuntu-latest
steps:
- name: Download artifact
id: download
uses: dawidd6/action-download-artifact@v21
with:
workflow: ${{ github.event.workflow_run.name }}
run_id: ${{ github.event.workflow_run.id }}
name: tdesign-vue-next-chat-site
path: ./site
if_no_artifact_found: warn
- name: Skip if artifact missing
if: steps.download.outputs.found_artifact != 'true'
run: echo "::notice::chat artifact not found, skipping chat deployment"
- name: Prepare SPA fallback
if: steps.download.outputs.found_artifact == 'true'
run: cp ./site/index.html ./site/200.html
- name: Deploy to Surge.sh
if: steps.download.outputs.found_artifact == 'true'
run: |
npx surge --project ./site \
--domain ${{ needs.preflight.outputs.chat_domain }} \
--token ${{ secrets.TDESIGN_SURGE_TOKEN }}
preview-comment:
if: always() && needs.preflight.result == 'success'
needs:
- preflight
- tdesign-web-components
- tdesign-vue-next-chat
runs-on: ubuntu-latest
steps:
- name: Collect job results
id: report
uses: actions/github-script@v7
env:
WEB_COMPONENTS_DOMAIN: ${{ needs.preflight.outputs.web_components_domain }}
CHAT_DOMAIN: ${{ needs.preflight.outputs.chat_domain }}
with:
script: |
const { WEB_COMPONENTS_DOMAIN, CHAT_DOMAIN } = process.env;
const { data: { jobs } } = await github.rest.actions.listJobsForWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.runId,
per_page: 100,
});
const components = {
'tdesign-web-components': {
package_name: '@tdesign/web-components-ui<br>@tdesign/web-components-chat',
domain: WEB_COMPONENTS_DOMAIN,
status: '',
report_url: '',
},
'tdesign-vue-next-chat': {
package_name: '@tdesign-vue-next/chat',
domain: CHAT_DOMAIN,
status: '',
report_url: '',
},
};
const previewSuccess = `<img width="300" alt="完成" src="https://user-images.githubusercontent.com/15634204/150816437-9f5bb788-cd67-4cbc-9897-b82d74e9aa65.png" />`;
const previewFailure = `<img width="300" alt="失败" src="https://user-images.githubusercontent.com/5378891/75333447-1e63a280-58c1-11ea-975d-235367fd1522.png" />`;
jobs
.filter((job) => Object.prototype.hasOwnProperty.call(components, job.name))
.forEach((job) => {
const entry = components[job.name];
if (job.conclusion === 'success') {
entry.status = previewSuccess;
entry.report_url = entry.domain;
} else {
entry.status = previewFailure;
entry.report_url = job.html_url;
}
});
const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
const rows = Object.values(components)
.map((c) => `| ${c.package_name} | [${c.status}](${c.report_url}) |`)
.join('\n');
const comment = [
`# TDesign Component Site Preview [Open](${runUrl})`,
'| Component | Preview |',
'| --------- | :--: |',
rows,
'',
'<!-- AUTO_PREVIEW_HOOK -->',
].join('\n');
core.setOutput('comment', comment);
- name: Post comment
uses: actions-cool/maintain-one-comment@v3
with:
token: ${{ secrets.TDESIGN_BOT_TOKEN }}
number: ${{ needs.preflight.outputs.pr_id }}
body: ${{ steps.report.outputs.comment }}
body-include: '<!-- AUTO_PREVIEW_HOOK -->'