GitHub Actions setup, parallel execution, sharding, test filtering, artifact management, and pipeline optimization for Playwright tests
CI/CD Integration Skill
Overview
Running Playwright tests in CI/CD is essential for catching regressions before they reach production. This skill covers GitHub Actions configuration, parallel execution, sharding, test filtering, report management, and pipeline optimization patterns.
// playwright.config.tsimport { defineConfig } from'@playwright/test';
exportdefaultdefineConfig({
// Number of parallel workers// CI: use fewer workers to avoid resource contentionworkers: process.env.CI ? 2 : undefined,
// Fail the build after N test failures (saves CI time)maxFailures: process.env.CI ? 10 : undefined,
// Retry failed tests in CI (catch flaky tests)retries: process.env.CI ? 2 : 0,
// Reporter configurationreporter: process.env.CI
? [['html', { open: 'never' }], ['github'], ['list']]
: [['html', { open: 'on-failure' }]],
});
Test Filtering in CI
5. Run Only Changed Tests on PR
# .github/workflows/playwright-changed.ymlname:Playwright-ChangedTestsOnlyon:pull_request:branches: [main]
jobs:test-changed:runs-on:ubuntu-lateststeps:-uses:actions/checkout@v4with:fetch-depth:0# Need full history for diff-uses:actions/setup-node@v4with:node-version:20cache:'npm'-run:npmci-run:npxplaywrightinstall--with-depschromium-name:Getchangedtestfilesid:changedrun:|
FILES=$(git diff --name-only origin/main...HEAD -- '*.spec.ts' '*.test.ts' | tr '\n' ' ')
echo "files=$FILES" >> $GITHUB_OUTPUT
echo "Changed test files: $FILES"
-name:Runchangedtestsif:steps.changed.outputs.files!=''run:npxplaywrighttest${{steps.changed.outputs.files}}-name:Notestschangedif:steps.changed.outputs.files==''run:echo"No test files changed in this PR"
6. Test Tags for Selective Execution
// Tag tests for selective CI executiontest('user login @smoke @auth', async ({ page }) => {
// This test runs in smoke suite
});
test('complex report generation @regression', async ({ page }) => {
// This test only runs in full regression
});
test('payment flow @critical @payment', async ({ page }) => {
// This test runs in critical path suite
});
# Run only smoke tests on every PR-name:Runsmoketestsrun:npxplaywrighttest--grep@smoke# Run full regression nightly-name:Runregressionrun:npxplaywrighttest--grep@regression# Run critical path before deploy-name:Runcriticaltestsrun:npxplaywrighttest--grep@critical
7. Run Failed Tests from Last Run
# Re-run only tests that failed in the previous run
npx playwright test --last-failed
# Combine with retries
npx playwright test --last-failed --retries=2
Artifacts & Reports
8. Upload Traces on Failure
// playwright.config.tsexportdefaultdefineConfig({
use: {
// Collect trace on first retry of a failed testtrace: 'on-first-retry',
// Screenshot on failurescreenshot: 'only-on-failure',
// Video on first retryvideo: 'on-first-retry',
},
});
# .github/workflows/nightly.ymlname:NightlyRegressionon:schedule:-cron:'0 2 * * *'# 2 AM UTC dailyworkflow_dispatch:# Allow manual triggerjobs:regression:runs-on:ubuntu-latesttimeout-minutes:60strategy:fail-fast:falsematrix:project: [chromium, firefox, webkit]
steps:-uses:actions/checkout@v4-uses:actions/setup-node@v4with:node-version:20cache:'npm'-run:npmci-run:npxplaywrightinstall--with-deps-name:Runfullregression(${{matrix.project}})run:npxplaywrighttest--project=${{matrix.project}}-name:Uploadreportuses:actions/upload-artifact@v4if:${{!cancelled()}}with:name:report-${{matrix.project}}path:playwright-report/retention-days:30
15. Deploy Preview Testing
# Test against Vercel/Netlify preview deploymentsname:PreviewTestson:deployment_status:jobs:test:if:github.event.deployment_status.state=='success'runs-on:ubuntu-lateststeps:-uses:actions/checkout@v4-uses:actions/setup-node@v4with:node-version:20cache:'npm'-run:npmci-run:npxplaywrightinstall--with-depschromium-name:Runtestsagainstpreviewrun:npxplaywrighttest--grep@smokeenv:BASE_URL:${{github.event.deployment_status.target_url}}
16. Status Checks & PR Comments
-name:AddtestresultstoPRif:github.event_name=='pull_request'&&!cancelled()uses:daun/playwright-report-summary@v3with:report-file:playwright-report/results.jsoncomment-title:'Playwright Test Results'
# After test run, check for flaky tests (passed on retry)-name:Checkforflakytestsif:${{!cancelled()}}run:|
if grep -q '"status":"flaky"' playwright-report/results.json 2>/dev/null; then
echo "::warning::Flaky tests detected! Check the report for details."
fi