@@ -301,6 +301,138 @@ jobs:
301301 run: dotnet test
302302` ` `
303303
304+ # ### Via Containers (dynamic)
305+ * langs: js
306+
307+ The Playwright Docker image tag can be resolved from the lockfile instead of hardcoding it in workflow YAML. This example uses [`lockparse`](https://www.npmjs.com/package/lockparse) to read `package-lock.json`, `yarn.lock`, `pnpm-lock.yaml`, or `bun.lock`.
308+
309+ Add `lockparse` as a dev dependency :
310+
311+ - ` npm install --save-dev lockparse`
312+ - ` yarn add --dev lockparse`
313+ - ` pnpm install --save-dev lockparse`
314+ - ` bun add --dev lockparse`
315+
316+ ` ` ` js title="scripts/resolve-playwright-version.mjs"
317+ import { existsSync } from 'node:fs';
318+ import { readFile } from 'node:fs/promises';
319+ import { parse } from 'lockparse';
320+
321+ const lockfileName = [
322+ 'package-lock.json',
323+ 'yarn.lock',
324+ 'pnpm-lock.yaml',
325+ 'bun.lock',
326+ ].find((file) => existsSync(file));
327+
328+ if (!lockfileName) {
329+ throw new Error('No supported lockfile found');
330+ }
331+
332+ const lockfile = await parse(
333+ await readFile(lockfileName, 'utf8'),
334+ lockfileName,
335+ JSON.parse(await readFile('package.json', 'utf8')),
336+ );
337+
338+ const playwrightTest = [
339+ ...(lockfile.root.dependencies ?? []),
340+ ...(lockfile.root.devDependencies ?? []),
341+ ].find((dependency) => dependency.name === '@playwright/test');
342+
343+ if (!playwrightTest) {
344+ throw new Error('No @playwright/test version found in lockfile');
345+ }
346+
347+ console.log(playwrightTest.version);
348+ ` ` `
349+
350+ ` ` ` yml js title=".github/workflows/playwright.yml"
351+ name: Playwright Tests
352+ on:
353+ push:
354+ branches: [ main, master ]
355+ pull_request:
356+ branches: [ main, master ]
357+ jobs:
358+ resolve-playwright-version:
359+ runs-on: ubuntu-latest
360+ outputs:
361+ version: ${{ steps.resolve-playwright-version.outputs.version }}
362+ steps:
363+ - uses: actions/checkout@v5
364+ - uses: actions/setup-node@v6
365+ with:
366+ node-version: lts/*
367+ - uses: oven-sh/setup-bun@v2
368+ if: ${{ hashFiles('bun.lock') != '' }}
369+ - name: Enable Corepack
370+ if: ${{ hashFiles('yarn.lock', 'pnpm-lock.yaml') != '' }}
371+ run: corepack enable
372+ - name: Install dependencies
373+ run: |
374+ if [ -f package-lock.json ]; then
375+ npm ci
376+ elif [ -f yarn.lock ]; then
377+ yarn install --immutable
378+ elif [ -f pnpm-lock.yaml ]; then
379+ pnpm install --frozen-lockfile
380+ elif [ -f bun.lock ]; then
381+ bun install --frozen-lockfile
382+ else
383+ echo "No supported lockfile found"
384+ exit 1
385+ fi
386+ - name: Resolve Playwright version from lockfile
387+ id: resolve-playwright-version
388+ run: echo "version=$(node scripts/resolve-playwright-version.mjs)" >> "$GITHUB_OUTPUT"
389+ playwright:
390+ name: 'Playwright Tests'
391+ runs-on: ubuntu-latest
392+ needs: resolve-playwright-version
393+ container:
394+ image: mcr.microsoft.com/playwright:v${{ needs.resolve-playwright-version.outputs.version }}-noble
395+ options: --user 1001
396+ steps:
397+ - uses: actions/checkout@v5
398+ - uses: actions/setup-node@v6
399+ with:
400+ node-version: lts/*
401+ - uses: oven-sh/setup-bun@v2
402+ if: ${{ hashFiles('bun.lock') != '' }}
403+ - name: Enable Corepack
404+ if: ${{ hashFiles('yarn.lock', 'pnpm-lock.yaml') != '' }}
405+ run: corepack enable
406+ - name: Install dependencies
407+ run: |
408+ if [ -f package-lock.json ]; then
409+ npm ci
410+ elif [ -f yarn.lock ]; then
411+ yarn install --immutable
412+ elif [ -f pnpm-lock.yaml ]; then
413+ pnpm install --frozen-lockfile
414+ elif [ -f bun.lock ]; then
415+ bun install --frozen-lockfile
416+ else
417+ echo "No supported lockfile found"
418+ exit 1
419+ fi
420+ - name: Run your tests
421+ run: |
422+ if [ -f package-lock.json ]; then
423+ npx playwright test
424+ elif [ -f yarn.lock ]; then
425+ yarn playwright test
426+ elif [ -f pnpm-lock.yaml ]; then
427+ pnpm exec playwright test
428+ elif [ -f bun.lock ]; then
429+ bunx playwright test
430+ else
431+ echo "No supported lockfile found"
432+ exit 1
433+ fi
434+ ` ` `
435+
304436# ### On deployment
305437
306438This will start the tests after a [GitHub Deployment](https://developer.github.com/v3/repos/deployments/) went into the `success` state.
0 commit comments