Update playwright.yml #18
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Playwright Tests | |
on: | |
push: | |
paths-ignore: | |
- 'README.md' | |
branches: main | |
pull_request: | |
paths-ignore: | |
- 'README.md' | |
branches: main | |
permissions: | |
contents: write | |
jobs: | |
generate-matrix: | |
runs-on: ubuntu-latest | |
outputs: | |
matrix: ${{ steps.set-matrix.outputs.matrix }} | |
steps: | |
- uses: actions/checkout@v4 | |
- id: set-matrix | |
run: | | |
# 初始化一个空的数组来存储符合条件的文件夹 | |
folders=() | |
# 遍历所有以 'test' 开头的子目录 | |
for dir in test*; do | |
if [ -d "$dir" ]; then | |
# 检查子目录中是否存在 package.json 文件 | |
if [ -f "$dir/playwright.config.ts" ]; then | |
# 可选:检查 package.json 中是否包含 Playwright 依赖 | |
echo "Adding $dir to matrix (contains Playwright)" | |
folders+=("$dir") | |
else | |
echo "Skipping $dir (playwright.config.ts not found)" | |
fi | |
fi | |
done | |
# 如果没有符合条件的文件夹,设置矩阵为空数组 | |
if [ ${#folders[@]} -eq 0 ]; then | |
echo "No valid folders found." | |
folders_json='[]' | |
else | |
# 将文件夹数组转换为 JSON 格式 | |
folders_json=$(printf '%s\n' "${folders[@]}" | jq -R -s -c 'split("\n") | map(select(length > 0))') | |
fi | |
echo "Folders JSON: $folders_json" | |
echo "matrix={\"folder\": $folders_json}" >> $GITHUB_OUTPUT | |
test: | |
needs: generate-matrix | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: ${{ fromJson(needs.generate-matrix.outputs.matrix) }} | |
fail-fast: false | |
max-parallel: 4 | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: actions/setup-node@v4 | |
with: | |
node-version: lts/* | |
- name: Install dependencies | |
run: npm ci | |
working-directory: ${{ matrix.folder }} | |
- name: Install Playwright Browsers | |
run: npx playwright install --with-deps | |
working-directory: ${{ matrix.folder }} | |
- name: Run Playwright tests | |
run: npx playwright test | |
working-directory: ${{ matrix.folder }} | |
- name: List test-results directory | |
run: | | |
echo "Listing contents of ${{ matrix.folder }}/test-results/" | |
ls -R ${{ matrix.folder }}/test-results/ | |
- name: Collect zip files | |
run: | | |
mkdir -p zip-files/${{ matrix.folder }} | |
find ${{ matrix.folder }}/test-results/ -name '*.zip' -type f -exec cp {} zip-files/${{ matrix.folder }}/ \; | |
- name: List collected zip files | |
run: | | |
echo "Listing collected zip files in zip-files/${{ matrix.folder }}/" | |
ls -R zip-files/${{ matrix.folder }}/ | |
- uses: actions/upload-artifact@v4 | |
if: always() | |
with: | |
name: zip-files-${{ matrix.folder }} | |
path: zip-files/${{ matrix.folder }} | |
retention-days: 30 | |
# - uses: actions/upload-artifact@v4 | |
# if: always() | |
# with: | |
# name: playwright-report-${{ matrix.folder }} | |
# path: ${{ matrix.folder }}/playwright-report/ | |
# retention-days: 30 | |
deploy: | |
needs: test | |
runs-on: ubuntu-latest | |
if: needs.test.result == 'success' | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: actions/download-artifact@v4 | |
with: | |
# 下载所有工件 | |
path: all-zip-files/ | |
- name: List downloaded artifacts | |
run: | | |
echo "Listing contents of all-zip-files/" | |
ls -R all-zip-files/ | |
- name: Collect all zip files | |
run: | | |
mkdir -p deploy-zip-files | |
find all-zip-files/ -name '*.zip' -type f -exec cp {} deploy-zip-files/ \; | |
- name: List deploy-zip-files directory | |
run: | | |
echo "Listing contents of deploy-zip-files/" | |
ls -R deploy-zip-files/ | |
- name: Generate index.html | |
run: | | |
echo "<!DOCTYPE html><html><head><meta charset='UTF-8'><title>Playwright Traces</title><style> | |
body { | |
background-color: #2B313F; | |
color: #EAF4FC; | |
font-family: Arial, sans-serif; | |
} | |
h1 { | |
text-align: center; | |
margin-top: 50px; | |
} | |
ul { | |
list-style-type: none; | |
padding: 0; | |
} | |
li { | |
text-align: center; | |
font-size: 1.2em; | |
margin: 20px 0; | |
} | |
a { | |
color: #EAF4FC; | |
text-decoration: none; | |
padding: 10px 20px; | |
display: inline-block; | |
transition: all 0.3s ease; | |
} | |
a:hover { | |
box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.3); | |
} | |
</style></head><body><h1>Playwright Traces</h1><ul>" > deploy-zip-files/index.html | |
for file in deploy-zip-files/*.zip; do | |
filename=$(basename "$file") | |
url="https://${{ github.repository_owner }}.github.io/${{github.event.repository.name}}/$filename" | |
trace_url="https://trace.playwright.dev/?trace=$url" | |
echo "<li><a href='$trace_url'>$filename</a></li>" >> deploy-zip-files/index.html | |
done | |
echo "</ul></body></html>" >> deploy-zip-files/index.html | |
- name: Deploy to GitHub Pages | |
uses: peaceiris/actions-gh-pages@v3 | |
with: | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
publish_dir: deploy-zip-files/ |