diff --git a/build-dependency-cache/README.md b/build-dependency-cache/README.md index 749c55c..41833df 100644 --- a/build-dependency-cache/README.md +++ b/build-dependency-cache/README.md @@ -93,7 +93,8 @@ every workflow would need a second step, placed last, to save. Their scripts onl 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 +region, so the restore keeps the region it ran with in the step's state, and the save takes it back, or unsets the +variables when the restore ran without a region (a job that configures credentials only after the cache step). 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 diff --git a/build-dependency-cache/s3-cache.js b/build-dependency-cache/s3-cache.js index c24b3ae..f058910 100644 --- a/build-dependency-cache/s3-cache.js +++ b/build-dependency-cache/s3-cache.js @@ -46,13 +46,16 @@ function rememberRegion(env, append, log) { } } -// env, with each region variable that's now empty set back to the value the restore ran with. +// env, with each region variable that's now empty as the restore saw it: set back to the value it had then, or +// unset when it had none. A job that configures AWS credentials only after the cache step restores with no region, +// which the CLI resolves itself; the cleanup then turns that into "", which it can't. 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) }; + const restored = { ...env }; + for (const name of REGION_VARIABLES.filter((name) => !env[name])) { + if (env[`STATE_${name}`]) restored[name] = env[`STATE_${name}`]; + else delete restored[name]; + } + return restored; } // Runs s3_cache.py and returns the step's exit status. scope is the folder that keeps the job's archives apart: an diff --git a/build-dependency-cache/tests/s3-cache.test.js b/build-dependency-cache/tests/s3-cache.test.js index 15b76c2..cd09632 100644 --- a/build-dependency-cache/tests/s3-cache.test.js +++ b/build-dependency-cache/tests/s3-cache.test.js @@ -119,6 +119,14 @@ test("a save gets back the region configure-aws-credentials blanked", () => { assert.equal(calls[0].env.AWS_DEFAULT_REGION, "us-east-1"); }); +test("a save unsets a region that was unset when the restore ran", () => { + const { calls, spawn } = fakeSpawn({ status: 0 }); + const env = { ...MAIN, INPUT_SAVE: "true", AWS_REGION: "", AWS_DEFAULT_REGION: "" }; + saveIfAsked("sbt", "", { env, spawn }); + assert.equal("AWS_REGION" in calls[0].env, false); + assert.equal("AWS_DEFAULT_REGION" in calls[0].env, false); +}); + 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" };