ci: 在 PR 评论中添加 API 文档预览链接 #8
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: PREVIEW_BUILD | |
| on: | |
| pull_request: | |
| branches: [develop, main] | |
| types: [opened, synchronize, reopened] | |
| jobs: | |
| tdesign-flutter-site: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout tdesign-flutter | |
| run: git clone --depth 1 --branch develop https://github.com/Tencent/tdesign-flutter.git | |
| - name: Override tdesign_flutter_tools ref to current PR | |
| working-directory: tdesign-flutter/tdesign-component | |
| env: | |
| PR_REPO: ${{ github.event.pull_request.head.repo.full_name }} | |
| PR_SHA: ${{ github.event.pull_request.head.sha }} | |
| run: | | |
| # 把 pubspec.yaml 中 tdesign_flutter_tools 的 git 引用临时替换为当前 PR 的 head | |
| # 让 CI 真正测试到本次 PR 的改动(而不是 main 分支的旧版本) | |
| echo "Override tdesign_flutter_tools -> https://github.com/${PR_REPO}.git @ ${PR_SHA}" | |
| python3 - <<'PY' | |
| import os, re, pathlib | |
| p = pathlib.Path('pubspec.yaml') | |
| text = p.read_text(encoding='utf-8') | |
| repo = os.environ['PR_REPO'] | |
| sha = os.environ['PR_SHA'] | |
| pattern = re.compile( | |
| r'(tdesign_flutter_tools:\s*\n\s*git:\s*\n)' | |
| r'(\s*url:\s*).*\n' | |
| r'(\s*ref:\s*).*', | |
| re.MULTILINE, | |
| ) | |
| new_text, n = pattern.subn( | |
| rf'\1\2https://github.com/{repo}.git\n\3{sha}', | |
| text, | |
| ) | |
| if n != 1: | |
| raise SystemExit(f'Expected exactly 1 match, got {n}') | |
| p.write_text(new_text, encoding='utf-8') | |
| PY | |
| grep -A 4 "tdesign_flutter_tools:" pubspec.yaml | |
| - name: Install pnpm | |
| uses: pnpm/action-setup@v2 | |
| with: | |
| version: 9 | |
| - name: Set Node version | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: "20" | |
| cache: "pnpm" | |
| cache-dependency-path: "tdesign-flutter/tdesign-site/pnpm-lock.yaml" | |
| - name: Set up Flutter | |
| uses: TDesignOteam/workflows/actions/setup-flutter@main | |
| with: | |
| flutter-version: 3.32.0 | |
| channel: stable | |
| cache: true | |
| aop-registry: true | |
| aop-project-path: ./tdesign-flutter/tdesign-component/example | |
| - name: Save PR number for publish workflow | |
| run: echo "PR_NUMBER=${{ github.event.number }}" > build-env.txt | |
| - name: Upload PR number | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: build-env | |
| path: build-env.txt | |
| retention-days: 5 | |
| - name: Generate site README via flutter test | |
| working-directory: tdesign-flutter/tdesign-component/example | |
| run: | | |
| # widget_test.dart 会遍历每个组件页面,生成 tdesign-site/src/<组件>/README.md | |
| # (内容已嵌入 example/assets/api/<组件>_api.md 的 API 表格) | |
| flutter test test/widget_test.dart --reporter expanded || \ | |
| echo "⚠ Widget test completed with warnings (expected)" | |
| - name: Build site | |
| working-directory: tdesign-flutter/tdesign-site | |
| run: | | |
| pnpm install --frozen-lockfile | |
| npm run site | |
| - name: Build flutter web demo | |
| working-directory: tdesign-flutter/tdesign-component/example | |
| run: flutter build web -t ./lib/main.dart --base-href /flutter/example/ --source-maps | |
| - name: Assemble preview artifact | |
| run: | | |
| mkdir -p tdesign-flutter-web/flutter/example | |
| # 站点页面(含组件文档,编译产物根目录指向 surge 根) | |
| cp -r tdesign-flutter/tdesign-site/_site/. tdesign-flutter-web/ | |
| # surge SPA 兜底 | |
| cp tdesign-flutter/tdesign-site/_site/index.html tdesign-flutter-web/404.html | |
| cp tdesign-flutter/tdesign-site/_site/index.html tdesign-flutter-web/200.html | |
| # flutter web demo | |
| cp -r tdesign-flutter/tdesign-component/example/build/web/. tdesign-flutter-web/flutter/example/ | |
| - uses: actions/upload-artifact@v4 | |
| with: | |
| name: tdesign-flutter-web | |
| path: ${{ github.workspace }}/tdesign-flutter-web | |
| retention-days: 5 |