AI-native design editor. Open-source Figma alternative.
ci: capture the landing posters on four runners (#1209)
Danila Poyarkovbc02ce8ec378
diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.ymlindex fbec1651a..c6f003c66 100644--- a/.github/workflows/docs.yml+++ b/.github/workflows/docs.yml@@ -13,27 +13,100 @@ on:default: falsejobs:- deploy:+ build:# Pre-releases (vX.Y.Z-beta.N) ship desktop and npm builds only, never production,# including when dispatched manually on their tag.if: ${{ !(startsWith(github.ref, 'refs/tags/v') && contains(github.ref_name, '-')) }}runs-on: ubuntu-latestpermissions:contents: read- deployments: writesteps:- uses: actions/checkout@v7+ with:+ persist-credentials: false- uses: ./.github/actions/setup-bun- name: Build SDK packages for documentationrun: bun run build:packages- # The production build captures stills of the landing's stages in headless Chromium.+ # The demo document the landing's stages open draws its shader stills in headless Chromium.+ - name: Install Chromium for the demo document+ run: bunx playwright install --with-deps chromium++ - run: bun run --filter @open-pencil/docs build:site++ - uses: actions/upload-artifact@v7+ with:+ name: docs-site+ path: packages/docs/.vitepress/dist+ include-hidden-files: true+ retention-days: 1++ # Capturing the landing's stills renders every stage in software WebGL, about 80 s per page,+ # so the pages are spread over runners.+ posters:+ needs: build+ runs-on: ubuntu-latest+ permissions:+ contents: read+ strategy:+ matrix:+ shard: [1, 2, 3, 4]+ steps:+ - uses: actions/checkout@v7+ with:+ persist-credentials: false++ - uses: ./.github/actions/setup-bun+ with:+ cache-save: 'false'+- name: Install Chromium for landing postersrun: bunx playwright install --with-deps chromium- - run: bun run --filter @open-pencil/docs build:production+ - uses: actions/download-artifact@v8+ with:+ name: docs-site+ path: packages/docs/.vitepress/dist++ - run: bun tools/generate/landing-posters/src/generate.ts --shard ${{ matrix.shard }}/4++ - uses: actions/upload-artifact@v7+ with:+ name: landing-posters-${{ matrix.shard }}+ path: .cache/landing-posters+ include-hidden-files: true+ retention-days: 1++ deploy:+ needs: posters+ runs-on: ubuntu-latest+ permissions:+ contents: read+ deployments: write+ steps:+ - uses: actions/checkout@v7+ with:+ persist-credentials: false++ - uses: ./.github/actions/setup-bun+ with:+ cache-save: 'false'++ - uses: actions/download-artifact@v8+ with:+ name: docs-site+ path: packages/docs/.vitepress/dist++ - uses: actions/download-artifact@v8+ with:+ pattern: landing-posters-*+ merge-multiple: true+ path: .cache/landing-posters++ - name: Add the landing posters to the site+ run: bun tools/generate/landing-posters/src/generate.ts --assemble- name: Keep the landing posters for reviewif: ${{ inputs.dry-run }}diff --git a/packages/docs/package.json b/packages/docs/package.jsonindex 7c93e1443..dd06e2b3d 100644--- a/packages/docs/package.json+++ b/packages/docs/package.json@@ -10,7 +10,8 @@"scripts": {"dev": "vitepress dev","build": "OPENPENCIL_DOCS_FAST_BUILD=1 NODE_OPTIONS=--max-old-space-size=8192 vitepress build --minify=false --buildConcurrency=8",- "build:production": "OPENPENCIL_LANDING_POSTERS=1 NODE_OPTIONS=--max-old-space-size=8192 vitepress build --minify=esbuild --buildConcurrency=8 && bun ../../tools/generate/landing-posters/src/generate.ts",+ "build:production": "bun run build:site && bun ../../tools/generate/landing-posters/src/generate.ts",+ "build:site": "OPENPENCIL_LANDING_POSTERS=1 NODE_OPTIONS=--max-old-space-size=8192 vitepress build --minify=esbuild --buildConcurrency=8","check": "bun ../../tools/checks/docs/src/check.ts","preview": "vitepress preview"},diff --git a/tools/AGENTS.md b/tools/AGENTS.mdindex 5e8e0066c..4299163b2 100644--- a/tools/AGENTS.md+++ b/tools/AGENTS.md@@ -41,7 +41,7 @@ The `/demo` document is a tour of eight chapters on one page, each a section sta## Landing posters-`tools/generate/landing-posters/` captures WebP stills of every docs landing stage, per locale, theme, and layout, from a production docs build served locally in headless Chromium with software WebGL. `build:production` in `packages/docs` runs it after `vitepress build`; it reuses `.cache/landing-posters/<fingerprint>` when the inputs in `src/fingerprint.ts` are unchanged and copies the stills into `dist`, and `generate:landing-posters` forces a capture. The docs deploy workflow installs Chromium for it; dispatching it with `dry-run` builds and captures without deploying and keeps the stills as an artifact. A stage that starts depending on a new source tree adds it to `LANDING_POSTER_INPUTS`.+`tools/generate/landing-posters/` captures WebP stills of every docs landing stage, per locale, theme, and layout, from a production docs build served locally in headless Chromium with software WebGL. `build:production` in `packages/docs` runs it after `vitepress build`; it reuses `.cache/landing-posters/<fingerprint>` when the inputs in `src/fingerprint.ts` are unchanged and copies the stills into `dist`, and `generate:landing-posters` forces a capture. The docs deploy workflow spreads the capture over runners: one job runs `build:site`, four run the generator with `--shard i/4`, and the deploy job gathers their stills and runs it with `--assemble`, which fails on any missing still; dispatching it with `dry-run` builds and captures without deploying and keeps the stills as an artifact. A stage that starts depending on a new source tree adds it to `LANDING_POSTER_INPUTS`.## Brand assetsdiff --git a/tools/generate/landing-posters/src/capture.ts b/tools/generate/landing-posters/src/capture.tsindex 40601f898..1aff3420d 100644--- a/tools/generate/landing-posters/src/capture.ts+++ b/tools/generate/landing-posters/src/capture.ts@@ -1,12 +1,9 @@import { mkdir, stat } from 'node:fs/promises'import { dirname, join, relative, resolve } from 'node:path'-import { LOCALE_PREFIXES } from '#docs-config/seo'import { FEATURE_KINDS } from '#docs-config/theme/landing/content/features'import {POSTER_CAPTURE_PARAM,- POSTER_LAYOUTS,- POSTER_THEMES,posterParts,posterPath,type PosterLayout,@@ -97,24 +94,18 @@ async function capturePage(browser: Browser, origin: string, target: PosterTarge}/**- * Captures every stage of every landing page in each theme and layout from the built site in- * `dist`, writing WebP stills under `out` at their poster paths.+ * Captures every stage of the `targets` landing pages from the built site in `dist`, writing+ * WebP stills under `out` at their poster paths.*/export async function capturePosters({dist,out,- fingerprint+ targets}: {dist: stringout: string- fingerprint: string+ targets: PosterTarget[]}): Promise<number> {- const locales = ['root', ...LOCALE_PREFIXES]- const targets: PosterTarget[] = locales.flatMap((locale) =>- POSTER_THEMES.flatMap((theme) =>- POSTER_LAYOUTS.map((layout) => ({ fingerprint, locale, theme, layout }))- )- )const server = serveSite(dist)let browser: Browser | null = nulltry {diff --git a/tools/generate/landing-posters/src/generate.ts b/tools/generate/landing-posters/src/generate.tsindex b848368b3..21233a635 100644--- a/tools/generate/landing-posters/src/generate.ts+++ b/tools/generate/landing-posters/src/generate.ts@@ -4,6 +4,7 @@ import { fileURLToPath } from 'node:url'import { capturePosters } from '#landing-posters/capture'import { landingPosterFingerprint } from '#landing-posters/fingerprint'+import { parseShard, posterFiles, posterTargets, shardTargets } from '#landing-posters/targets'import { resolveWorkspaceRoot } from '@open-pencil/package-artifacts-tools/workspace'@@ -18,10 +19,19 @@ const exists = (path: string) =>() => false)+function argumentAfter(flag: string): string | null {+ const position = process.argv.indexOf(flag)+ return position === -1 ? null : (process.argv[position + 1] ?? '')+}+/*** Adds stage stills to a production docs build. The build names the fingerprint in each landing* page's data; this captures the stills from that build unless the cache already holds them for* the fingerprint, then copies them into the build. `--force` captures again.+ *+ * CI spreads the capture over runners: `--shard 2/4` captures only that part of the pages into+ * the cache, and `--assemble`, run once the parts are gathered in the cache, checks that every+ * still is there before copying them into the build.*/async function main(): Promise<void> {const root = await resolveWorkspaceRoot(fileURLToPath(import.meta.url))@@ -38,11 +48,40 @@ async function main(): Promise<void> {const cacheRoot = join(root, CACHE)const cached = join(cacheRoot, POSTERS, fingerprint)const complete = `${cached}.complete`+ const targets = posterTargets(fingerprint)++ const shard = argumentAfter('--shard')+ if (shard !== null) {+ const part = shardTargets(targets, parseShard(shard))+ await mkdir(cacheRoot, { recursive: true })+ await capturePosters({ dist, out: cacheRoot, targets: part })+ console.log(`landing posters ${fingerprint}: shard ${shard} captured ${part.length} pages`)+ return+ }++ if (process.argv.includes('--assemble')) {+ // Recapturing here would delete the gathered stills and redo every page on one runner.+ if (process.argv.includes('--force'))+ throw new Error('--force cannot be combined with --assemble')+ const files = targets.flatMap(posterFiles)+ const missing = (+ await Promise.all(+ files.map(async (file) => ((await exists(join(cacheRoot, file))) ? [] : [file]))+ )+ ).flat()+ if (missing.length > 0) {+ throw new Error(+ `${missing.length} of ${files.length} landing posters are missing, first ${missing[0]}`+ )+ }+ await writeFile(complete, `${targets.length}\n`)+ }+if (process.argv.includes('--force') || !(await exists(complete))) {// Stills of older sources are never served again.await rm(join(cacheRoot, POSTERS), { recursive: true, force: true })await mkdir(cacheRoot, { recursive: true })- const count = await capturePosters({ dist, out: cacheRoot, fingerprint })+ const count = await capturePosters({ dist, out: cacheRoot, targets })await writeFile(complete, `${count}\n`)}await cp(cached, join(dist, POSTERS, fingerprint), { recursive: true })diff --git a/tools/generate/landing-posters/src/targets.ts b/tools/generate/landing-posters/src/targets.tsnew file mode 100644index 000000000..e9b623d4a--- /dev/null+++ b/tools/generate/landing-posters/src/targets.ts@@ -0,0 +1,49 @@+import { LOCALE_PREFIXES } from '#docs-config/seo'+import { FEATURE_KINDS } from '#docs-config/theme/landing/content/features'+import {+ POSTER_LAYOUTS,+ POSTER_THEMES,+ posterParts,+ posterPath,+ type PosterTarget+} from '#docs-config/theme/landing/posters'++/** One of `count` parts of the capture, numbered from 1, so CI can spread it over runners. */+export interface PosterShard {+ index: number+ count: number+}++const SHARD = /^([1-9]\d*)\/([1-9]\d*)$/++/** Reads `index/count`, such as `2/4`. */+export function parseShard(text: string): PosterShard {+ const match = SHARD.exec(text)+ const index = Number(match?.[1])+ const count = Number(match?.[2])+ if (!match || index > count) {+ throw new Error(`Invalid shard "${text}": write index/count with 1 <= index <= count`)+ }+ return { index, count }+}++/** Every page captured: each locale's landing in each theme and layout. */+export function posterTargets(fingerprint: string): PosterTarget[] {+ return ['root', ...LOCALE_PREFIXES].flatMap((locale) =>+ POSTER_THEMES.flatMap((theme) =>+ POSTER_LAYOUTS.map((layout) => ({ fingerprint, locale, theme, layout }))+ )+ )+}++/** The pages one shard captures; the shards of a count together take every page once. */+export function shardTargets(targets: PosterTarget[], shard: PosterShard): PosterTarget[] {+ return targets.filter((_, position) => position % shard.count === shard.index - 1)+}++/** The site paths of every still captured of one page. */+export function posterFiles(target: PosterTarget): string[] {+ return FEATURE_KINDS.flatMap((kind) =>+ posterParts(kind).map((part) => posterPath(target, kind, part))+ )+}diff --git a/tools/generate/landing-posters/tests/targets.test.ts b/tools/generate/landing-posters/tests/targets.test.tsnew file mode 100644index 000000000..666ce765a--- /dev/null+++ b/tools/generate/landing-posters/tests/targets.test.ts@@ -0,0 +1,41 @@+import { describe, expect, test } from 'bun:test'++import { parseShard, posterFiles, posterTargets, shardTargets } from '#landing-posters/targets'++const FINGERPRINT = '0123456789abcdef'++describe('landing poster targets', () => {+ test('cover each locale in both themes and layouts', () => {+ const targets = posterTargets(FINGERPRINT)+ expect(targets).toContainEqual({+ fingerprint: FINGERPRINT,+ locale: 'root',+ theme: 'dark',+ layout: 'phone'+ })+ expect(targets.length % 4).toBe(0)+ })++ test('split into shards that together capture every page once', () => {+ const targets = posterTargets(FINGERPRINT)+ const shards = [1, 2, 3, 4].map((index) => shardTargets(targets, { index, count: 4 }))+ expect(shards.flat()).toHaveLength(targets.length)+ expect(new Set(shards.flat()).size).toBe(targets.length)+ for (const shard of shards) expect(shard.length).toBe(targets.length / 4)+ })++ test('read a shard as index/count and refuse anything else', () => {+ expect(parseShard('2/4')).toEqual({ index: 2, count: 4 })+ expect(() => parseShard('0/4')).toThrow()+ expect(() => parseShard('5/4')).toThrow()+ expect(() => parseShard('2')).toThrow()+ })++ test('list one still per stage part of a page', () => {+ const [target] = posterTargets(FINGERPRINT)+ if (!target) throw new Error('no targets')+ const files = posterFiles(target)+ expect(new Set(files).size).toBe(files.length)+ expect(files).toContain(`landing-posters/${FINGERPRINT}/root/light-desktop/collab-frame.webp`)+ })+})