-
Notifications
You must be signed in to change notification settings - Fork 48.4k
/
Copy pathdownload-build-artifacts.js
226 lines (202 loc) · 6.32 KB
/
download-build-artifacts.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
'use strict';
const {join} = require('path');
const theme = require('../theme');
const {exec} = require('child-process-promise');
const {existsSync, mkdtempSync, readFileSync} = require('fs');
const {logPromise} = require('../utils');
const os = require('os');
if (process.env.GH_TOKEN == null) {
console.log(
theme`{error Expected GH_TOKEN to be provided as an env variable}`
);
process.exit(1);
}
const OWNER = 'facebook';
const REPO = 'react';
const WORKFLOW_ID = 'runtime_build_and_test.yml';
const GITHUB_HEADERS = `
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${process.env.GH_TOKEN}" \
-H "X-GitHub-Api-Version: 2022-11-28"`.trim();
async function executableIsAvailable(name) {
try {
await exec(`which ${name}`);
return true;
} catch (_error) {
return false;
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function getWorkflowId() {
if (
existsSync(join(__dirname, `../../../.github/workflows/${WORKFLOW_ID}`))
) {
return WORKFLOW_ID;
} else {
throw new Error(
`Incorrect workflow ID: .github/workflows/${WORKFLOW_ID} does not exist. Please check the name of the workflow being downloaded from.`
);
}
}
async function getWorkflowRun(commit) {
const res = await exec(
`curl -L ${GITHUB_HEADERS} https://api.github.com/repos/${OWNER}/${REPO}/actions/workflows/${getWorkflowId()}/runs?head_sha=${commit}`
);
const json = JSON.parse(res.stdout);
const workflowRun = json.workflow_runs.find(run => run.head_sha === commit);
if (workflowRun == null || workflowRun.id == null) {
console.log(
theme`{error The workflow run for the specified commit (${commit}) could not be found.}`
);
process.exit(1);
}
return workflowRun;
}
async function getArtifact(workflowRunId, artifactName) {
const res = await exec(
`curl -L ${GITHUB_HEADERS} https://api.github.com/repos/${OWNER}/${REPO}/actions/runs/${workflowRunId}/artifacts?per_page=100&name=${artifactName}`
);
const json = JSON.parse(res.stdout);
const artifact = json.artifacts.find(
_artifact => _artifact.name === artifactName
);
if (artifact == null) {
console.log(
theme`{error The specified workflow run (${workflowRunId}) does not contain any build artifacts.}`
);
process.exit(1);
}
return artifact;
}
async function processArtifact(artifact, opts) {
// Download and extract artifact
const cwd = join(__dirname, '..', '..', '..');
const tmpDir = mkdtempSync(join(os.tmpdir(), 'react_'));
await exec(`rm -rf ./build`, {cwd});
await exec(
`curl -L ${GITHUB_HEADERS} ${artifact.archive_download_url} > artifacts_combined.zip`,
{
cwd: tmpDir,
}
);
if (opts.noVerify === true) {
console.log(theme`{caution Skipping verification of build artifact.}`);
} else {
// Use https://cli.github.com/manual/gh_attestation_verify to verify artifact
if (executableIsAvailable('gh')) {
await exec(
`gh attestation verify artifacts_combined.zip --repo=${OWNER}/${REPO}`,
{
cwd: tmpDir,
}
);
}
}
await exec(
`unzip ${tmpDir}/artifacts_combined.zip -d . && rm build2.tgz && tar -xvzf build.tgz && rm build.tgz`,
{
cwd,
}
);
// Copy to staging directory
// TODO: Consider staging the release in a different directory from the CI
// build artifacts: `./build/node_modules` -> `./staged-releases`
if (!existsSync(join(cwd, 'build'))) {
await exec(`mkdir ./build`, {cwd});
} else {
await exec(`rm -rf ./build/node_modules`, {cwd});
}
let sourceDir;
// TODO: Rename release channel to `next`
if (opts.releaseChannel === 'stable') {
sourceDir = 'oss-stable';
} else if (opts.releaseChannel === 'experimental') {
sourceDir = 'oss-experimental';
} else if (opts.releaseChannel === 'rc') {
sourceDir = 'oss-stable-rc';
} else if (opts.releaseChannel === 'latest') {
sourceDir = 'oss-stable-semver';
} else {
console.error(
'Internal error: Invalid release channel: ' + opts.releaseChannel
);
process.exit(opts.releaseChannel);
}
await exec(`cp -r ./build/${sourceDir} ./build/node_modules`, {
cwd,
});
// Validate artifact
const buildSha = readFileSync('./build/COMMIT_SHA', 'utf8').replace(
/[\u0000-\u001F\u007F-\u009F]/g,
''
);
if (buildSha !== opts.commit) {
throw new Error(
`Requested commit sha does not match downloaded artifact. Expected: ${opts.commit}, got: ${buildSha}`
);
}
}
async function downloadArtifactsFromGitHub(opts) {
let workflowRun;
let retries = 0;
// wait up to 10 mins for build to finish: 10 * 60 * 1_000) / 30_000 = 20
while (retries < 20) {
workflowRun = await getWorkflowRun(opts.commit);
if (typeof workflowRun.status === 'string') {
switch (workflowRun.status) {
case 'queued':
case 'in_progress':
case 'waiting': {
retries++;
console.log(theme`Build still in progress, waiting 30s...`);
await sleep(30_000);
break;
}
case 'completed': {
if (workflowRun.conclusion === 'success') {
const artifact = await getArtifact(
workflowRun.id,
'artifacts_combined'
);
await processArtifact(artifact, opts);
return;
} else {
console.log(
theme`{error Could not download build as its conclusion was: ${workflowRun.conclusion}}`
);
process.exit(1);
}
break;
}
default: {
console.log(
theme`{error Unhandled workflow run status: ${workflowRun.status}}`
);
process.exit(1);
}
}
} else {
retries++;
console.log(
theme`{error Expected workflow run status to be a string, got: ${workflowRun.status}. Retrying...}`
);
}
}
console.log(
theme`{error Could not download build from GitHub. Last workflow run: }
${workflowRun != null ? JSON.stringify(workflowRun, null, '\t') : workflowRun}`
);
process.exit(1);
}
async function downloadBuildArtifacts(opts) {
const label = theme`commit {commit ${opts.commit}})`;
return logPromise(
downloadArtifactsFromGitHub(opts),
theme`Downloading artifacts from GitHub for ${label}`
);
}
module.exports = {
downloadBuildArtifacts,
};