forked from GoogleChrome/web.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version-check.js
50 lines (43 loc) · 1.45 KB
/
version-check.js
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
/**
* @fileoverview This file compares the current commit SHA against the one
* of the deployed site. If they are different the build is allowed to
* continue, otherwise the build is cancelled.
*/
const fetch = require('node-fetch');
const {ErrorReporting} = require('@google-cloud/error-reporting');
const {execSync} = require('child_process');
const {CloudBuildClient} = require('@google-cloud/cloudbuild');
const client = new CloudBuildClient();
const errors = new ErrorReporting();
const ERROR_MESSAGE = 'NOT FOUND';
/**
* @returns {Promise<string>}
*/
const getDeployedVersion = () => {
// @ts-ignore
return fetch('https://web.dev/version')
.then((res) => (res.ok ? res.text() : ERROR_MESSAGE))
.catch(() => ERROR_MESSAGE);
};
(async () => {
const deployedVersion = await getDeployedVersion();
const currentVersion = execSync('git rev-parse HEAD').toString().trim();
console.log(`Current version: ${currentVersion}`);
console.log(`Deployed version: ${deployedVersion}`);
if (deployedVersion === ERROR_MESSAGE) {
errors.report('Deployed commit SHA not found');
}
if (deployedVersion === currentVersion) {
console.log(
'The current and deployed versions are the same, stopping build.',
);
await client.cancelBuild({
id: process.env.BUILD_ID,
projectId: process.env.PROJECT_ID,
});
} else {
console.log(
'The current and deployed versions are different, continuing build.',
);
}
})();