Skip to content

Commit 6266328

Browse files
committed
Upload size report to the metrics service.
1 parent 1c34983 commit 6266328

File tree

3 files changed

+86
-13
lines changed

3 files changed

+86
-13
lines changed

.github/workflows/test-all.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: Run All Tests
22

3-
on:
3+
on:
44
pull_request:
55
branches:
66
- master
@@ -40,8 +40,6 @@ jobs:
4040
github-token: ${{ secrets.GITHUB_TOKEN }}
4141
path-to-lcov: ./lcov-all.info
4242
continue-on-error: true
43-
- name: Generate Size Report
44-
run: yarn size-report
4543
deploy:
4644
name: Canary Deploy
4745
runs-on: ubuntu-latest

.github/workflows/test-changed.yml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,27 @@ jobs:
2828
run: yarn build
2929
- name: Run tests on changed packages
3030
run: xvfb-run yarn test:changed
31-
- name: Generate Size Report
32-
run: yarn size-report
31+
32+
health-metrics-test:
33+
name: Health Metrics Test
34+
runs-on: ubuntu-latest
35+
env:
36+
METRICS_SERVICE_URL: ${{ secrets.METRICS_SERVICE_URL }}
37+
GITHUB_PULL_REQUEST_NUMBER: ${{ github.event.pull_request.number }}
38+
GITHUB_PULL_REQUEST_BASE_SHA: ${{ github.event.pull_request.base.sha }}
39+
steps:
40+
- uses: actions/checkout@v2
41+
- uses: actions/setup-node@v1
42+
with:
43+
node-version: 10.x
44+
- uses: GoogleCloudPlatform/github-actions/setup-gcloud@master
45+
with:
46+
service_account_key: ${{ secrets.GCP_SA_KEY }}
47+
- run: cp config/ci.config.json config/project.json
48+
- run: yarn install
49+
- run: yarn build
50+
51+
- name: Run health-metrics/binary-size test
52+
run: yarn size-report
53+
54+
# TODO(yifany): Enable startup times testing on CI.

scripts/report_binary_size.js

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
const { resolve } = require('path');
22
const fs = require('fs');
33
const { execSync } = require('child_process');
4+
const https = require('https');
45

56
const repoRoot = resolve(__dirname, '..');
67

7-
const commitHash = process.env.GITHUB_SHA || execSync('git rev-parse HEAD').toString();
88
const runId = process.env.GITHUB_RUN_ID || 'local-run-id';
99

10+
const METRICS_SERVICE_URL = process.env.METRICS_SERVICE_URL;
11+
1012
// CDN scripts
1113
function generateReportForCDNScripts() {
1214
const reports = [];
1315
const firebaseRoot = resolve(__dirname, '../packages/firebase');
1416
const pkgJson = require(`${firebaseRoot}/package.json`);
15-
17+
1618
const special_files = [
1719
'firebase-performance-standalone.es2017.js',
1820
'firebase-performance-standalone.js',
1921
'firebase.js'
2022
];
21-
23+
2224
const files = [
23-
...special_files.map(file => `${firebaseRoot}/${file}`),
25+
...special_files.map(file => `${firebaseRoot}/${file}`),
2426
...pkgJson.components.map(component => `${firebaseRoot}/firebase-${component}.js`)
2527
];
26-
28+
2729
for (const file of files) {
2830
const { size } = fs.statSync(file);
2931
const fileName = file.split('/').slice(-1)[0];
@@ -83,13 +85,64 @@ function generateSizeReport() {
8385
console.log(r.sdk, r.type, r.value);
8486
}
8587

86-
console.log(`Github Action URL: https://github.com/firebase/firebase-js-sdk/actions/runs/${runId}`);
88+
console.log(`Github Action URL: https://github.com/${process.env.GITHUB_REPOSITORY}/actions/runs/${runId}`);
8789

8890
return {
89-
log: `https://github.com/firebase/firebase-js-sdk/actions/runs/${runId}`,
91+
log: `https://github.com/${process.env.GITHUB_REPOSITORY}/actions/runs/${runId}`,
9092
metric: "BinarySize",
9193
results: reports
9294
};
9395
}
9496

95-
generateSizeReport();
97+
function constructRequestPath() {
98+
const repo = process.env.GITHUB_REPOSITORY;
99+
const commit = process.env.GITHUB_SHA;
100+
let path = `/repos/${repo}/commits/${commit}/reports`;
101+
if (process.env.GITHUB_EVENT_NAME === 'pull_request') {
102+
const pullRequestNumber = process.env.GITHUB_PULL_REQUEST_NUMBER;
103+
const pullRequestBaseSha = process.env.GITHUB_PULL_REQUEST_BASE_SHA;
104+
path += `?pull_request=${pullRequestNumber}&base_commit=${pullRequestBaseSha}`;
105+
}
106+
return path;
107+
}
108+
109+
function constructRequestOptions(path) {
110+
const accessToken = execSync('gcloud auth print-identity-token', { encoding: 'utf8' }).trim();
111+
return {
112+
path: path,
113+
method: 'POST',
114+
headers: {
115+
'Authorization': `Bearer ${accessToken}`,
116+
'Content-Type': 'application/json'
117+
}
118+
};
119+
}
120+
121+
function upload(report) {
122+
if (!process.env.GITHUB_ACTIONS) {
123+
console.log('Metrics upload is only enabled on CI.');
124+
return;
125+
}
126+
127+
const path = constructRequestPath();
128+
const options = constructRequestOptions(path);
129+
130+
console.log(`${report.metric} report:`, report);
131+
console.log(`Posting to metrics service endpoint: ${path} ...`);
132+
133+
const request = https.request(METRICS_SERVICE_URL, options, response => {
134+
response.setEncoding('utf8');
135+
console.log(`Response status code: ${response.statusCode}`);
136+
response.on('data', console.log);
137+
response.on('end', () => {
138+
if (response.statusCode !== 202) {
139+
process.exit(1);
140+
}
141+
})
142+
});
143+
request.write(JSON.stringify(report));
144+
request.end();
145+
}
146+
147+
const report = generateSizeReport();
148+
upload(report);

0 commit comments

Comments
 (0)