Skip to content

Commit ceb16bd

Browse files
committed
feat: add reusable CI workflows for building and E2E testing
1 parent 0b9fdb4 commit ceb16bd

6 files changed

Lines changed: 145 additions & 76 deletions

File tree

.github/workflows/pr-ci-build.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,21 @@ jobs:
4242
- name: Build components
4343
run: pnpm build
4444

45+
- name: Build docs
46+
run: pnpm -F docs build
47+
48+
- name: Save PR number
49+
if: github.event_name == 'pull_request'
50+
run: echo "${{ github.event.number }}" > pr_number.txt
51+
4552
- name: Upload build artifacts
4653
uses: actions/upload-artifact@v4
4754
with:
48-
name: build-${{ github.sha }}
55+
name: build-${{ github.event.pull_request.head.sha || github.sha }}
4956
path: |
5057
packages/components/dist
5158
packages/kit/dist
5259
packages/svgs/dist
60+
docs/dist
61+
pr_number.txt
5362
retention-days: 1

.github/workflows/pr-ci-e2e-test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ jobs:
3838
- name: Download build artifacts
3939
uses: actions/download-artifact@v4
4040
with:
41-
name: build-${{ github.sha }}
42-
path: packages
41+
name: build-${{ github.event.pull_request.head.sha || github.sha }}
42+
path: .
4343

4444
- name: Install Playwright Browser
4545
run: |
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: 'CI: Publish Packages'
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
pr-number:
7+
description: 'Pull Request number'
8+
type: number
9+
required: true
10+
11+
jobs:
12+
preview-job:
13+
name: preview
14+
runs-on: ubuntu-latest
15+
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
34+
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-
39+
40+
- name: Install dependencies
41+
run: pnpm i --no-frozen-lockfile
42+
43+
- name: Download build artifacts
44+
uses: actions/download-artifact@v4
45+
with:
46+
name: build-${{ github.event.pull_request.head.sha || github.sha }}
47+
path: .
48+
49+
- name: Publish to pkg.pr.new
50+
id: pkg-pr-new
51+
run: |
52+
npx pkg-pr-new publish \
53+
./packages/components \
54+
./packages/kit \
55+
./packages/svgs \
56+
--pnpm \
57+
--json output.json \
58+
--comment=off
59+
60+
- name: Save pkg.pr.new output
61+
run: |
62+
if [ -f output.json ]; then
63+
cp output.json pkg-pr-new-output.json
64+
fi
65+
66+
- name: Upload pkg.pr.new output
67+
uses: actions/upload-artifact@v4
68+
with:
69+
name: pkg-pr-new-output-${{ github.event.pull_request.head.sha || github.sha }}
70+
path: pkg-pr-new-output.json
71+
retention-days: 1
72+
if-no-files-found: ignore

.github/workflows/pr-ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ jobs:
2828
with:
2929
skip-playground: true
3030

31-
# 生成预览(依赖构建,仅 PR 事件)
32-
preview:
31+
# 发布包预览(依赖构建,仅 PR 事件)
32+
publish-packages:
3333
needs: [build]
3434
if: github.event_name == 'pull_request'
35-
uses: ./.github/workflows/pr-ci-preview.yml
35+
uses: ./.github/workflows/pr-ci-publish-packages.yml
3636
with:
3737
pr-number: ${{ github.event.pull_request.number }}
3838
secrets: inherit
3939

40-
# E2E 测试(依赖构建,与预览并行
40+
# E2E 测试(依赖构建)
4141
e2e-test:
4242
needs: [build]
4343
uses: ./.github/workflows/pr-ci-e2e-test.yml

.github/workflows/pr-cleanup.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
name: PR Docs Cleanup
1+
name: 'CI: Docs Cleanup'
22

33
on:
4-
pull_request:
4+
pull_request_target:
55
types: [closed]
66

7+
permissions:
8+
pull-requests: write
9+
710
jobs:
811
cleanup:
912
runs-on: ubuntu-latest
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,88 @@
1-
name: 'CI: Preview'
1+
name: 'CI: Deploy Preview'
22

33
on:
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

1114
jobs:
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) =>
@@ -113,7 +98,7 @@ jobs:
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

Comments
 (0)