Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions build-dependency-cache/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ build or `uv sync` has fetched everything, as `actions/cache` does. Composite ac
every workflow would need a second step, placed last, to save. Their scripts only start `s3_cache.py`, through
`s3-cache.js`.

The save then runs after the post steps of any action placed below the cache step. `configure-aws-credentials` sets
`AWS_REGION` and `AWS_DEFAULT_REGION` to empty strings in its post step, and the aws CLI takes an empty region as the
region, so the restore keeps the region it ran with in the step's state and the save takes it back. The credentials
that post step blanks don't matter: the CLI skips them and uses the runner's instance role, as the restore did.

`setup-mise` has to be composite to call `jdx/mise-action`, and saves right after the install instead: mise's folder is
complete then, a job that fails later still saves it, and later steps can't add to it. Saving at the end would let the
job's real work start a few seconds sooner on the pushes that save; its save step's comment explains the trade-off.
Expand Down
35 changes: 33 additions & 2 deletions build-dependency-cache/s3-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//
// The process's environment, spawnSync and console.log are parameters, so tests/s3-cache.test.js can pass fakes.
const { spawnSync } = require("node:child_process");
const { appendFileSync } = require("node:fs");
const path = require("node:path");

const SCRIPT = path.join(__dirname, "s3_cache.py");
Expand All @@ -26,17 +27,47 @@ function shouldSave(env = process.env) {
);
}

// The variables the aws CLI reads its region from. configure-aws-credentials' post step sets both to "" when the job
// ends, and post steps run in the reverse order of their steps, so a cache step placed before it saves after that.
// GitHub Actions can't unset a variable, and the aws CLI takes "" as the region: it calls https://s3..amazonaws.com.
// The credentials it blanks too are fine: the CLI skips empty ones and falls back to the runner's instance role,
// the one the restore used before configure-aws-credentials ran.
const REGION_VARIABLES = ["AWS_REGION", "AWS_DEFAULT_REGION"];

// Keeps the region the restore ran with in the step's state, which the runner hands to the post step as
// STATE_<name>. Failing to only means a save may run without a region, so it warns.
function rememberRegion(env, append, log) {
const lines = REGION_VARIABLES.filter((name) => env[name]).map((name) => `${name}=${env[name]}\n`);
if (!env.GITHUB_STATE || lines.length === 0) return;
try {
append(env.GITHUB_STATE, lines.join(""));
} catch (error) {
log(`::warning::couldn't keep the AWS region for the save: ${error.message}`);
}
}

// env, with each region variable that's now empty set back to the value the restore ran with.
function withRestoredRegion(env) {
const restored = REGION_VARIABLES.filter((name) => !env[name] && env[`STATE_${name}`]).map((name) => [
name,
env[`STATE_${name}`],
]);
return { ...env, ...Object.fromEntries(restored) };
}

// Runs s3_cache.py and returns the step's exit status. scope is the folder that keeps the job's archives apart: an
// aggregate for sbt, the project for uv.
function run(
kind,
mode,
scope,
{ env = process.env, spawn = spawnSync, log = console.log } = {},
{ env = process.env, spawn = spawnSync, log = console.log, append = appendFileSync } = {},
) {
if (mode === "restore") rememberRegion(env, append, log);
const scriptEnv = mode === "save" ? withRestoredRegion(env) : env;
const result = spawn("python3", [SCRIPT, mode, "--kind", kind], {
stdio: "inherit",
env: { ...env, S3_CACHE_URL: input("url", env), S3_CACHE_SCOPE: scope },
env: { ...scriptEnv, S3_CACHE_URL: input("url", env), S3_CACHE_SCOPE: scope },
});
const failed = Boolean(result.error) || result.status !== 0;
if (failed) {
Expand Down
54 changes: 54 additions & 0 deletions build-dependency-cache/tests/s3-cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,57 @@ test("saveIfAsked runs a save only when asked, on main", () => {
assert.deepEqual(saved.calls[0].args.slice(1), ["save", "--kind", "sbt"]);
assert.equal(saved.calls[0].env.S3_CACHE_SCOPE, "agg");
});

// Records each append to a file.
function fakeAppend() {
const writes = [];
return { writes, append: (file, text) => writes.push({ file, text }) };
}

test("a restore keeps the region it ran with in the step's state", () => {
const { writes, append } = fakeAppend();
const env = { GITHUB_STATE: "/state", AWS_REGION: "us-east-1", AWS_DEFAULT_REGION: "us-east-1" };
run("sbt", "restore", "", { env, spawn: fakeSpawn({ status: 0 }).spawn, log: () => {}, append });
assert.deepEqual(writes, [{ file: "/state", text: "AWS_REGION=us-east-1\nAWS_DEFAULT_REGION=us-east-1\n" }]);
});

test("a restore without a region keeps nothing", () => {
const { writes, append } = fakeAppend();
run("sbt", "restore", "", { env: { GITHUB_STATE: "/state" }, spawn: fakeSpawn({ status: 0 }).spawn, append });
assert.deepEqual(writes, []);
});

test("a restore that can't keep the region warns and still restores", () => {
const lines = [];
const { calls, spawn } = fakeSpawn({ status: 0 });
const append = () => {
throw new Error("EACCES");
};
const env = { GITHUB_STATE: "/state", AWS_REGION: "us-east-1" };
assert.equal(run("sbt", "restore", "", { env, spawn, log: (line) => lines.push(line), append }), 0);
assert.equal(calls.length, 1);
assert.match(lines[0], /^::warning::couldn't keep the AWS region for the save: EACCES$/);
});

test("a save gets back the region configure-aws-credentials blanked", () => {
const { calls, spawn } = fakeSpawn({ status: 0 });
const env = {
...MAIN,
INPUT_SAVE: "true",
AWS_REGION: "",
AWS_DEFAULT_REGION: "",
STATE_AWS_REGION: "us-east-1",
STATE_AWS_DEFAULT_REGION: "us-east-1",
};
saveIfAsked("sbt", "", { env, spawn });
assert.equal(calls[0].env.AWS_REGION, "us-east-1");
assert.equal(calls[0].env.AWS_DEFAULT_REGION, "us-east-1");
});

test("a save keeps a region that's still set", () => {
const { calls, spawn } = fakeSpawn({ status: 0 });
const env = { ...MAIN, INPUT_SAVE: "true", AWS_REGION: "eu-west-1", STATE_AWS_REGION: "us-east-1" };
saveIfAsked("sbt", "", { env, spawn });
assert.equal(calls[0].env.AWS_REGION, "eu-west-1");
assert.equal(calls[0].env.AWS_DEFAULT_REGION, undefined);
});