Skip to content

Commit

Permalink
websites-integration: polish
Browse files Browse the repository at this point in the history
  • Loading branch information
cometkim committed Jun 7, 2024
1 parent 8d57b6d commit a8d01e4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
42 changes: 28 additions & 14 deletions _workers/websites-integration/deployment-awaiter.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { setInterval } from 'node:timers/promises';
import { parseArgs } from 'node:util';
import { createWriteStream } from 'node:fs';
import { Readable } from 'node:stream';
import { finished } from 'node:stream/promises';
import prettyMilliseconds from 'pretty-ms';

import { $ } from 'zx';

Expand All @@ -19,7 +23,12 @@ if (CF_PAGES !== '1') {
throw new Error("deployment-awaiter should be executed only on Cloudflare Pages' build");
}

const startedAt = Date.now();

const deploymentUrl = new URL(WEBSITES_DEPLOYMENT_ENDPOINT);
const headers = new Headers({
Authorization: `AdminKey ${WEBSITES_ADMIN_KEY}`,
});

const { values } = parseArgs({
args: process.argv.slice(2),
Expand All @@ -42,11 +51,7 @@ const params = {

const initResponse = await fetch(deploymentUrl, {
method: 'POST',
headers: {
Accept: 'application/json',
Authorization: `AdminKey ${WEBSITES_ADMIN_KEY}`,
'Content-Type': 'application/json',
},
headers,
body: JSON.stringify(params),
});
const initData = await initResponse.json();
Expand All @@ -66,16 +71,11 @@ const artifactUrl = new URL(initData.artifact_url);
const timeout = Number.parseInt(values.timeout);
for await (const startTime of setInterval(5000, Date.now())) {
if (Date.now() - startTime >= timeout) {
console.error(`Timeout exceeded (${timeout} ms)`);
console.error(`Timeout exceeded (${prettyMilliseconds(timeout)})`);
process.exit(1);
}

const res = await fetch(checkUrl, {
headers: {
Accept: 'application/json',
Authorization: `AdminKey ${WEBSITES_ADMIN_KEY}`,
},
});
const res = await fetch(checkUrl, { headers });
const data = await res.json();
if (!res.ok) {
console.error({ status: res.status, data });
Expand All @@ -91,7 +91,7 @@ for await (const startTime of setInterval(5000, Date.now())) {
if (state.runId && !bound) {
bound = true;
runUrl = `https://github.com/daangn/websites/actions/runs/${state.runId}`;
console.log(`Awaiting build completed on ${runUrl} (timeout: ${timeout}ms)`);
console.log(`Waiting for job to finish on ${runUrl} (timeout: ${prettyMilliseconds(timeout)})`);
}

if (state.type === 'IN_PROGRESS') {
Expand All @@ -114,7 +114,21 @@ for await (const startTime of setInterval(5000, Date.now())) {
}

console.log('Downloading artifact...');
await $`curl -fL -H "Authorization: AdminKey ${WEBSITES_ADMIN_KEY}" "${artifactUrl.toString()}" -o public.tar.zst`;
const downloadRes = await fetch(artifactUrl, { headers });
if (!downloadRes.ok) {
console.error('Failed to download artifact');
try {
const resData = await downloadRes.json();
console.error('status: %d, res: %o', downloadRes.status, resData);
} catch {
console.error('status: %d, res: %s', downloadRes.status, downloadRes.statusText);
}
process.exit(1);
}
const fileStream = createWriteStream('public.tar.zst');
await finished(Readable.from(downloadRes.body).pipe(fileStream));

console.log('Extracting artifact...');
await $`tar --use-compress-program="zstd -d" -xvf public.tar.zst`;

console.log(`Build is ready in ${prettyMilliseconds(Date.now() - startedAt)}`);
1 change: 1 addition & 0 deletions _workers/websites-integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"dependencies": {
"@octokit/rest": "^20.1.1",
"pretty-ms": "^9.0.0",
"zx": "^8.1.2"
},
"devDependencies": {
Expand Down
17 changes: 17 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15192,6 +15192,13 @@ __metadata:
languageName: node
linkType: hard

"parse-ms@npm:^4.0.0":
version: 4.0.0
resolution: "parse-ms@npm:4.0.0"
checksum: 10c0/a7900f4f1ebac24cbf5e9708c16fb2fd482517fad353aecd7aefb8c2ba2f85ce017913ccb8925d231770404780df46244ea6fec598b3bde6490882358b4d2d16
languageName: node
linkType: hard

"parse-path@npm:^7.0.0":
version: 7.0.0
resolution: "parse-path@npm:7.0.0"
Expand Down Expand Up @@ -15909,6 +15916,15 @@ __metadata:
languageName: node
linkType: hard

"pretty-ms@npm:^9.0.0":
version: 9.0.0
resolution: "pretty-ms@npm:9.0.0"
dependencies:
parse-ms: "npm:^4.0.0"
checksum: 10c0/ba4a2acd1fe92a1c629e5cdeb555d7fa344ae9920e20fa00e8ac1db61b8d3dff8638ffc70c7569f681e375df68c9f31291c2c1912cefd02ef1b1bdd0861a4aed
languageName: node
linkType: hard

"printable-characters@npm:^1.0.42":
version: 1.0.42
resolution: "printable-characters@npm:1.0.42"
Expand Down Expand Up @@ -19194,6 +19210,7 @@ __metadata:
dependencies:
"@cloudflare/workers-types": "npm:^4.20240529.0"
"@octokit/rest": "npm:^20.1.1"
pretty-ms: "npm:^9.0.0"
typescript: "npm:^5.4.5"
wrangler: "npm:^3.58.0"
zx: "npm:^8.1.2"
Expand Down

0 comments on commit a8d01e4

Please sign in to comment.