diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f75541d..53f1276 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -45,3 +45,50 @@ jobs: - name: Verify the workspace run: pnpm check + + preview-artifact: + name: Build exact-head static preview + needs: quality + if: >- + github.event_name == 'pull_request' && + 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: 20 + steps: + - name: Checkout the exact pull-request head + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 + with: + ref: ${{ github.event.pull_request.head.sha }} + persist-credentials: false + + - name: Install the pinned pnpm release + uses: pnpm/action-setup@0ebf47130e4866e96fce0953f49152a61190b271 # v6.0.9 + with: + version: 11.17.0 + + - name: Set up Node + uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6 + with: + node-version: 22.22.3 + + - name: Install the frozen candidate toolchain + run: | + pnpm install --frozen-lockfile + + - name: Build the exact-head Histoire preview + run: pnpm story + + - name: Upload exact-head static preview + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a + with: + name: pages-preview-${{ github.event.pull_request.head.sha }} + path: packages/web/.histoire/dist + 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 d6b4172..15d675f 100644 --- a/.github/workflows/pages-preview.yml +++ b/.github/workflows/pages-preview.yml @@ -57,6 +57,8 @@ jobs: 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}` ) @@ -78,6 +80,8 @@ jobs: permissions: contents: read deployments: write + actions: read + pull-requests: read runs-on: ubuntu-latest timeout-minutes: 20 environment: @@ -91,13 +95,6 @@ jobs: path: controller persist-credentials: false - - name: Checkout the exact pull-request head - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - ref: ${{ needs.identity.outputs.head_sha }} - path: candidate - persist-credentials: false - - name: Install the pinned pnpm release uses: pnpm/action-setup@0ebf47130e4866e96fce0953f49152a61190b271 # v6.0.9 with: @@ -107,18 +104,57 @@ jobs: uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6 with: node-version: 22.22.3 - cache: pnpm - cache-dependency-path: | - candidate/pnpm-lock.yaml - controller/pnpm-lock.yaml - - name: Install the candidate and trusted delivery toolchains + - name: Install the trusted delivery toolchain run: | - pnpm --dir candidate install --frozen-lockfile pnpm --dir controller install --frozen-lockfile - - name: Build the exact-head Histoire preview - run: pnpm --dir candidate story + - name: Download the triggering run exact-head artifact + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c + with: + 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: Validate static preview files + env: + PREVIEW_DIST: ${{ runner.temp }}/pages-preview-artifact + run: | + 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: 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 the exact-head Histoire preview env: @@ -128,7 +164,7 @@ jobs: PREVIEW_HEAD_SHA: ${{ needs.identity.outputs.head_sha }} run: >- pnpm --dir controller exec wrangler pages deploy - "$GITHUB_WORKSPACE/candidate/packages/web/.histoire/dist" + "$RUNNER_TEMP/pages-preview-artifact" --project-name="${{ vars.CLOUDFLARE_PAGES_PROJECT }}" --branch="$PREVIEW_BRANCH" --commit-hash="$PREVIEW_HEAD_SHA"