Skip to content

Commit 2682ba8

Browse files
dhalbertclaude
andcommitted
Run the size record download from github-script
The artifact API token `ACTIONS_RUNTIME_TOKEN` is only given to action steps, not `run:` steps, so calling the script with `node` found no records at all. `ci_download_sizes.mjs` is now a module that an `actions/github-script` step imports and calls; the install of `@actions/artifact` stays a shell step. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 7435c96 commit 2682ba8

2 files changed

Lines changed: 53 additions & 35 deletions

File tree

‎.github/workflows/build.yml‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,26 @@ jobs:
262262
# actions/download-artifact matches a pattern against a listing of the run's
263263
# artifacts that stops at 1000, and a full build has more. The script fetches each
264264
# board's record by exact name instead, from the board list the scheduler produced.
265+
# It runs from github-script because only action steps get the artifact API token.
266+
- name: Install the artifact client
267+
continue-on-error: true
268+
run: npm install --no-save --no-package-lock --no-audit --no-fund @actions/artifact@6
269+
265270
- name: Download firmware sizes
266271
continue-on-error: true
272+
uses: actions/github-script@v9
267273
env:
268274
BOARDS: ${{ needs.scheduler.outputs.ports }}
269-
run: |
270-
npm install --no-save --no-package-lock --no-audit --no-fund @actions/artifact@6
271-
node tools/ci_download_sizes.mjs sizes
275+
with:
276+
script: |
277+
const { pathToFileURL } = require("node:url");
278+
const sizes = await import(pathToFileURL(`${process.env.GITHUB_WORKSPACE}/tools/ci_download_sizes.mjs`));
279+
await sizes.downloadSizes({
280+
boards: sizes.boardsFromSchedule(JSON.parse(process.env.BOARDS || "{}")),
281+
outDir: "sizes",
282+
info: core.info,
283+
warning: core.warning,
284+
});
272285
273286
- name: Summarize firmware sizes
274287
continue-on-error: true

‎tools/ci_download_sizes.mjs‎

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#!/usr/bin/env node
2-
31
// SPDX-FileCopyrightText: 2014 MicroPython & CircuitPython contributors (https://github.com/adafruit/circuitpython/graphs/contributors)
42
//
53
// SPDX-License-Identifier: MIT
@@ -12,41 +10,48 @@
1210
// a full build exceeds. Looking each record up by name has no such limit and uses the same
1311
// internal artifact API, so it costs nothing against the REST rate limit.
1412
//
15-
// Usage: BOARDS='<the scheduler job's "ports" output>' node ci_download_sizes.mjs <out-dir>
16-
// Needs @actions/artifact where node can resolve it: `npm install` at the repository root.
13+
// That API needs the runner's ACTIONS_RUNTIME_TOKEN, which only `uses:` action steps get,
14+
// so build.yml calls downloadSizes() from an actions/github-script step rather than from
15+
// `node` in a `run:` step. @actions/artifact must be installed where this file can resolve
16+
// it: `npm install` at the repository root.
1717

1818
import { DefaultArtifactClient } from "@actions/artifact";
1919

20-
const outDir = process.argv[2] ?? "sizes";
21-
const spec = JSON.parse(process.env.BOARDS || "{}");
22-
const boards = (spec.ports ?? []).flatMap((port) => spec[port]);
23-
const client = new DefaultArtifactClient();
24-
const missing = [];
25-
let downloaded = 0;
26-
27-
async function fetchRecord(board) {
28-
try {
29-
const { artifact } = await client.getArtifact(`zz-sizes-${board}`);
30-
await client.downloadArtifact(artifact.id, { path: outDir });
31-
downloaded++;
32-
} catch (error) {
33-
missing.push(board);
34-
console.log(`No size record for ${board}: ${error.message}`);
35-
}
20+
// The boards in the scheduler job's "ports" output: {<port>: [boards...], ports: [<port>...]}.
21+
export function boardsFromSchedule(schedule) {
22+
return (schedule.ports ?? []).flatMap((port) => schedule[port]);
3623
}
3724

38-
// A few downloads at a time: enough to get through a full build in a minute or two,
39-
// few enough not to trip the artifact service's throttling.
40-
const queue = [...boards];
41-
await Promise.all(
42-
Array.from({ length: 8 }, async () => {
43-
while (queue.length) {
44-
await fetchRecord(queue.shift());
25+
export async function downloadSizes({ boards, outDir, info = console.log, warning = console.warn }) {
26+
const client = new DefaultArtifactClient();
27+
const missing = [];
28+
let downloaded = 0;
29+
30+
async function fetchRecord(board) {
31+
try {
32+
const { artifact } = await client.getArtifact(`zz-sizes-${board}`);
33+
await client.downloadArtifact(artifact.id, { path: outDir });
34+
downloaded++;
35+
} catch (error) {
36+
missing.push(board);
37+
info(`No size record for ${board}: ${error.message}`);
4538
}
46-
}),
47-
);
39+
}
40+
41+
// A few downloads at a time: enough to get through a full build in a minute or two,
42+
// few enough not to trip the artifact service's throttling.
43+
const queue = [...boards];
44+
await Promise.all(
45+
Array.from({ length: 8 }, async () => {
46+
while (queue.length) {
47+
await fetchRecord(queue.shift());
48+
}
49+
}),
50+
);
4851

49-
console.log(`Downloaded ${downloaded} of ${boards.length} size records to ${outDir}`);
50-
if (missing.length) {
51-
console.log(`::warning::No firmware size record for ${missing.length} board(s): ${missing.join(" ")}`);
52+
info(`Downloaded ${downloaded} of ${boards.length} size records to ${outDir}`);
53+
if (missing.length) {
54+
warning(`No firmware size record for ${missing.length} board(s): ${missing.join(" ")}`);
55+
}
56+
return { downloaded, missing };
5257
}

0 commit comments

Comments
 (0)