在 CI/CD 中快取 Playwright 瀏覽器
何時使用此技能
適合以下情境:
- 為含有 Playwright E2E 測試的專案設定 CI/CD
- 建置時間因瀏覽器下載(約 400MB,1-2 分鐘)而過長
- 希望 Playwright 版本更新時自動使快取失效
- 使用 GitHub Actions 或 Azure DevOps 管線
問題
Playwright 瀏覽器(約 400MB)預設會在每次 CI 執行時下載,導致:
- 每次建置增加 1-2 分鐘
- 浪費頻寬
- 可能因暫時性網路問題失敗
- 拖慢 PR 回饋循環
核心模式
- 從
Directory.Packages.props(CPM)提取 Playwright 版本作為快取金鑰 - 根據平台路徑快取瀏覽器二進位檔
- 條件式安裝-僅在快取未命中時下載
- 自動快取失效-金鑰包含版本,升級套件時自動失效
各作業系統的快取路徑
| 作業系統 | 路徑 |
|---|---|
| Linux | ~/.cache/ms-playwright |
| macOS | ~/Library/Caches/ms-playwright |
| Windows | %USERPROFILE%\AppData\Local\ms-playwright |
GitHub Actions
- name: Get Playwright Version
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: Cache Playwright Browsers
id: playwright-cache
uses: actions/cache@v4
with:
path: ~/.cache/ms-playwright
key: ${{ runner.os }}-playwright-${{ env.PlaywrightVersion }}
- name: Install Playwright Browsers
if: steps.playwright-cache.outputs.cache-hit != 'true'
shell: pwsh
run: ./build/playwright.ps1 install --with-deps
多作業系統 GitHub Actions
針對在多個作業系統上執行的工作流程:
- name: Cache Playwright Browsers
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: 'Get Playwright Version'
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: 'Cache Playwright Browsers'
inputs:
key: 'playwright | "$(Agent.OS)" | $(PlaywrightVersion)'
path: '$(HOME)/.cache/ms-playwright'
cacheHitVar: 'PlaywrightCacheHit'
- task: PowerShell@2
displayName: 'Install Playwright Browsers'
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 not found. Run 'dotnet build' first."
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 "Playwright CLI not found in $playwrightDir/.playwright/node"
exit 1
}
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
先決條件
此模式假設:
-
使用中央套件管理 (CPM) 並有
Directory.Packages.props:<Project> <ItemGroup> <PackageVersion Include="Microsoft.Playwright" Version="1.40.0" /> </ItemGroup> </Project> -
在執行
playwright.ps1之前已建置過專案(確保 DLL 存在) -
CI 代理程式上可使用 PowerShell(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- 中央套件管理設定