1- name : ' CI: Preview'
1+ name : ' CI: Deploy Preview'
22
33on :
4- workflow_call :
5- inputs :
6- pr-number :
7- description : ' Pull Request number'
8- type : number
9- required : true
4+ workflow_run :
5+ workflows : ["ci"]
6+ types :
7+ - completed
8+
9+ permissions :
10+ pull-requests : write
11+ actions : read
12+ contents : read
1013
1114jobs :
12- preview-job :
13- name : preview
15+ deploy :
16+ # 只在 PR 触发且 CI 成功时运行
17+ if : >
18+ github.event.workflow_run.event == 'pull_request' &&
19+ github.event.workflow_run.conclusion == 'success'
1420 runs-on : ubuntu-latest
1521 steps :
16- - name : Checkout
17- uses : actions/checkout@v4
18-
19- - name : Setup pnpm
20- uses : pnpm/action-setup@v4
21-
22- - name : Setup Node.js
23- uses : actions/setup-node@v4
24- with :
25- node-version : 20
26- registry-url : ' https://registry.npmjs.org'
27-
28- - name : Get pnpm store directory
29- id : pnpm-cache
30- run : echo "pnpm_cache_dir=$(pnpm store path)" >> $GITHUB_OUTPUT
31-
32- - name : Setup pnpm cache
33- uses : actions/cache@v4
22+ - name : Download build artifacts
23+ uses : actions/download-artifact@v4
3424 with :
35- path : ${{ steps.pnpm-cache.outputs.pnpm_cache_dir }}
36- key : ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
37- restore-keys : |
38- ${{ runner.os }}-pnpm-store-
25+ name : build- ${{ github.event.workflow_run.head_sha }}
26+ path : artifacts
27+ github-token : ${{ secrets.GITHUB_TOKEN }}
28+ run-id : ${{ github.event.workflow_run.id }}
3929
40- - name : Install dependencies
41- run : pnpm i --no-frozen-lockfile
30+ - name : Read PR Number
31+ id : pr
32+ run : |
33+ if [ -f artifacts/pr_number.txt ]; then
34+ echo "number=$(cat artifacts/pr_number.txt)" >> $GITHUB_OUTPUT
35+ else
36+ echo "PR number file not found"
37+ exit 1
38+ fi
4239
43- - name : Download build artifacts
40+ - name : Download pkg.pr.new output
4441 uses : actions/download-artifact@v4
4542 with :
46- name : build- ${{ github.sha }}
47- path : packages
48-
49- - name : Build docs only (components already built)
50- run : pnpm -F docs build
43+ name : pkg-pr-new-output- ${{ github.event.workflow_run.head_sha }}
44+ path : pkg-artifacts
45+ github-token : ${{ secrets.GITHUB_TOKEN }}
46+ run-id : ${{ github.event.workflow_run.id }}
47+ continue-on-error : true
5148
5249 - name : Comment build generating
53- if : github.event_name == 'pull_request'
5450 uses : actions-cool/maintain-one-comment@v3.1.1
5551 with :
5652 token : ${{ secrets.GITHUB_TOKEN }}
5753 body : |
5854 [<img width="400" src="https://github.com/user-attachments/assets/c25bbbe4-0cf5-4aca-b4b3-db49e7a264d9">](#)
5955
60- 🔨 Building packages ...
56+ 🔨 Deploying preview ...
6157 <!-- TINY_ROBOT_BUILD -->
6258 body-include : ' <!-- TINY_ROBOT_BUILD -->'
63- number : ${{ inputs.pr-number }}
64-
65- - name : Publish to pkg.pr.new
66- id : pkg-pr-new
67- run : |
68- npx pkg-pr-new publish \
69- ./packages/components \
70- ./packages/kit \
71- ./packages/svgs \
72- --pnpm \
73- --json output.json \
74- --comment=off
59+ number : ${{ steps.pr.outputs.number }}
7560
7661 - name : Post or update pkg.pr.new comment
77- if : github.event_name == 'pull_request '
62+ if : hashFiles('pkg-artifacts/pkg-pr-new-output.json') != ' '
7863 uses : actions/github-script@v7
7964 with :
8065 github-token : ${{ secrets.GITHUB_TOKEN }}
8166 script : |
8267 const fs = require('fs');
83- const output = JSON.parse(fs.readFileSync('output.json', 'utf8'));
68+ const output = JSON.parse(fs.readFileSync('pkg-artifacts/pkg-pr-new- output.json', 'utf8'));
8469
85- const sha = context.payload.pull_request.head.sha .substring(0, 7);
70+ const sha = '${{ github.event.workflow_run.head_sha }}' .substring(0, 7);
8671
8772 const packages = output.packages.map((p) => {
8873 const shortUrl = p.url.replace(/@[a-f0-9]{40}/, '@' + sha);
8974 return 'pnpm add ' + shortUrl;
9075 }).join('\n\n');
9176
92- const commitUrl = 'https://github.com/' + context.repo.owner + '/' + context.repo.repo + ' /commit/' + sha;
77+ const commitUrl = 'https://github.com/${{ github.repository }} /commit/' + sha;
9378
9479 const body = '## 📦 Package Preview\n\n' + packages + '\n\n**commit:** [' + sha + '](' + commitUrl + ')\n\n<!-- PKG_PR_NEW_COMMENT -->';
9580 const botCommentIdentifier = '<!-- PKG_PR_NEW_COMMENT -->';
9681
9782 const comments = await github.rest.issues.listComments({
9883 owner: context.repo.owner,
9984 repo: context.repo.repo,
100- issue_number: context.issue. number,
85+ issue_number: ${{ steps.pr.outputs. number }} ,
10186 });
10287
10388 const existingComment = comments.data.find((comment) =>
11398 });
11499 } else {
115100 await github.rest.issues.createComment({
116- issue_number: context.issue. number,
101+ issue_number: ${{ steps.pr.outputs. number }} ,
117102 owner: context.repo.owner,
118103 repo: context.repo.repo,
119104 body: body,
@@ -123,38 +108,38 @@ jobs:
123108 - name : Deploy Site to Surge
124109 id : deploy
125110 run : |
126- DEPLOY_DOMAIN=preview-${{ inputs .pr- number }}-tiny-robot.surge.sh
111+ DEPLOY_DOMAIN=preview-${{ steps .pr.outputs. number }}-tiny-robot.surge.sh
127112 echo "Deploying to: https://$DEPLOY_DOMAIN"
128- npx surge --project ./docs/dist --domain $DEPLOY_DOMAIN --token $SURGE_TOKEN
113+ npx surge --project ./artifacts/ docs/dist --domain $DEPLOY_DOMAIN --token $SURGE_TOKEN
129114 echo "url=https://$DEPLOY_DOMAIN" >> $GITHUB_OUTPUT
130115 env :
131116 SURGE_TOKEN : ${{ secrets.SURGE_TOKEN }}
132117
133118 - name : Comment build success
134- if : ${{ success() && github.event_name == 'pull_request' }}
119+ if : success()
135120 uses : actions-cool/maintain-one-comment@v3.1.1
136121 with :
137122 token : ${{ secrets.GITHUB_TOKEN }}
138123 body : |
139- [<img width="400" src="https://github.com/user-attachments/assets/2a0bb639-dfaf-4384-8d73-cec616ea4b97">](https://preview-${{ inputs .pr- number }}-tiny-robot.surge.sh)
124+ [<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)
140125
141126 ✅ Preview build completed successfully!
142127
143128 > Click the image above to preview.
144129 > Preview will be automatically removed when this PR is closed.
145130 <!-- TINY_ROBOT_BUILD -->
146131 body-include : ' <!-- TINY_ROBOT_BUILD -->'
147- number : ${{ inputs .pr- number }}
132+ number : ${{ steps .pr.outputs. number }}
148133
149134 - name : Comment build failed
150- if : ${{ failure() && github.event_name == 'pull_request' }}
135+ if : failure()
151136 uses : actions-cool/maintain-one-comment@v3.1.1
152137 with :
153138 token : ${{ secrets.GITHUB_TOKEN }}
154139 body : |
155140 [<img width="400" src="https://github.com/user-attachments/assets/acfe799d-40be-4ffb-8190-da33f84056dc">](#)
156141
157- ❌ Build failed. Please check the logs for details.
142+ ❌ Deploy failed. Please check the logs for details.
158143 <!-- TINY_ROBOT_BUILD -->
159144 body-include : ' <!-- TINY_ROBOT_BUILD -->'
160- number : ${{ inputs .pr- number }}
145+ number : ${{ steps .pr.outputs. number }}
0 commit comments