研发协作
CI/CD 中缓存 Playwright…
在 GitHub Actions 或 Azure DevOps 中运行 Playwright E2E 测试时,用 Playwright 版本号缓存浏览器二进制,避免每次构建 1-2 分钟下载开销。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 38/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 59/100 ×0.15
适用场景
CI/CD 中缓存 Playwright特定
解决思路
- 从 Directory.Packages.props(CPM)中提取 Playwright 版本,作为缓存 key。
- 按操作系统使用对应浏览器二进制缓存路径:Linux 为 ~/.cache/ms-playwright;macOS 为 ~/Library/Caches/ms-playwright;Windows 为 %USERPROFILE%\AppData\Local\ms-playwright。
- 缓存 Playwright 浏览器二进制文件。
- 只在缓存未命中时安装浏览器,避免每次下载。
- 在缓存 key 中包含 Playwright 版本,实现包升级后自动失效缓存。
- GitHub Actions:添加名为 Get Playwright Version 的步骤,shell: pwsh,解析 Directory.Packages.props 的 XML,在 Project.ItemGroup.PackageVersion 中查找 Include 等于 Microsoft.Playwright 的项并取 Version,将 PlaywrightVersion=$version 写入 $env:GITHUB_ENV。
- GitHub Actions:添加 id 为 playwright-cache 的 actions/cache@v4 步骤,path: ~/.cache/ms-playwright,key: ${{ runner.os }}-playwright-${{ env.PlaywrightVersion }}。
- GitHub Actions:添加 Install Playwright Browsers 步骤,if: steps.playwright-cache.outputs.cache-hit != 'true',shell: pwsh,run: ./build/playwright.ps1 install --with-deps。
- 多操作系统 GitHub Actions:在 actions/cache@v4 的 path 中同时列出 ~/.cache/ms-playwright、~/Library/Caches/ms-playwright、~/AppData/Local/ms-playwright,key 使用 ${{ runner.os }}-playwright-${{ env.PlaywrightVersion }}。
- Azure DevOps:添加 PowerShell@2 任务 Get Playwright Version,inline 脚本解析 Directory.Packages.props,查找 Microsoft.Playwright 版本,并用 Write-Host "##vso[task.setvariable variable=PlaywrightVersion]$version" 设置变量。
- Azure DevOps:添加 Cache@2 任务 Cache Playwright Browsers,key: 'playwright | "$(Agent.OS)" | $(PlaywrightVersion)',path: '$(HOME)/.cache/ms-playwright',cacheHitVar: 'PlaywrightCacheHit'。
- Azure DevOps:添加 PowerShell@2 任务 Install Playwright Browsers,condition: ne(variables['PlaywrightCacheHit'], 'true'),filePath: 'build/playwright.ps1',arguments: 'install --with-deps'。
- 创建 build/playwright.ps1 辅助脚本,用于发现并运行 Playwright CLI,以屏蔽不同项目结构中 CLI 位置差异。
- 在 playwright.ps1 中定义参数:param([Parameter(ValueFromRemainingArguments = $true)][string[]]$Arguments)。
- 在 playwright.ps1 中递归查找第一个 Microsoft.Playwright.dll;如果找不到,则 Write-Error "Microsoft.Playwright.dll not found. Run 'dotnet build' first." 并 exit 1。
- 在 playwright.ps1 中将 $playwrightDir 设置为 $playwrightDll.DirectoryName。
- 在 playwright.ps1 中到 "$playwrightDir/.playwright/node" 下递归查找 playwright.cmd;如果找不到,再查找 Unix 可执行文件并筛选 $_.Name -eq "playwright";都找不到则 Write-Error "Playwright CLI not found in $playwrightDir/.playwright/node" 并 exit 1。
- 在 playwright.ps1 中输出 Write-Host "Using Playwright CLI: $($playwrightCmd.FullName)",并用 & $playwrightCmd.FullName @Arguments 执行传入参数。
- 使用辅助脚本的方式包括:./build/playwright.ps1 install --with-deps;./build/playwright.ps1 install chromium;./build/playwright.ps1 install --dry-run。
- 使用基于版本的 cache key,确保升级 Playwright 时自动失效、不保留与 SDK 版本不匹配的旧浏览器二进制、无需手动清理缓存。
- 不要硬编码固定 cache key(例如 playwright-browsers-v1),否则每次升级 Playwright 都要手动 bump,否则会出现难懂的版本不匹配错误。
- 缓存未使用时:验证版本提取步骤输出正确版本;检查缓存路径是否匹配运行 OS;确认 Directory.Packages.props 存在且包含 Playwright 包。
- 缓存命中后出现 "Browser not found" 时:确认 cache key 包含 Playwright 版本;检查版本提取是否静默失败;修复方式是确保版本在 cache key 中。
- playwright.ps1 找不到 DLL 时:先运行 dotnet build 或 dotnet restore,因为 Microsoft.Playwright.dll 只在 NuGet restore 后存在。
适用边界 · 注意事项
- 项目未使用 Central Package Management(没有 Directory.Packages.props)时,不能按该技能提取版本作为缓存 key。
- CI agent 没有 PowerShell,或不在 GitHub Actions/Azure DevOps 环境时,需要改写为对应 shell/平台方案。
- 运行 build/playwright.ps1 前未执行 dotnet build 或 dotnet restore,导致 Microsoft.Playwright.dll 不存在时,脚本无法发现 Playwright CLI。