Skip to content

Commit

Permalink
Next attempt at fixing CWV
Browse files Browse the repository at this point in the history
  • Loading branch information
sanrai committed Nov 20, 2024
1 parent 1231010 commit c4791e5
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 57 deletions.
19 changes: 6 additions & 13 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ jobs:
record-web-vitals:
runs-on: ubuntu-latest
needs: deployment
continue-on-error: true
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Required for git history

- name: Setup Node.js
uses: actions/setup-node@v3
with:
Expand All @@ -97,13 +97,16 @@ jobs:
sudo apt-get install -y xvfb libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdrm2 libxkbcommon0 libxcomposite1 libxdamage1 libxfixes3 libxrandr2 libgbm1 libasound2
- name: Record Web Vitals
id: record-vitals
continue-on-error: true
run: |
xvfb-run --auto-servernum --server-args="-screen 0 1920x1080x24" node scripts/record-web-vitals.js
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
PR_TITLE: ${{ github.event.pull_request.title }}

- name: Create PR Comment
if: always()
uses: actions/github-script@v6
with:
script: |
Expand All @@ -124,24 +127,14 @@ jobs:
| CLS | ${formatMetric(metrics.metrics.cls)} |
Recorded at: ${metrics.date}
PR: #${metrics.pr.number}
`;
PR: #${metrics.pr.number}`;
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});
- name: Commit updated history
if: github.event.action == 'closed' && github.event.pull_request.merged == true
run: |
git config --global user.name 'GitHub Action'
git config --global user.email 'action@github.com'
git add .github/web-vitals-history.json
git commit -m "chore: update web vitals history [skip ci]" || echo "No changes to commit"
git push || echo "No changes to push"
run-e2e-tests:
runs-on: ubuntu-latest
needs: deployment
Expand Down
126 changes: 82 additions & 44 deletions scripts/record-web-vitals.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,105 @@ const execPromise = util.promisify(exec);

async function collectMetrics() {
console.log('Collecting web vitals metrics...');
const isMergeContext = process.env.GITHUB_EVENT_NAME === 'pull_request' &&
process.env.GITHUB_EVENT_ACTION === 'closed' &&
process.env.GITHUB_PULL_REQUEST_MERGED === 'true';

// Debug logging for CI/CD
console.log('Environment variables:', {
GITHUB_EVENT_NAME: process.env.GITHUB_EVENT_NAME,
GITHUB_EVENT_ACTION: process.env.GITHUB_EVENT_ACTION,
GITHUB_PULL_REQUEST_MERGED: process.env.GITHUB_PULL_REQUEST_MERGED,
PR_NUMBER: process.env.PR_NUMBER,
PR_TITLE: process.env.PR_TITLE
});
console.log('Is merge context?', isMergeContext);

let metricsOutput;
try {
// Execute web-vitals-check.js
// Run web-vitals-check.js
const url = 'https://adobecom.github.io/caas/';
const command = `node ${path.join(process.cwd(), 'scripts', 'web-vitals-check.js')} "${url}"`;

const { stdout, stderr } = await execPromise(command);
if (stderr) {
console.error('Command stderr:', stderr);
try {
console.log('Running web-vitals-check.js...');
const { stdout, stderr } = await execPromise(command);
console.log('Web Vitals Check Output:', stdout);
if (stderr) console.log('Stderr:', stderr);
metricsOutput = stdout;
} catch (error) {
// Still capture output even when check fails
console.log('Web Vitals Check Output:', error.stdout);
if (error.stderr) console.log('Stderr:', error.stderr);
metricsOutput = error.stdout;

// Only exit for failures in non-merge context
if (!isMergeContext) {
console.log('Exiting due to poor metrics in non-merge context');
process.exit(1);
}
}

// Parse metrics from the output
const currentMetricsPath = path.join(process.cwd(), '.github', 'current-metrics.json');
// Process metrics for history in merge context
if (isMergeContext) {
console.log('Processing metrics in merge context...');
const currentMetricsPath = path.join(process.cwd(), '.github', 'current-metrics.json');
console.log('Looking for current metrics at:', currentMetricsPath);

if (fs.existsSync(currentMetricsPath)) {
const currentMetrics = JSON.parse(fs.readFileSync(currentMetricsPath, 'utf8'));
if (fs.existsSync(currentMetricsPath)) {
console.log('Found current metrics file');
const currentMetrics = JSON.parse(fs.readFileSync(currentMetricsPath, 'utf8'));

const record = {
date: new Date().toISOString(),
pr: {
number: process.env.PR_NUMBER || 'unknown',
title: process.env.PR_TITLE || 'unknown'
},
metrics: currentMetrics.metrics
};
const record = {
date: new Date().toISOString(),
pr: {
number: process.env.PR_NUMBER || 'unknown',
title: process.env.PR_TITLE || 'unknown'
},
metrics: currentMetrics.metrics
};

// Ensure .github directory exists
const githubDir = path.join(process.cwd(), '.github');
if (!fs.existsSync(githubDir)) {
fs.mkdirSync(githubDir, { recursive: true });
}
// Ensure .github directory exists
const githubDir = path.join(process.cwd(), '.github');
if (!fs.existsSync(githubDir)) {
console.log('Creating .github directory');
fs.mkdirSync(githubDir, { recursive: true });
}

// Store metrics in history file
const historyPath = path.join(githubDir, 'web-vitals-history.json');
let history = [];
// Update history file
const historyPath = path.join(githubDir, 'web-vitals-history.json');
console.log('Updating history at:', historyPath);
let history = [];

if (fs.existsSync(historyPath)) {
try {
const content = fs.readFileSync(historyPath, 'utf8');
history = JSON.parse(content);
} catch (error) {
console.warn('Error reading history file, starting fresh:', error.message);
if (fs.existsSync(historyPath)) {
try {
const content = fs.readFileSync(historyPath, 'utf8');
history = JSON.parse(content);
console.log('Loaded existing history with', history.length, 'entries');
} catch (error) {
console.warn('Error reading history file, starting fresh:', error.message);
}
} else {
console.log('No existing history file, creating new one');
}
}

if (!Array.isArray(history)) {
history = [];
}

history.push(record);
fs.writeFileSync(historyPath, JSON.stringify(history, null, 2));
if (!Array.isArray(history)) {
console.log('History was not an array, resetting');
history = [];
}

console.log('Metrics collected:');
console.log(JSON.stringify(record, null, 2));
} else {
console.error('No metrics file found. Web vitals check may have failed.');
process.exit(1);
history.push(record);
fs.writeFileSync(historyPath, JSON.stringify(history, null, 2));
console.log('Successfully updated metrics history file');
} else {
console.warn('No current metrics file found at:', currentMetricsPath);
}
}

} catch (error) {
console.error('Error collecting metrics:', error);
process.exit(1);
console.error('Fatal error in collectMetrics:', error);
// Don't exit with error - let GitHub action continue
process.exit(0);
}
}

Expand Down

0 comments on commit c4791e5

Please sign in to comment.