From b12a4732c4670887e07492b4be1211b5975e05ca Mon Sep 17 00:00:00 2001 From: Sami Dalouche Date: Tue, 24 Nov 2026 16:10:11 -0500 Subject: [PATCH] fix(build-dependency-cache): save with the region the restore ran with (SC-88184) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit cache-sbt and cache-uv save from their post step, and post steps run in the reverse order of their steps, so in a job that configures AWS credentials after the cache step, the save runs after configure-aws-credentials' post step. That step sets AWS_REGION and AWS_DEFAULT_REGION to "", since GitHub Actions can't unset a variable, and the aws CLI takes "" as the region. Every save in such a job failed, after narrative-snowflake-data-plane#395 moved to these actions: sbt dependency cache: couldn't save, so other runs restore an older one: couldn't check s3://…/aws-to-snowflake-monitoring/8b3ed52c….tar.zst: Invalid endpoint: https://s3..amazonaws.com The restore didn't, as it ran before configure-aws-credentials. It's the case in narrative-snowflake-data-plane's aws-to-snowflake-monitoring-test and data-plane-snowflake-test, and narrative-connectors' global-dependency-graph. - s3-cache.js: the restore keeps AWS_REGION and AWS_DEFAULT_REGION in the step's state, which the runner hands to the post step as STATE_, and the save sets each back when it's empty. A region still set is left alone. Failing to keep it only warns, since a save in a job without configure-aws-credentials doesn't need it. - The credentials that post step blanks are left as they are: the CLI skips empty ones and falls back to the runner's instance role, the one the restore used. With an empty region, `aws s3api head-object` fails with the error above; with the region set, it goes on to look for credentials. - tests/s3-cache.test.js: the restore keeps the region, keeps nothing without one and warns when it can't; the save gets a blanked region back and keeps one still set. - README.md says why the save needs it. Co-Authored-By: Claude Opus 5.5 --- build-dependency-cache/README.md | 5 ++ build-dependency-cache/s3-cache.js | 35 +++++++++++- build-dependency-cache/tests/s3-cache.test.js | 54 +++++++++++++++++++ 3 files changed, 92 insertions(+), 2 deletions(-) diff --git a/build-dependency-cache/README.md b/build-dependency-cache/README.md index 1b5dd5f..749c55c 100644 --- a/build-dependency-cache/README.md +++ b/build-dependency-cache/README.md @@ -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. diff --git a/build-dependency-cache/s3-cache.js b/build-dependency-cache/s3-cache.js index d1dd0d5..c24b3ae 100644 --- a/build-dependency-cache/s3-cache.js +++ b/build-dependency-cache/s3-cache.js @@ -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"); @@ -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_. 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) { diff --git a/build-dependency-cache/tests/s3-cache.test.js b/build-dependency-cache/tests/s3-cache.test.js index 4478d55..15b76c2 100644 --- a/build-dependency-cache/tests/s3-cache.test.js +++ b/build-dependency-cache/tests/s3-cache.test.js @@ -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); +});