Skip to content

Commit 4780b28

Browse files
committed
ci: 添加可复用的 PR 预览工作流
1 parent 7be1637 commit 4780b28

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
name: reusable-pr-preview
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
surge-token:
7+
required: true
8+
type: string
9+
10+
jobs:
11+
preview-success:
12+
runs-on: ubuntu-latest
13+
if: >
14+
github.event.workflow_run.event == 'pull_request' &&
15+
github.event.workflow_run.conclusion == 'success'
16+
steps:
17+
- name: download pr artifact
18+
uses: dawidd6/action-download-artifact@v6
19+
with:
20+
workflow: ${{ github.event.workflow_run.workflow_id }}
21+
run_id: ${{ github.event.workflow_run.id }}
22+
name: pr
23+
- name: save PR id
24+
id: pr
25+
run: echo "id=$(<pr-id.txt)" >> $GITHUB_OUTPUT
26+
- name: download _site artifact
27+
uses: dawidd6/action-download-artifact@v6
28+
with:
29+
workflow: ${{ github.event.workflow_run.workflow_id }}
30+
run_id: ${{ github.event.workflow_run.id }}
31+
name: _site
32+
- run: |
33+
unzip _site.zip
34+
35+
- name: Upload surge service and generate preview URL
36+
id: deploy
37+
run: |
38+
repository=${{github.repository}}
39+
project_name=${repository#*/}
40+
export DEPLOY_DOMAIN=https://preview-pr-${{ steps.pr.outputs.id }}-$project_name.surge.sh
41+
npx surge --project ./_site --domain $DEPLOY_DOMAIN --token ${{ inputs.surge-token }}
42+
echo the preview URL is $DEPLOY_DOMAIN
43+
echo "url=$DEPLOY_DOMAIN" >> $GITHUB_OUTPUT
44+
- name: update status comment
45+
uses: actions-cool/maintain-one-comment@v3
46+
with:
47+
token: ${{ secrets.GITHUB_TOKEN }}
48+
body: |
49+
<a href="${{steps.deploy.outputs.url}}"><img height="96" alt="完成" src="https://user-images.githubusercontent.com/15634204/150816437-9f5bb788-cd67-4cbc-9897-b82d74e9aa65.png" /></a>
50+
<!-- [工作流地址](${{github.server_url}}/${{github.repository}}/actions/runs/${{github.run_id}}) -->
51+
<!-- AUTO_PREVIEW_HOOK -->
52+
number: ${{ steps.pr.outputs.id }}
53+
body-include: <!-- AUTO_PREVIEW_HOOK -->
54+
55+
- run: |
56+
rm -rf _site/
57+
58+
- name: The job failed
59+
if: ${{ failure() }}
60+
uses: actions-cool/maintain-one-comment@v3
61+
with:
62+
token: ${{ secrets.GITHUB_TOKEN }}
63+
body: |
64+
[<img height="96" alt="失败" src="https://user-images.githubusercontent.com/5378891/75333447-1e63a280-58c1-11ea-975d-235367fd1522.png">](${{github.server_url}}/${{github.repository}}/actions/runs/${{github.run_id}})
65+
<!-- AUTO_PREVIEW_HOOK -->
66+
number: ${{ steps.pr.outputs.id }}
67+
body-include: <!-- AUTO_PREVIEW_HOOK -->
68+
69+
preview-failed:
70+
runs-on: ubuntu-latest
71+
if: >
72+
github.event.workflow_run.event == 'pull_request' &&
73+
github.event.workflow_run.conclusion == 'failure'
74+
steps:
75+
- name: download pr artifact
76+
uses: dawidd6/action-download-artifact@v6
77+
with:
78+
workflow: ${{ github.event.workflow_run.workflow_id }}
79+
run_id: ${{ github.event.workflow_run.id }}
80+
name: pr
81+
- name: save PR id
82+
id: pr
83+
run: echo "id=$(<pr-id.txt)" >> $GITHUB_OUTPUT
84+
- name: The job failed
85+
uses: actions-cool/maintain-one-comment@v3
86+
with:
87+
token: ${{ secrets.GITHUB_TOKEN }}
88+
body: |
89+
[<img height="96" alt="失败" src="https://user-images.githubusercontent.com/5378891/75333447-1e63a280-58c1-11ea-975d-235367fd1522.png">](${{github.server_url}}/${{github.repository}}/actions/runs/${{github.run_id}})
90+
<!-- AUTO_PREVIEW_HOOK -->
91+
number: ${{ steps.pr.outputs.id }}
92+
body-include: <!-- AUTO_PREVIEW_HOOK -->

0 commit comments

Comments
 (0)