Skip to content

Commit 26bfc91

Browse files
committed
build: get previous payload by querying git
Currently the previous payload is just the latest uploaded payload result on Firebase. This is problematic because it can happen that the current payload is uploaded "too fast" and the payload task is comparing against itself. Just calculating before uploading could work most of the time, but there it would still fail if another Travis Job uploades payload results at the same time. The safest solution would be resolving the parent commit of the current Travis job. This way the payload diff would be always correct (even if multiple Jobs are running).
1 parent 82d752b commit 26bfc91

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

tools/gulp/tasks/payload.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {statSync} from 'fs';
44
import {isTravisBuild, isTravisMasterBuild} from '../util/travis-ci';
55
import {buildConfig} from '../packaging/build-config';
66
import {openFirebaseDashboardApp, openFirebaseDashboardAppAsGuest} from '../util/firebase';
7+
import {spawnSync} from 'child_process';
78
import * as firebaseAdmin from 'firebase-admin';
89

910

@@ -78,14 +79,17 @@ async function calculatePayloadDiff(database: firebaseAdmin.database.Database, c
7879
return;
7980
}
8081

81-
const previousPayload = await getLastPayloadResults(database);
82+
const previousSha = getCommitFromPreviousPayloadUpload();
83+
const previousPayload = await getPayloadResults(database, previousSha);
8284

8385
if (!previousPayload) {
8486
console.warn('There are no previous payload results uploaded. Cannot calculate payload ' +
8587
'difference for this job');
8688
return;
8789
}
8890

91+
console.log(`Comparing payload against payload results from SHA ${previousSha}`);
92+
8993
// Calculate the payload diffs by subtracting the previous size of the FESM ES2015 bundles.
9094
const cdkFullSize = currentPayload.cdk_fesm_2015;
9195
const cdkDiff = cdkFullSize - previousPayload.cdk_fesm_2015;
@@ -139,15 +143,24 @@ async function uploadPayloadResults(database: firebaseAdmin.database.Database, c
139143
}
140144

141145
/** Gets the last payload uploaded from previous Travis builds. */
142-
async function getLastPayloadResults(database: firebaseAdmin.database.Database) {
143-
const snapshot = await database.ref('payloads')
144-
.orderByChild('timestamp')
145-
.limitToLast(1)
146-
.once('value');
146+
async function getPayloadResults(database: firebaseAdmin.database.Database, commitSha: string) {
147+
const snapshot = await database.ref('payloads').child(commitSha).once('value');
148+
149+
if (!snapshot.exists()) {
150+
throw `There is no payload data uploaded for SHA ${commitSha}`;
151+
}
147152

148-
// The value of the DataSnapshot is an object with the SHA as a key. Later only the
149-
// first value of the object will be returned because the SHA is unnecessary.
150-
const results = snapshot.val();
153+
return snapshot.val();
154+
}
151155

152-
return snapshot.hasChildren() ? results[Object.keys(results)[0]] : null;
156+
/** Gets the SHA of the commit where the payload was uploaded before this Travis Job started. */
157+
function getCommitFromPreviousPayloadUpload(): string {
158+
if (isTravisMasterBuild()) {
159+
const commitRange = process.env['TRAVIS_COMMIT_RANGE'];
160+
const commitCount = spawnSync('git', ['rev-list', '--count', commitRange]).stdout
161+
.toString().trim();
162+
return spawnSync('git', ['rev-parse', `HEAD~${commitCount}`]).stdout.toString().trim();
163+
} else {
164+
return spawnSync('git', ['rev-parse', process.env['TRAVIS_BRANCH']]).stdout.toString().trim();
165+
}
153166
}

0 commit comments

Comments
 (0)