forked from GoogleChrome/web.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version-check.js
79 lines (68 loc) · 2.18 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
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
/**
* @fileoverview This file compares the current commit SHA against the one
* of the deployed site. If they are different then another Cloud Build task
* is kicked off ('deploy.yml').
*/
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';
const deployTriggerId = '616df5ff-f33e-48ea-bfc9-1ab92121b5ba';
/**
* @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, not continuing build.',
);
return;
}
console.log(
'The current and deployed versions are different, kicking off deploy build.',
);
// Check if there are any existing builds.
const ret = client.listBuildsAsync({
projectId: process.env.PROJECT_ID,
pageSize: 1,
filter: `trigger_id="${deployTriggerId}" AND (status="WORKING" OR status="QUEUED")`,
});
let activeBuild = false;
// eslint-disable-next-line no-unused-vars
for await (const _build of ret) {
activeBuild = true;
break;
}
if (activeBuild) {
console.log(
'There is a current active or queued build. Not starting another.',
);
return;
}
try {
// This just waits for the build to be kicked off, not for its completion (it
// returns a LROperation).
await client.runBuildTrigger({
projectId: process.env.PROJECT_ID,
triggerId: deployTriggerId,
});
} catch (e) {
errors.report(e);
}
})();