Skip to content

Commit 3bfb608

Browse files
authored
fix: Optimizes Playwright workflow. (#1306)
* fix: Optimizes Playwright workflow. * Add top-level check for changes This commit simplifies the overall logic by adding a check for changed files at top, and using the boolean throughout the script. * Update Playwright workflow with env vars and failure handling Add environment variables and issue creation step for failures. * Add conditional skip for dist.sh execution
1 parent 4ee3648 commit 3bfb608

2 files changed

Lines changed: 55 additions & 15 deletions

File tree

.github/workflows/playwright.yml

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 Google LLC
1+
# Copyright 2026 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -33,9 +33,9 @@ on:
3333
schedule:
3434
- cron: "0 12 * * *"
3535

36-
#NEW
3736
env:
3837
FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true
38+
SKIP_DIST: ${{ github.event_name == 'pull_request' || github.event_name == 'schedule' }}
3939

4040
concurrency:
4141
group: ${{ github.workflow }}-${{ github.ref }}
@@ -61,39 +61,59 @@ jobs:
6161
run: |
6262
bash samples/find-changes.sh ${{ github.event_name == 'pull_request' && format('origin/{0}', github.base_ref) || github.event.before }}
6363
64+
- name: Check for changes
65+
id: check_changes
66+
run: |
67+
if [ "${{ steps.get_workspaces.outputs.changed_workspaces }}" != "" ]; then
68+
echo "HAS_CHANGES=true" >> $GITHUB_ENV
69+
else
70+
echo "HAS_CHANGES=false" >> $GITHUB_ENV
71+
fi
72+
6473
- name: Setup Node.js
74+
if: env.HAS_CHANGES == 'true'
6575
uses: actions/setup-node@v4
6676
with:
6777
node-version: '24.x'
6878

6979
- name: Cache npm dependencies
80+
if: env.HAS_CHANGES == 'true'
7081
uses: actions/cache@v4
7182
with:
7283
path: ~/.npm
7384
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
7485
restore-keys: |
7586
${{ runner.os }}-node-
7687
88+
- name: Get Playwright Version
89+
id: playwright-version
90+
if: env.HAS_CHANGES == 'true'
91+
run: |
92+
echo "version=$(node -p "require('./package.json').devDependencies['@playwright/test']")" >> $GITHUB_OUTPUT
93+
7794
- name: Cache Playwright browsers
7895
id: playwright-cache
96+
if: env.HAS_CHANGES == 'true'
7997
uses: actions/cache@v4
8098
with:
8199
path: ~/.cache/ms-playwright # Default Playwright cache path
82-
key: ${{ runner.os }}-playwright-${{ hashFiles('package-lock.json') }}-${{ hashFiles('playwright.config.ts') }}
100+
key: ${{ runner.os }}-playwright-${{ steps.playwright-version.outputs.version }}-${{ hashFiles('playwright.config.ts') }}
83101
restore-keys: |
84-
${{ runner.os }}-playwright-${{ hashFiles('package-lock.json') }}- # Fallback for lock file changes
102+
${{ runner.os }}-playwright-${{ steps.playwright-version.outputs.version }}- # Fallback for lock file changes
85103
${{ runner.os }}-playwright- # Broader fallback
86104
env:
87105
PLAYWRIGHT_BROWSERS_PATH: ~/.cache/ms-playwright
88106

89107
- name: Install dependencies
108+
if: env.HAS_CHANGES == 'true'
90109
run: npm install
91110

92111
- name: Install Playwright Browsers
93-
if: steps.playwright-cache.outputs.cache-hit != 'true'
94-
run: npx playwright install --with-deps
112+
if: steps.playwright-cache.outputs.cache-hit != 'true' && env.HAS_CHANGES == 'true'
113+
run: npx playwright install chromium --with-deps
95114

96115
- name: Build Changed Workspaces
116+
if: env.HAS_CHANGES == 'true'
97117
run: |
98118
IFS=$'\n'
99119
CHANGED_WORKSPACES_ARRAY=(${STEPS_GET_WORKSPACES_OUTPUTS_CHANGED_WORKSPACES})
@@ -109,13 +129,12 @@ jobs:
109129

110130
- name: Generate Index (Run Once After Builds)
111131
run: bash samples/generate-index.sh
112-
# Consider adding an 'if' condition if it only needs to run when 'samples/' changed at all.
113-
# if: |
114-
# steps.get_workspaces.outputs.changed_workspaces != '' || # If any workspace built
115-
# contains(github.event.pull_request.paths.*, 'samples/generate-index.sh') # Or if the script itself changed
132+
if: |
133+
env.HAS_CHANGES == 'true' ||
134+
contains(github.event.pull_request.paths.*, 'samples/generate-index.sh')
116135
117136
- name: Run All Playwright Tests
118-
if: steps.get_workspaces.outputs.changed_workspaces != ''
137+
if: env.HAS_CHANGES == 'true'
119138
run: npx playwright test e2e/samples.spec.ts
120139
env:
121140
# Pass the correct base reference for find-changes.sh when called by samples.spec.ts
@@ -133,6 +152,8 @@ jobs:
133152
name: Scheduled Full E2E Tests
134153
if: github.event_name == 'schedule'
135154
runs-on: ubuntu-latest
155+
permissions:
156+
issues: write
136157
steps:
137158
- uses: actions/checkout@v4
138159
with:
@@ -151,13 +172,19 @@ jobs:
151172
restore-keys: |
152173
${{ runner.os }}-node-
153174
175+
- name: Get Playwright Version
176+
id: playwright-version
177+
run: |
178+
echo "version=$(node -p "require('./package.json').devDependencies['@playwright/test']")" >> $GITHUB_OUTPUT
179+
154180
- name: Cache Playwright browsers
181+
id: playwright-cache
155182
uses: actions/cache@v4
156183
with:
157184
path: ~/.cache/ms-playwright # Default Playwright cache path
158-
key: ${{ runner.os }}-playwright-${{ hashFiles('package-lock.json') }}-${{ hashFiles('playwright.config.ts') }}
185+
key: ${{ runner.os }}-playwright-${{ steps.playwright-version.outputs.version }}-${{ hashFiles('playwright.config.ts') }}
159186
restore-keys: |
160-
${{ runner.os }}-playwright-${{ hashFiles('package-lock.json') }}- # Fallback for lock file changes
187+
${{ runner.os }}-playwright-${{ steps.playwright-version.outputs.version }}- # Fallback for lock file changes
161188
${{ runner.os }}-playwright- # Broader fallback
162189
163190
- name: Install dependencies
@@ -170,7 +197,8 @@ jobs:
170197
run: bash samples/generate-index.sh
171198

172199
- name: Install Playwright browsers with OS dependencies
173-
run: npx playwright install --with-deps
200+
if: steps.playwright-cache.outputs.cache-hit != 'true'
201+
run: npx playwright install chromium --with-deps
174202

175203
- name: Run Full E2E Tests (Scheduled)
176204
run: npx playwright test e2e/samples.spec.ts
@@ -183,3 +211,10 @@ jobs:
183211
with:
184212
name: Test Results
185213
path: test-results/
214+
215+
- name: Create Issue on Failure
216+
if: failure()
217+
run: |
218+
gh issue create --title "Nightly Playwright Tests Failed" --body "The nightly E2E tests failed. Please check the logs: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
219+
env:
220+
GH_TOKEN: ${{ github.token }}

samples/dist.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
# Copy/generate:
44
# - Vite build output for hosting
55

6+
if [ "$SKIP_DIST" = "true" ]; then
7+
echo ">>>Skipping dist.sh (SKIP_DIST is true)"
8+
exit 0
9+
fi
10+
611
echo ">>>Running dist.sh"
712

813
NAME=$1 # The name of the folder, taken from package.json "build" line.
@@ -17,4 +22,4 @@ SAMPLE_DIR="${PROJECT_ROOT}/dist/samples/${NAME}"
1722
echo "PROJECT_ROOT: ${PROJECT_ROOT}"
1823

1924
# Copy Vite output files to /dist/samples/${NAME}/dist
20-
cp -r "${SCRIPT_DIR}/${NAME}/dist" "${SAMPLE_DIR}"
25+
cp -r "${SCRIPT_DIR}/${NAME}/dist" "${SAMPLE_DIR}"

0 commit comments

Comments
 (0)