forked from uber/baseweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alpha-test-web-code.js
118 lines (95 loc) · 3.04 KB
/
alpha-test-web-code.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
Copyright (c) 2018-2020 Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// Sets local package.json to current baseui version.
/* eslint-env node */
// @flow
const {spawnSync} = require('child_process');
const fetch = require('node-fetch').default;
const publishToNpm = require('./publish-to-npm.js');
const BUILDKITE_TASK_RUNNER_URL =
'https://api.buildkite.com/v2/organizations/uber/pipelines/web-code-task-runner/builds';
function annotateBuild(body, style = 'info') {
spawnSync(
'buildkite-agent',
['annotate', body, '--append', '--style', style],
{stdio: 'inherit'},
);
}
function wait(ms) {
return new Promise(res => setTimeout(res, ms));
}
async function createBuild(token, version) {
const body = {
commit: 'HEAD',
branch: 'main',
message: '[alpha-test-baseui] Triggered from CI',
meta_data: {task: 'baseui-alpha-test'},
env: {
TASK: 'baseui-alpha-test',
TASK_ENV: `PACKAGE_VERSION=baseui@${version}`,
},
};
const createBuildResponse = await fetch(BUILDKITE_TASK_RUNNER_URL, {
method: 'post',
body: JSON.stringify(body),
headers: {Authorization: `Bearer ${token}`},
});
if (!createBuildResponse.ok) {
throw new Error(
`Failed to create build. Status: ${createBuildResponse.status}. Message: ${createBuildResponse.statusText}`,
);
}
const json = await createBuildResponse.json();
return json;
}
async function getBuild(token, number) {
const getBuildResponse = await fetch(
`${BUILDKITE_TASK_RUNNER_URL}/${number}`,
{headers: {Authorization: `Bearer ${token}`}},
);
if (!getBuildResponse.ok) {
throw new Error(
`Failed to create build. Status: ${getBuildResponse.status}. Message: ${getBuildResponse.statusText}`,
);
}
const json = await getBuildResponse.json();
return json;
}
async function main() {
const buildkiteToken = process.env.BUILDKITE_API_TOKEN;
if (!buildkiteToken) {
throw new Error(
'Failed to alpha test web-code. BUILDKITE_API_TOKEN env var not set.',
);
}
const version = publishToNpm({
tag: 'alpha',
commit: process.env.BUILDKITE_COMMIT,
});
console.log(`--- Starting web-code alpha-test with baseui@${version}`);
const {web_url, number} = await createBuild(buildkiteToken, version);
annotateBuild(`View web-code alpha build CI check [here]${web_url}`);
console.log(`View alpha build CI checks at ${web_url}.`);
// eslint-disable-next-line no-constant-condition
while (true) {
await wait(60 * 1000);
const {state} = await getBuild(buildkiteToken, number);
if (state === 'passed') {
console.log('Alpha build passed CI checks.');
break;
} else if (state === 'running' || state === 'scheduled') {
console.log(
`Alpha build CI checks are ${state}. Waiting 60s before polling again.`,
);
} else {
console.log(
`Alpha build CI checks failed with ${state || 'UNDEFINED'} state.`,
);
process.exit(1);
}
}
}
main();