在 CI/CD 中缓存 Playwright 浏览器
何时使用此技能
在以下情况下使用此技能:
- 为包含 Playwright E2E 测试的项目设置 CI/CD
- 由于浏览器下载(约 400MB,1-2 分钟)导致构建时间变慢
- 希望在 Playwright 版本更改时自动使缓存失效
- 使用 GitHub Actions 或 Azure DevOps 管道
问题
默认情况下,Playwright 浏览器(约 400MB)必须在每次 CI 运行时下载。这会导致:
- 每次构建增加 1-2 分钟
- 浪费带宽
- 可能因临时网络问题而失败
- 减慢 PR 反馈循环
核心模式
- 提取 Playwright 版本,从
Directory.Packages.props(CPM) 中读取,用作缓存键 - 缓存浏览器二进制文件,使用适合平台的路径
- 条件安装 - 仅在缓存未命中时下载
- 自动缓存失效 - 缓存键包含版本,因此包升级会使缓存失效
各操作系统的缓存路径
| 操作系统 | 路径 |
|---|---|
| Linux | ~/.cache/ms-playwright |
| macOS | ~/Library/Caches/ms-playwright |
| Windows | %USERPROFILE%\AppData\Local\ms-playwright |
GitHub Actions
- name: 获取 Playwright 版本
shell: pwsh
run: |
$propsPath = "Directory.Packages.props"
[xml]$props = Get-Content $propsPath
$version = $props.Project.ItemGroup.PackageVersion |
Where-Object { $_.Include -eq "Microsoft.Playwright" } |
Select-Object -ExpandProperty Version
echo "PlaywrightVersion=$version" >> $env:GITHUB_ENV
- name: 缓存 Playwright 浏览器
id: playwright-cache
uses: actions/cache@v4
with:
path: ~/.cache/ms-playwright
key: ${{ runner.os }}-playwright-${{ env.PlaywrightVersion }}
- name: 安装 Playwright 浏览器
if: steps.playwright-cache.outputs.cache-hit != 'true'
shell: pwsh
run: ./build/playwright.ps1 install --with-deps
多操作系统 GitHub Actions
适用于在多个操作系统上运行的工作流:
- name: 缓存 Playwright 浏览器
id: playwright-cache
uses: actions/cache@v4
with:
path: |
~/.cache/ms-playwright
~/Library/Caches/ms-playwright
~/AppData/Local/ms-playwright
key: ${{ runner.os }}-playwright-${{ env.PlaywrightVersion }}
Azure DevOps
- task: PowerShell@2
displayName: '获取 Playwright 版本'
inputs:
targetType: 'inline'
script: |
[xml]$props = Get-Content "Directory.Packages.props"
$version = $props.Project.ItemGroup.PackageVersion |
Where-Object { $_.Include -eq "Microsoft.Playwright" } |
Select-Object -ExpandProperty Version
Write-Host "##vso[task.setvariable variable=PlaywrightVersion]$version"
- task: Cache@2
displayName: '缓存 Playwright 浏览器'
inputs:
key: 'playwright | "$(Agent.OS)" | $(PlaywrightVersion)'
path: '$(HOME)/.cache/ms-playwright'
cacheHitVar: 'PlaywrightCacheHit'
- task: PowerShell@2
displayName: '安装 Playwright 浏览器'
condition: ne(variables['PlaywrightCacheHit'], 'true')
inputs:
filePath: 'build/playwright.ps1'
arguments: 'install --with-deps'
辅助脚本:playwright.ps1
创建一个 build/playwright.ps1 脚本,用于发现并运行 Playwright CLI。这抽象了因项目结构而异的 Playwright CLI 位置。
# build/playwright.ps1
# 发现 Microsoft.Playwright.dll 并运行捆绑的 Playwright CLI
param(
[Parameter(ValueFromRemainingArguments = $true)]
[string[]]$Arguments
)
# 查找 Playwright DLL(在 dotnet build/restore 之后)
$playwrightDll = Get-ChildItem -Path . -Recurse -Filter "Microsoft.Playwright.dll" -ErrorAction SilentlyContinue |
Select-Object -First 1
if (-not $playwrightDll) {
Write-Error "未找到 Microsoft.Playwright.dll。请先运行 'dotnet build'。"
exit 1
}
$playwrightDir = $playwrightDll.DirectoryName
# 查找 playwright CLI(路径因操作系统和 node 版本而异)
$playwrightCmd = Get-ChildItem -Path "$playwrightDir/.playwright/node" -Recurse -Filter "playwright.cmd" -ErrorAction SilentlyContinue |
Select-Object -First 1
if (-not $playwrightCmd) {
# 尝试 Unix 可执行文件
$playwrightCmd = Get-ChildItem -Path "$playwrightDir/.playwright/node" -Recurse -Filter "playwright" -ErrorAction SilentlyContinue |
Where-Object { $_.Name -eq "playwright" } |
Select-Object -First 1
}
if (-not $playwrightCmd) {
Write-Error "在 $playwrightDir/.playwright/node 中未找到 Playwright CLI"
exit 1
}
Write-Host "正在使用 Playwright CLI: $($playwrightCmd.FullName)"
& $playwrightCmd.FullName @Arguments
用法:
# 安装浏览器
./build/playwright.ps1 install --with-deps
# 安装特定浏览器
./build/playwright.ps1 install chromium
# 显示已安装的浏览器
./build/playwright.ps1 install --dry-run
前提条件
此模式假设:
-
集中包管理 (CPM) 使用
Directory.Packages.props:<Project> <ItemGroup> <PackageVersion Include="Microsoft.Playwright" Version="1.40.0" /> </ItemGroup> </Project> -
项目已构建,然后再运行
playwright.ps1(以便 DLL 存在) -
PowerShell 在 CI 代理上可用(GitHub Actions 和 Azure DevOps 上预装)
为什么基于版本的缓存键很重要
在缓存键中使用 Playwright 版本可确保:
- 升级 Playwright 时自动失效
- 不存在与 SDK 版本不匹配的过时浏览器二进制文件
- 版本升级后无需手动清除缓存
如果硬编码缓存键(例如,playwright-browsers-v1),则每次升级 Playwright 时都需要手动更新,否则会出现晦涩的版本不匹配错误。
故障排除
缓存未使用
- 验证版本提取步骤是否输出正确的版本
- 检查缓存路径是否与您的操作系统匹配
- 确保
Directory.Packages.props存在并包含 Playwright 包
缓存命中后 "找不到浏览器"
缓存的浏览器与 Playwright SDK 版本不匹配。这发生在以下情况:
- 缓存键不包含版本
- 版本提取静默失败
解决方法:确保 Playwright 版本在缓存键中。
playwright.ps1 找不到 DLL
在运行脚本之前,先运行 dotnet build 或 dotnet restore。Playwright DLL 仅在 NuGet 恢复后才存在。
参考
此模式已在生产项目中经过实战测试:
相关技能
dotnet-skills:playwright-blazor- 为 Blazor 应用程序编写 Playwright 测试dotnet-skills:project-structure- 集中包管理设置