playwright-ci-caching by aaronontheweb/dotnet-skills
npx skills add https://github.com/aaronontheweb/dotnet-skills --skill playwright-ci-caching在以下情况下使用此技能:
默认情况下,每次 CI 运行时都必须下载 Playwright 浏览器(约 400MB)。这会:
Directory.Packages.props(CPM)中提取,用作缓存键| 操作系统 | 路径 |
|---|---|
| Linux | ~/.cache/ms-playwright |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| macOS | ~/Library/Caches/ms-playwright |
| Windows | %USERPROFILE%\AppData\Local\ms-playwright |
- 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
对于在多个操作系统上运行的流水线:
- 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 }}
- 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'
创建一个 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-browsers-v1),则每次升级 Playwright 时都需要手动更新它,否则会出现难以理解的版本不匹配错误。
Directory.Packages.props 存在并包含 Playwright 包缓存的浏览器与 Playwright SDK 版本不匹配。这发生在以下情况:
修复:确保 Playwright 版本包含在缓存键中。
在运行脚本之前运行 dotnet build 或 dotnet restore。Playwright DLL 仅在 NuGet 还原后才存在。
此模式已在生产项目中经过实战测试:
dotnet-skills:playwright-blazor - 为 Blazor 应用程序编写 Playwright 测试dotnet-skills:project-structure - 中央包管理设置每周安装次数
70
仓库
GitHub 星标数
488
首次出现
2026年2月1日
安全审计
安装于
claude-code57
codex45
opencode42
github-copilot41
gemini-cli41
cursor40
Use this skill when:
Playwright browsers (~400MB) must be downloaded on every CI run by default. This:
Directory.Packages.props (CPM) to use as cache key| OS | Path |
|---|---|
| Linux | ~/.cache/ms-playwright |
| macOS | ~/Library/Caches/ms-playwright |
| Windows | %USERPROFILE%\AppData\Local\ms-playwright |
- 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
For workflows that run on multiple operating systems:
- 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 }}
- 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'
Create a build/playwright.ps1 script that discovers and runs the Playwright CLI. This abstracts away the Playwright CLI location which varies by project structure.
# build/playwright.ps1
# Discovers Microsoft.Playwright.dll and runs the bundled Playwright CLI
param(
[Parameter(ValueFromRemainingArguments = $true)]
[string[]]$Arguments
)
# Find the Playwright DLL (after 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
# Find the playwright CLI (path varies by OS and node version)
$playwrightCmd = Get-ChildItem -Path "$playwrightDir/.playwright/node" -Recurse -Filter "playwright.cmd" -ErrorAction SilentlyContinue |
Select-Object -First 1
if (-not $playwrightCmd) {
# Try Unix executable
$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
Usage:
# Install browsers
./build/playwright.ps1 install --with-deps
# Install specific browser
./build/playwright.ps1 install chromium
# Show installed browsers
./build/playwright.ps1 install --dry-run
This pattern assumes:
Central Package Management (CPM) with Directory.Packages.props:
<Project>
<ItemGroup>
<PackageVersion Include="Microsoft.Playwright" Version="1.40.0" />
</ItemGroup>
</Project>
Project has been built before running playwright.ps1 (so DLLs exist)
PowerShell available on CI agents (pre-installed on GitHub Actions and Azure DevOps)
Using the Playwright version in the cache key ensures:
If you hardcode the cache key (e.g., playwright-browsers-v1), you'll need to manually bump it every time you upgrade Playwright, or you'll get cryptic version mismatch errors.
Directory.Packages.props exists and has the Playwright packageThe cached browsers don't match the Playwright SDK version. This happens when:
Fix: Ensure the Playwright version is in the cache key.
Run dotnet build or dotnet restore before running the script. The Playwright DLL only exists after NuGet restore.
This pattern is battle-tested in production projects:
dotnet-skills:playwright-blazor - Writing Playwright tests for Blazor applicationsdotnet-skills:project-structure - Central Package Management setupWeekly Installs
70
Repository
GitHub Stars
488
First Seen
Feb 1, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
claude-code57
codex45
opencode42
github-copilot41
gemini-cli41
cursor40
Azure Data Explorer (Kusto) 查询技能:KQL数据分析、日志遥测与时间序列处理
130,600 周安装
Gemini CLI 技能:本地命令行工具,集成Google Gemini AI进行代码分析、头脑风暴与安全沙箱执行
98 周安装
CLAUDE.md 架构师技能:为软件项目生成和优化 AI 项目指令文件,提升 Claude 代码效率
98 周安装
PlantUML 语法参考大全:15+图表类型快速上手,从UML到C4架构图
98 周安装
React 19、Next.js 16、Vue 3.5 前端开发专家 - 现代Web应用与组件架构模式
98 周安装
Supabase RPC函数安全审计指南:发现RLS绕过与SQL注入漏洞
98 周安装
ScrapeNinja:高性能网络爬虫API,绕过反爬虫,支持JS渲染与代理轮换
98 周安装