diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f2ab83a0..ddd5402b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -91,7 +91,8 @@ jobs: - name: Checkout uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: - fetch-depth: 0 + ref: ${{ github.sha }} + persist-credentials: false - name: Install the pinned pnpm release uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0 @@ -110,21 +111,8 @@ jobs: - name: 校验源代码格式、静态规则和类型 run: pnpm format:check && pnpm lint && pnpm type-check - - name: 读取待处理的版本计划 - id: build-versions - run: | - git show-ref --verify --quiet refs/heads/main || git branch main HEAD - pnpm exec changeset status --output "$RUNNER_TEMP/build-versions.json" - node -e 'console.log("needed=" + (require(process.argv[1]).releases.length > 0))' "$RUNNER_TEMP/build-versions.json" >> "$GITHUB_OUTPUT" - - - name: 在临时 checkout 中准备兼容构建版本 - if: steps.build-versions.outputs.needed == 'true' - run: | - pnpm release:version - pnpm install --lockfile-only --ignore-scripts - - name: 构建并校验相互兼容的 Host 与 MF 产物 - run: pnpm build + run: pnpm review:candidate build - name: 验证 SDK、运行时、采集与发布的独立契约 run: pnpm test:contracts @@ -138,6 +126,9 @@ jobs: steps: - name: Checkout uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: ${{ github.sha }} + persist-credentials: false - name: Install the pinned pnpm release uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0 @@ -164,7 +155,7 @@ jobs: password: ${{ secrets.GITHUB_TOKEN }} - name: 验证邮件阅读及独占浏览器与数据库链路 - run: pnpm test:e2e:web:with-mail + run: pnpm review:candidate web-e2e env: INKCRE_CORE_IMAGE: ${{ needs.core-release.outputs.image }} @@ -188,6 +179,9 @@ jobs: steps: - name: Checkout uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: ${{ github.sha }} + persist-credentials: false - name: Install the pinned pnpm release uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0 @@ -214,7 +208,7 @@ jobs: password: ${{ secrets.GITHUB_TOKEN }} - name: 验证浏览器扩展产物与独占数据库采集、重启恢复 - run: pnpm test:e2e:webext + run: pnpm review:candidate webext-e2e env: INKCRE_CORE_IMAGE: ${{ needs.core-release.outputs.image }} diff --git a/.github/workflows/pages-preview-build.yml b/.github/workflows/pages-preview-build.yml new file mode 100644 index 00000000..86775254 --- /dev/null +++ b/.github/workflows/pages-preview-build.yml @@ -0,0 +1,86 @@ +name: Web Pages preview build + +on: + pull_request: + branches: [main] + types: [opened, reopened, synchronize, ready_for_review] + +permissions: + contents: read + +concurrency: + group: pages-preview-build-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + build: + if: >- + github.event.pull_request.state == 'open' && + github.event.pull_request.base.ref == 'main' && + github.event.pull_request.base.repo.full_name == github.repository && + github.event.pull_request.head.repo.full_name == github.repository + permissions: + contents: read + runs-on: ubuntu-latest + timeout-minutes: 25 + steps: + - name: Checkout the exact pull-request head + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: ${{ github.event.pull_request.head.sha }} + path: candidate + persist-credentials: false + + - name: Install the pinned pnpm release + uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0 + with: + version: 11.11.0 + + - name: Set up Node + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version-file: candidate/package.json + cache: pnpm + cache-dependency-path: candidate/pnpm-lock.yaml + + - name: Set up Python and PDM + uses: pdm-project/setup-pdm@544d7237314ee09c256785bd360f6b30add38b37 # v4.6 + with: + python-version-file: candidate/.python-version + version: 2.28.0 + cache: true + cache-dependency-path: candidate/pdm.lock + + - name: Install frozen preview toolchains + working-directory: candidate + run: | + pnpm install --frozen-lockfile + pdm install --frozen-lockfile + env: + PDM_CHECK_UPDATE: 'false' + + - name: Build the exact-head SPA and Module Federation snapshots + working-directory: candidate + run: pnpm review:candidate build + + - name: Build the same-origin Preview Registry facade + working-directory: candidate + env: + PREVIEW_ORIGIN: https://preview-client-web-pr-${{ github.event.pull_request.number }}.${{ vars.CLOUDFLARE_PAGES_PROJECT }}.pages.dev + run: | + pdm run inkcre-ext preview build \ + --inventory .github/preview/extensions.json \ + --public-origin "$PREVIEW_ORIGIN" \ + --output .preview-registry + cp -R .preview-registry/. apps/web/dist/ + cp -R apps/web/dist "$GITHUB_WORKSPACE/.pages-preview" + + - name: Upload exact-head static preview + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 + with: + name: pages-preview-${{ github.event.pull_request.head.sha }} + path: .pages-preview + if-no-files-found: error + include-hidden-files: true + overwrite: true + retention-days: 3 diff --git a/.github/workflows/pages-preview.yml b/.github/workflows/pages-preview.yml index 24de6b82..aaa5153c 100644 --- a/.github/workflows/pages-preview.yml +++ b/.github/workflows/pages-preview.yml @@ -1,24 +1,24 @@ name: Pages preview on: - pull_request_target: - branches: - - main + workflow_run: + workflows: + - Web Pages preview build types: - - opened - - reopened - - synchronize + - completed permissions: + actions: read contents: read + pull-requests: read jobs: identity: name: Resolve trusted preview identity if: >- - github.event.pull_request.base.ref == 'main' && - github.event.pull_request.base.repo.full_name == github.repository && - github.event.pull_request.head.repo.full_name == github.repository + github.event.workflow_run.conclusion == 'success' && + github.event.workflow_run.event == 'pull_request' && + github.event.workflow_run.head_repository.full_name == github.repository runs-on: ubuntu-latest outputs: branch: ${{ steps.identity.outputs.branch }} @@ -30,19 +30,45 @@ jobs: uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 with: script: | - const pull = context.payload.pull_request - const repository = `${context.repo.owner}/${context.repo.repo}` - if ( - pull.state !== 'open' || - pull.base.ref !== 'main' || - pull.base.repo.full_name !== repository || - pull.head.repo?.full_name !== repository - ) { - core.setFailed('Preview delivery only accepts an open same-repository PR targeting main.') + const runId = context.payload.workflow_run.id + const { data: run } = await github.rest.actions.getWorkflowRun({ + owner: context.repo.owner, + repo: context.repo.repo, + run_id: runId, + }) + if (run.name !== 'Web Pages preview build' || run.path !== '.github/workflows/pages-preview-build.yml') { + core.setFailed('Preview must be orchestrated by the trusted Web Pages preview build workflow.') + return + } + if (run.conclusion !== 'success' || run.event !== 'pull_request') { + core.setFailed('Preview delivery requires a successful pull-request check run.') + return + } + if (run.head_repository?.full_name !== `${context.repo.owner}/${context.repo.repo}`) { + core.setFailed('Fork pull requests are not eligible for preview credentials.') + return + } + + const pulls = await github.rest.repos.listPullRequestsAssociatedWithCommit({ + owner: context.repo.owner, + repo: context.repo.repo, + commit_sha: run.head_sha, + }) + const pull = pulls.data.find((candidate) => + candidate.state === 'open' && + candidate.base.ref === 'main' && + candidate.base.repo.full_name === `${context.repo.owner}/${context.repo.repo}` && + run.pull_requests.some((item) => item.number === candidate.number) && + candidate.head.sha === run.head_sha && + candidate.head.repo?.full_name === `${context.repo.owner}/${context.repo.repo}` + ) + if (!pull) { + core.setFailed('Run is not the exact head of an eligible internal pull request.') return } + core.setOutput('branch', `preview/client-web/pr-${pull.number}`) - core.setOutput('head_sha', pull.head.sha) + core.setOutput('head_sha', run.head_sha) core.setOutput('pull_number', String(pull.number)) deploy: @@ -54,6 +80,8 @@ jobs: permissions: contents: read deployments: write + actions: read + pull-requests: read runs-on: ubuntu-latest timeout-minutes: 25 environment: @@ -67,77 +95,65 @@ jobs: path: controller persist-credentials: false - - name: Checkout the exact pull-request head - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Set up trusted Node + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 with: - ref: ${{ needs.identity.outputs.head_sha }} - path: candidate - fetch-depth: 0 - persist-credentials: false + node-version-file: controller/package.json - - name: Install the pinned pnpm release - uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0 - with: - version: 11.11.0 - - - name: Set up Node - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 - with: - node-version-file: candidate/package.json - cache: pnpm - cache-dependency-path: candidate/pnpm-lock.yaml + - name: Prepare isolated delivery tool directory + run: mkdir --parents "$RUNNER_TEMP/pages-preview-tool" - - name: Set up Python and PDM - uses: pdm-project/setup-pdm@544d7237314ee09c256785bd360f6b30add38b37 # v4.6 + - name: Download the triggering run exact-head artifact + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 with: - python-version-file: candidate/.python-version - version: 2.28.0 - cache: true - cache-dependency-path: candidate/pdm.lock + github-token: ${{ github.token }} + run-id: ${{ github.event.workflow_run.id }} + name: pages-preview-${{ needs.identity.outputs.head_sha }} + path: ${{ runner.temp }}/pages-preview-artifact - - name: Install frozen preview toolchains - working-directory: candidate - run: | - pnpm install --frozen-lockfile - pdm install --frozen-lockfile - env: - PDM_CHECK_UPDATE: 'false' - - - name: Inspect pending preview versions - id: preview-versions - working-directory: candidate - run: | - git show-ref --verify --quiet refs/heads/main || git branch main HEAD - pnpm exec changeset status --output "$RUNNER_TEMP/preview-versions.json" - node -e 'console.log("needed=" + (require(process.argv[1]).releases.length > 0))' "$RUNNER_TEMP/preview-versions.json" >> "$GITHUB_OUTPUT" - - - name: Prepare preview versions in the temporary checkout - if: steps.preview-versions.outputs.needed == 'true' - working-directory: candidate - run: | - pnpm release:version - pnpm install --lockfile-only --ignore-scripts - - - name: Build the exact-head SPA and Module Federation snapshots - working-directory: candidate - run: pnpm build - - - name: Build the same-origin Preview Registry facade - working-directory: candidate + - name: Validate static preview files env: - PREVIEW_ORIGIN: https://preview-client-web-pr-${{ needs.identity.outputs.pull_number }}.${{ vars.CLOUDFLARE_PAGES_PROJECT }}.pages.dev + PREVIEW_DIST: ${{ runner.temp }}/pages-preview-artifact run: | - pdm run inkcre-ext preview build \ - --inventory .github/preview/extensions.json \ - --public-origin "$PREVIEW_ORIGIN" \ - --output .preview-registry - cp -R .preview-registry/. apps/web/dist/ - cp -R apps/web/dist "$GITHUB_WORKSPACE/.pages-preview" + python3 - <<'PYCODE' + import os + import stat + from pathlib import Path + root = Path(os.environ['PREVIEW_DIST']) + for directory, dirs, files in os.walk(root, followlinks=False): + for name in dirs + files: + path = Path(directory) / name + mode = path.lstat().st_mode + if stat.S_ISLNK(mode) or not (stat.S_ISDIR(mode) or stat.S_ISREG(mode)): + raise SystemExit(f'Unsupported static artifact entry: {path}') + if name == '_worker.js': + raise SystemExit(f'Pages Worker code is forbidden: {path}') + if not (root / 'index.html').is_file(): + raise SystemExit('Static preview requires index.html') + PYCODE - name: Bind Module Federation assets to the selected snapshot env: INKCRE_PREVIEW_SOURCE_SHA: ${{ needs.identity.outputs.head_sha }} - run: node controller/scripts/prepare-pages-preview.mjs "$GITHUB_WORKSPACE/.pages-preview" + run: node controller/scripts/prepare-pages-preview.mjs "$RUNNER_TEMP/pages-preview-artifact" + + - name: Reverify the current pull request before delivery + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 + env: + PREVIEW_PULL_NUMBER: ${{ needs.identity.outputs.pull_number }} + PREVIEW_HEAD_SHA: ${{ needs.identity.outputs.head_sha }} + with: + script: | + const { data: pull } = await github.rest.pulls.get({ + ...context.repo, + pull_number: Number(process.env.PREVIEW_PULL_NUMBER), + }) + const repository = `${context.repo.owner}/${context.repo.repo}` + if (pull.state !== 'open' || pull.head.sha !== process.env.PREVIEW_HEAD_SHA || + pull.base.ref !== 'main' || pull.base.repo.full_name !== repository || + pull.head.repo?.full_name !== repository) { + core.setFailed('The current PR no longer matches the exact-head preview build.') + } - name: Deploy to the isolated Pages preview branch id: pages @@ -145,11 +161,11 @@ jobs: with: apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} - packageManager: pnpm + packageManager: npm wranglerVersion: 4.114.0 - workingDirectory: candidate + workingDirectory: ${{ runner.temp }}/pages-preview-tool command: >- - pages deploy ../.pages-preview + pages deploy ${{ runner.temp }}/pages-preview-artifact --project-name=${{ vars.CLOUDFLARE_PAGES_PROJECT }} --branch=${{ needs.identity.outputs.branch }} --commit-hash=${{ needs.identity.outputs.head_sha }} diff --git a/docs/40-deployment/web-delivery.md b/docs/40-deployment/web-delivery.md index f16865f5..0907053f 100644 --- a/docs/40-deployment/web-delivery.md +++ b/docs/40-deployment/web-delivery.md @@ -14,6 +14,12 @@ validates the candidate but uploads no deployable SPA, Module Federation, or Reg artifact. Preview and production each build at their own authority boundary and cannot publish first-party Extensions. +Workspace compilation, Web and WebExt browser acceptance, and Pages preview start with +`pnpm review:candidate` in a clean detached checkout. The command applies that checkout's +pending Changesets and refreshes workspace dependencies before dispatching to the requested +build or acceptance. App checks use the event commit and Pages preview uses the exact PR head. +Release workflows use the committed release versions. + The browser-side authority behind this invariant belongs to [App Runtime and Delegation](../30-unit-tdd/app-runtime-and-delegation.md). @@ -127,14 +133,18 @@ identity; never substitute the current controller SHA or overwrite an existing t ## Pull-request Preview -[`.github/workflows/pages-preview.yml`](../../.github/workflows/pages-preview.yml) owns previews for -open, same-repository pull requests targeting `main`; fork pull requests are ineligible. The -`pull_request_target` controller checks out its trusted default-branch revision separately from the -exact candidate head, installs the candidate's frozen pnpm and PDM environments, builds its SPA and -selected Module Federation snapshots, and uses `inkcre-ext preview build` with the explicit -`.github/preview/extensions.json` inventory to add a same-origin static Registry facade. The -current inventory includes the CalDAV, GitHub, Twitter and Mail Module Federation distributions. The controller uses the PR head selected by the identity job through staging and deployment. - +[`.github/workflows/pages-preview-build.yml`](../../.github/workflows/pages-preview-build.yml) +在普通 `pull_request` 低权限作业中,为指向 `main` 的同仓开放 PR 构建精确 head;fork PR 不参与预览。 +该作业安装候选的冻结 pnpm/PDM 环境,通过 `pnpm review:candidate build` 构建 SPA 和 Module Federation +快照,并用 `inkcre-ext preview build` 加入 `.github/preview/extensions.json` 指定的同源静态 Registry facade。 +当前清单包括 CalDAV、GitHub、Twitter 和 Mail。`PREVIEW_ORIGIN` 来自 PR +编号及仓库级非秘密变量 `CLOUDFLARE_PAGES_PROJECT`,静态输出按 head SHA 命名并上传为短期 artifact。 + +[`.github/workflows/pages-preview.yml`](../../.github/workflows/pages-preview.yml) 由成功的 +`Web Pages preview build` 的 `workflow_run` 触发,仅 checkout 可信控制器。它核对工作流路径、同仓 +PR 及精确 head,仅下载触发 run 的同名 artifact,拒绝符号链接、非常规文件及 `_worker.js`,然后运行 +可信快照绑定并在独立工具目录部署静态文件。部署前再次核对 PR 的 open、base、repo 和 head。 +候选的代码、依赖、配置和缓存不会进入有部署权限的执行路径。 The Toolkit places read-only native Extension snapshots and preview Releases beside the exact-head app build, using the stable alias branch `preview/client-web/pr-`. This is a preview projection, not Registry publication: the workflow has no @@ -142,8 +152,7 @@ projection, not Registry publication: the workflow has no through Pages and reports the provider deployment result. The GitHub environment links to the Pages branch alias; the job summary includes that alias, the deployment-specific URL, and the exact source SHA. This reporting reads Wrangler outputs without adding network acceptance checks. -Because `pull_request_target` uses the trusted base workflow, reporting changes take effect after -they reach `main`; they cannot rewrite summaries of completed runs. It does not turn edge propagation, +`workflow_run` 使用默认分支上的可信部署流程,因此报告逻辑变更到达 `main` 后才生效,不会重写已完成 run 的摘要。 It does not turn edge propagation, deep route traversal, cache behavior, or byte comparison into synchronous delivery gates; consumer acceptance is a separate black-box activity. diff --git a/package.json b/package.json index 14ad1d18..b75d0ddf 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "contract:check": "node scripts/sync-database-contract.mjs --check", "changeset": "changeset", "release:version": "changeset version", + "review:candidate": "node scripts/review-candidate.mjs", "release:webext:build": "node scripts/webext-release.mjs build", "release:webext:check": "node scripts/webext-release.mjs check", "dev:all": "concurrently \"pnpm dev\" \"pnpm -r --filter './extensions/*' dev\"", @@ -50,6 +51,8 @@ "devDependencies": { "@axe-core/playwright": "^4.14.0", "@changesets/cli": "3.0.0", + "@changesets/pre": "3.0.0", + "@changesets/read": "1.0.0", "@playwright/test": "1.61.1", "@tanstack/intent": "0.3.6", "@types/semver": "7.5.8", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f337078a..da921f62 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -53,6 +53,12 @@ importers: '@changesets/cli': specifier: 3.0.0 version: 3.0.0 + '@changesets/pre': + specifier: 3.0.0 + version: 3.0.0 + '@changesets/read': + specifier: 1.0.0 + version: 1.0.0 '@playwright/test': specifier: 1.61.1 version: 1.61.1 diff --git a/scripts/check-local-runtime-contract.mjs b/scripts/check-local-runtime-contract.mjs index a1bb2ee3..a7327583 100644 --- a/scripts/check-local-runtime-contract.mjs +++ b/scripts/check-local-runtime-contract.mjs @@ -42,7 +42,8 @@ if ( for (const [path, expectedNodeFile] of [ ['.github/workflows/ci.yml', 'package.json'], ['.github/workflows/pages-cleanup.yml', 'controller/package.json'], - ['.github/workflows/pages-preview.yml', 'candidate/package.json'], + ['.github/workflows/pages-preview-build.yml', 'candidate/package.json'], + ['.github/workflows/pages-preview.yml', 'controller/package.json'], ['.github/workflows/pages-deploy.yml', 'release/package.json'], ]) { const workflow = parse(await readFile(`${repoRoot}/${path}`, 'utf8')) diff --git a/scripts/review-candidate.mjs b/scripts/review-candidate.mjs new file mode 100644 index 00000000..22d608a1 --- /dev/null +++ b/scripts/review-candidate.mjs @@ -0,0 +1,37 @@ +import { execFileSync } from 'node:child_process' +import { fileURLToPath } from 'node:url' +import { readPreState } from '@changesets/pre' +import { readChangesets } from '@changesets/read' + +const repoRoot = fileURLToPath(new URL('..', import.meta.url)) +const workspaceScripts = new Map([ + ['build', 'build'], + ['web-e2e', 'test:e2e:web:with-mail'], + ['webext-e2e', 'test:e2e:webext'], +]) +const workspaceScript = workspaceScripts.get(process.argv[2]) +if (!workspaceScript || process.argv.length !== 3) { + throw new Error('Usage: pnpm review:candidate ') +} + +function git(...args) { + return execFileSync('git', args, { cwd: repoRoot, encoding: 'utf8' }).trim() +} + +function pnpm(...args) { + execFileSync('pnpm', args, { cwd: repoRoot, stdio: 'inherit' }) +} + +if (git('rev-parse', '--abbrev-ref', 'HEAD') !== 'HEAD' || git('status', '--porcelain')) { + throw new Error('Review candidates require a clean, detached checkout of the source commit') +} + +const [changesets, preState] = await Promise.all([readChangesets(repoRoot), readPreState(repoRoot)]) +if (changesets.length > 0 || preState?.mode === 'exit') { + pnpm('release:version') + if (git('diff', '--name-only', 'HEAD', '--', 'package.json', ':(glob)**/package.json')) { + pnpm('install', '--no-frozen-lockfile', '--ignore-scripts') + } +} + +pnpm(workspaceScript)