-
Notifications
You must be signed in to change notification settings - Fork 7
364 lines (320 loc) · 14.4 KB
/
Copy pathpr-quality-standalone.yml
File metadata and controls
364 lines (320 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
name: PR Quality Gate - Standalone
on:
pull_request:
branches: ['leader']
workflow_dispatch:
inputs:
branch:
description: 'Branch to check'
required: true
default: 'leader'
concurrency:
group: pr-quality-standalone-${{ github.event.pull_request.number || github.ref || github.run_id }}
cancel-in-progress: true
permissions:
contents: read
checks: write # Required for dorny/test-reporter
actions: read # Required for artifact downloads
pull-requests: write # Required for gh pr comment
issues: write # Required for posting comments
env:
FORCE_COLOR: 1
# Unique session identifier for concurrent runs
SESSION_ID: ${{ github.event.pull_request.number || github.ref_name || github.run_id }}
# Maximum bytes to include from each log file in failure comments
LOG_TRUNCATE_BYTES: 20000
jobs:
# ===========================================================================
# SINGLE JOB: SETUP AND ALL TESTS
# Sets up environment and runs all quality checks, tests in one job
# ===========================================================================
test:
name: 🧪 Setup & All Tests
runs-on: self-hosted
timeout-minutes: 25
steps:
- name: Checkout
uses: actions/checkout@v6
with:
ref: ${{ inputs.branch || github.ref }}
fetch-depth: 0
clean: true # 🧹 Wipes previous run artifacts to ensure a fresh start
- name: Define Dynamic Cache Path
shell: bash
# This expands $HOME (e.g., /home/ari) and saves it to the workflow env
run: echo "PLAYWRIGHT_BROWSERS_PATH=$HOME/.cache/ms-playwright" >> $GITHUB_ENV
- name: Sanitize SESSION_ID
shell: bash
run: |
# Replace / with - in SESSION_ID to prevent invalid artifact names
SAFE_SESSION_ID=$(echo "${{ env.SESSION_ID }}" | tr '/' '-')
echo "SESSION_ID=$SAFE_SESSION_ID" >> $GITHUB_ENV
# --- GLOBAL PERSISTENCE CONFIG ---
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
run_install: false
- name: Configure Persistent Store
shell: bash
run: |
mkdir -p ~/.local/share/pnpm/store
# Use session-specific cache directories to prevent conflicts
mkdir -p ~/.cache/pnpm-${{ env.SESSION_ID }}
pnpm config set store-dir ~/.local/share/pnpm/store
pnpm config set cache-dir ~/.cache/pnpm-${{ env.SESSION_ID }}
pnpm config set side-effects-cache true
- name: Setup Node.js
uses: actions/setup-node@v6
with:
node-version: '20.x'
# ---------------------------------
- name: Create Runtime Envs
run: |
mkdir -p logs coverage playwright-report test-results
echo "NEXTAUTH_URL=http://127.0.0.1:3000" > .env.local
echo "NEXTAUTH_SECRET=ci-secret-${{ env.SESSION_ID }}" >> .env.local
- name: Run Setup Script
run: |
chmod +x ./scripts/setup.sh
./scripts/setup.sh
# This installs dependencies into ./node_modules
# 1. Get version for cache key
- name: Get installed Playwright version
id: playwright-version
shell: bash
run: echo "VERSION=$(npx playwright --version | cut -d ' ' -f 2)" >> $GITHUB_OUTPUT
# 2. Cache the browser binaries in the HOME directory
#- name: Cache Playwright Browsers
# id: playwright-cache
# uses: actions/cache@v4
# with:
# path: ${{ env.PLAYWRIGHT_BROWSERS_PATH }}
# key: ${{ runner.os }}-playwright-${{ steps.playwright-version.outputs.VERSION }}
# restore-keys: |
# ${{ runner.os }}-playwright-
# 3. Install Browsers (ONLY if cache miss)
# NOTE: If running on a new server, install OS deps manually once with:
# sudo env PATH="$(dirname $(which node)):$PATH" npx playwright install-deps
- name: Install Playwright Browsers
# if: steps.playwright-cache.outputs.cache-hit != 'true'
# CHANGE: Removed --with-deps (System deps are already on your server)
run: pnpm exec playwright install chromium
- name: Cache Next.js Build
uses: actions/cache@v4
with:
path: ${{ github.workspace }}/.next/cache
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/pnpm-lock.yaml') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }}
restore-keys: |
${{ runner.os }}-nextjs-${{ hashFiles('**/pnpm-lock.yaml') }}-
# ADD THIS LINE: Matches any previous Next.js cache
${{ runner.os }}-nextjs-
- name: Lint Code
id: lint
run: |
set -o pipefail
pnpm run lint 2>&1 | tee logs/lint-output.log
- name: Verify Build
id: build
run: |
set -o pipefail
pnpm run build 2>&1 | tee logs/build-output.log
- name: Run Infrastructure Tests and Generate JUnit Report
id: infra_tests
# Run tests and output the report file to a specific path, capture output
run: |
set -o pipefail
PLAYWRIGHT_JUNIT_OUTPUT_NAME=test-results/infra-results.xml pnpm run test:infra --reporter=junit 2>&1 | tee logs/infra-output.log
# continue-on-error allows subsequent steps to run for failure reporting
continue-on-error: true
- name: Publish Infra Test Results to GitHub Checks
# This action reads the generated XML and displays it in the Checks/Summary tab
uses: dorny/test-reporter@v1
if: always() && steps.infra_tests.outcome != 'skipped' # Ensures the report is published even if tests failed, but not if skipped
with:
name: Infrastructure Tests Report # The name of the check run
path: test-results/infra-results.xml # Path to the generated XML file
reporter: java-junit
fail-on-error: true # Fails the overall workflow job if any tests failed
- name: Run Unit Tests
id: unit_tests
run: |
set -o pipefail
pnpm run test:unit 2>&1 | tee logs/unit-output.log
continue-on-error: true
- name: Publish Unit Test Results to GitHub Checks
# This action reads the generated XML and displays it in the Checks/Summary tab
uses: dorny/test-reporter@v1
if: always() && steps.unit_tests.outcome != 'skipped' # Ensures the report is published even if tests failed, but not if skipped
with:
name: Unit Tests Report # The name of the check run
path: test-results/unit-results.xml # Path to the generated XML file
reporter: java-junit
fail-on-error: true # Fails the overall workflow job if any tests failed
- name: Run Visual Tests
id: visual_tests
run: |
set -o pipefail
pnpm run test:visual 2>&1 | tee logs/visual-output.log
# NOTE: This generates files in the default ./blob-report directory
continue-on-error: true
# --- NEW SECTION: PERFORMANCE PROFILING ---
- name: Run Performance Profiling
id: perf_tests
if: always() && steps.build.outcome == 'success' # Only run if build passed
run: |
set -o pipefail
# 1. Defensive Cleanup: Ensure no stray servers from visual tests are hogging port 3000
pnpm run pm2:delete || true
fuser -k 3000/tcp || true
# 2. Run the Combined Profiling Suite
# (Ensure 'profile:all' is defined in package.json as: "bash scripts/profile-server.sh && bash scripts/profile-frontend.sh")
pnpm run profile:all 2>&1 | tee logs/performance-output.log
continue-on-error: true
# ------------------------------------------
- name: Publish Visual Test Results to GitHub Checks
uses: dorny/test-reporter@v1
if: always() && steps.visual_tests.outcome != 'skipped'
with:
name: Visual Tests Report
path: test-results/results.xml
reporter: java-junit
fail-on-error: true
- name: Merge Playwright Reports
if: always() && steps.visual_tests.outcome != 'skipped'
run: npx playwright merge-reports --reporter html ./blob-report
# The command now correctly points to the default directory containing blob reports
- name: Upload Test Results
uses: actions/upload-artifact@v4
if: always()
with:
name: test-results-${{ env.SESSION_ID }}
path: |
coverage/
playwright-report/
test-results/
logs/
reports/performance/
retention-days: 7
# Post comment on PR when tests fail
- name: Report Failure to Jules
if: always() && (steps.lint.outcome == 'failure' || steps.build.outcome == 'failure' || steps.infra_tests.outcome == 'failure' || steps.unit_tests.outcome == 'failure' || steps.visual_tests.outcome == 'failure' || steps.perf_tests.outcome == 'failure')
env:
GH_TOKEN: ${{ secrets.ARI_PAT }}
run: |
# 1. Identify the PR Number
# Try getting it from the event (pull_request)
PR_NUMBER="${{ github.event.pull_request.number }}"
# If triggered by workflow_dispatch, find the PR associated with this branch
if [ -z "$PR_NUMBER" ] || [ "$PR_NUMBER" == "null" ]; then
PR_NUMBER=$(gh pr list --head "${{ github.ref_name }}" --json number --jq '.[0].number')
fi
if [ -z "$PR_NUMBER" ] || [ "$PR_NUMBER" == "null" ]; then
echo "::warning::No Pull Request found for this branch. Skipping comment."
exit 0
fi
# 2. Build the list of failed checks and collect logs to temp files
FAILED_CHECKS=""
# Define comment file path in workspace
COMMENT_FILE="test-results/pr-comment.md"
# Ensure test-results exists (it should, but safety first)
mkdir -p test-results
# Initialize the comment file with header
# We construct the content directly into the final file to avoid temp file issues
# 1. Collect failed checks
if [ "${{ steps.lint.outcome }}" == "failure" ]; then
FAILED_CHECKS="${FAILED_CHECKS}Lint, "
fi
if [ "${{ steps.build.outcome }}" == "failure" ]; then
FAILED_CHECKS="${FAILED_CHECKS}Build, "
fi
if [ "${{ steps.infra_tests.outcome }}" == "failure" ]; then
FAILED_CHECKS="${FAILED_CHECKS}Infrastructure Tests, "
fi
if [ "${{ steps.unit_tests.outcome }}" == "failure" ]; then
FAILED_CHECKS="${FAILED_CHECKS}Unit Tests, "
fi
if [ "${{ steps.visual_tests.outcome }}" == "failure" ]; then
FAILED_CHECKS="${FAILED_CHECKS}Visual Tests, "
fi
if [ "${{ steps.perf_tests.outcome }}" == "failure" ]; then
FAILED_CHECKS="${FAILED_CHECKS}Performance Profiling, "
fi
FAILED_CHECKS="${FAILED_CHECKS%, }"
# 2. Write Header
echo "@jules fix ${FAILED_CHECKS}" > "$COMMENT_FILE"
echo "" >> "$COMMENT_FILE"
echo "<details>" >> "$COMMENT_FILE"
echo "<summary><strong>Failed Test Report Log</strong></summary>" >> "$COMMENT_FILE"
# 3. Append Logs
append_log() {
local log_file="$1"
local test_name="$2"
if [ -f "$log_file" ]; then
echo "" >> "$COMMENT_FILE"
echo "### ${test_name} Log" >> "$COMMENT_FILE"
echo '```' >> "$COMMENT_FILE"
tail -c "${{ env.LOG_TRUNCATE_BYTES }}" "$log_file" | sed -r 's/\x1b\[[0-9;]*m//g' >> "$COMMENT_FILE"
echo '```' >> "$COMMENT_FILE"
else
echo "" >> "$COMMENT_FILE"
echo "### ${test_name} Log" >> "$COMMENT_FILE"
echo "_Log file not found: ${log_file}_" >> "$COMMENT_FILE"
fi
}
if [ "${{ steps.lint.outcome }}" == "failure" ]; then
append_log "logs/lint-output.log" "Lint"
fi
if [ "${{ steps.build.outcome }}" == "failure" ]; then
append_log "logs/build-output.log" "Build"
fi
if [ "${{ steps.infra_tests.outcome }}" == "failure" ]; then
append_log "logs/infra-output.log" "Infrastructure Tests"
fi
if [ "${{ steps.unit_tests.outcome }}" == "failure" ]; then
append_log "logs/unit-output.log" "Unit Tests"
fi
if [ "${{ steps.visual_tests.outcome }}" == "failure" ]; then
append_log "logs/visual-output.log" "Visual Tests"
fi
if [ "${{ steps.perf_tests.outcome }}" == "failure" ]; then
append_log "logs/performance-output.log" "Performance Test Output"
# Also attach the specific background logs if they exist
append_log "logs/ws-stress-client.log" "WebSocket Stress Client"
append_log "logs/frontend-perf-server.log" "Frontend Perf Server"
fi
# 4. Close Details
echo "</details>" >> "$COMMENT_FILE"
# 5. Post the Comment
if [ -s "$COMMENT_FILE" ]; then
gh pr comment "$PR_NUMBER" --body-file "$COMMENT_FILE"
else
echo "::error::Comment file is empty, skipping comment."
exit 1
fi
- name: Checkout
uses: actions/checkout@v6
if: always()
with:
ref: ${{ inputs.branch || github.ref }}
fetch-depth: 0
# CHANGED: Set to false to keep .next/cache and node_modules on disk
clean: false
- name: Clean Workspace (Preserve Cache)
if: always()
run: |
# Delete everything EXCEPT .next and node_modules
find . -maxdepth 1 -mindepth 1 \
-not -name '.next' \
-not -name 'node_modules' \
-not -name '.git' \
-not -name '.cache' \
-exec rm -rf {} +
# Reset git state to ensure code is fresh
git reset --hard HEAD
git clean -fdx --exclude .next --exclude node_modules --exclude .eslintcache --exclude .cache
# Fail the job if any tests failed (must be at the very end)
- name: Fail Workflow on Test Failures
if: always() && (steps.lint.outcome == 'failure' || steps.build.outcome == 'failure' || steps.infra_tests.outcome == 'failure' || steps.unit_tests.outcome == 'failure' || steps.visual_tests.outcome == 'failure' || steps.perf_tests.outcome == 'failure')
run: |
echo "One or more tests failed. Failing the workflow."
exit 1