From 993ce21c74e3e961b202f6f983027b450f97d646 Mon Sep 17 00:00:00 2001 From: Jasperhino Date: Thu, 21 May 2026 15:40:39 +0200 Subject: [PATCH] Add env fallback for GCS cache bucket --- __tests__/actionUtils.test.ts | 34 ++++++++++++++++++++++++++++++++++ action.yml | 2 +- dist/restore-only/index.js | 10 +++++++--- dist/restore/index.js | 10 +++++++--- dist/save-only/index.js | 10 +++++++--- dist/save/index.js | 10 +++++++--- restore/action.yml | 2 +- save/action.yml | 2 +- src/utils/actionUtils.ts | 6 +++++- src/utils/gcsCache.ts | 6 +++--- src/utils/testUtils.ts | 1 + 11 files changed, 74 insertions(+), 19 deletions(-) diff --git a/__tests__/actionUtils.test.ts b/__tests__/actionUtils.test.ts index c2e6823..269da8e 100644 --- a/__tests__/actionUtils.test.ts +++ b/__tests__/actionUtils.test.ts @@ -203,6 +203,40 @@ test("getInputAsBool throws if required and value missing", () => { ).toThrowError(); }); +test("getGCSBucket returns gcs-bucket input when provided", () => { + try { + testUtils.setInput("gcs-bucket", "input-bucket"); + process.env["CULA_CACHE_GCS_BUCKET"] = "env-bucket"; + + expect(actionUtils.getGCSBucket()).toBe("input-bucket"); + } finally { + testUtils.clearInputs(); + delete process.env["CULA_CACHE_GCS_BUCKET"]; + } +}); + +test("getGCSBucket falls back to CULA_CACHE_GCS_BUCKET", () => { + try { + process.env["CULA_CACHE_GCS_BUCKET"] = "env-bucket"; + + expect(actionUtils.getGCSBucket()).toBe("env-bucket"); + } finally { + delete process.env["CULA_CACHE_GCS_BUCKET"]; + } +}); + +test("getGCSBucket returns empty string without input or environment variable", () => { + try { + testUtils.clearInputs(); + delete process.env["CULA_CACHE_GCS_BUCKET"]; + + expect(actionUtils.getGCSBucket()).toBe(""); + } finally { + testUtils.clearInputs(); + delete process.env["CULA_CACHE_GCS_BUCKET"]; + } +}); + test("isCacheFeatureAvailable for ac enabled", () => { jest.spyOn(cache, "isFeatureAvailable").mockImplementation(() => true); diff --git a/action.yml b/action.yml index 0ceb653..5378462 100644 --- a/action.yml +++ b/action.yml @@ -27,7 +27,7 @@ inputs: default: 'false' required: false gcs-bucket: - description: 'Google Cloud Storage bucket name to use for caching. When provided, GCS will be used as the cache backend.' + description: 'Google Cloud Storage bucket name to use for caching. When provided, GCS will be used as the cache backend. Defaults to the CULA_CACHE_GCS_BUCKET environment variable when omitted.' required: false gcs-path-prefix: description: 'Optional prefix path within the GCS bucket for cache files' diff --git a/dist/restore-only/index.js b/dist/restore-only/index.js index 5189aed..83a37f4 100644 --- a/dist/restore-only/index.js +++ b/dist/restore-only/index.js @@ -78328,6 +78328,7 @@ exports.isValidEvent = isValidEvent; exports.getInputAsArray = getInputAsArray; exports.getInputAsInt = getInputAsInt; exports.getInputAsBool = getInputAsBool; +exports.getGCSBucket = getGCSBucket; exports.isGCSAvailable = isGCSAvailable; exports.isCacheFeatureAvailable = isCacheFeatureAvailable; const cache = __importStar(__nccwpck_require__(5116)); @@ -78374,10 +78375,13 @@ function getInputAsBool(name, options) { const result = core.getInput(name, options); return result.toLowerCase() === "true"; } +function getGCSBucket() { + return core.getInput(constants_1.Inputs.GCSBucket) || process.env["CULA_CACHE_GCS_BUCKET"] || ""; +} // Check if GCS is configured and available function isGCSAvailable() { try { - const bucket = core.getInput(constants_1.Inputs.GCSBucket); + const bucket = getGCSBucket(); if (!bucket) { core.info("GCS bucket name not provided, falling back to GitHub cache"); return false; @@ -78548,7 +78552,7 @@ function restoreFromGCS(_paths_1, primaryKey_1) { if (!storage) { return undefined; } - const bucket = core.getInput(constants_1.Inputs.GCSBucket); + const bucket = (0, actionUtils_1.getGCSBucket)(); const pathPrefix = core.getInput(constants_1.Inputs.GCSPathPrefix) || DEFAULT_PATH_PREFIX; const compressionMethod = yield utils.getCompressionMethod(); const archiveFolder = yield utils.createTempDirectory(); @@ -78605,7 +78609,7 @@ function saveToGCS(paths, key) { if (!storage) { return undefined; } - const bucket = core.getInput(constants_1.Inputs.GCSBucket); + const bucket = (0, actionUtils_1.getGCSBucket)(); const pathPrefix = core.getInput(constants_1.Inputs.GCSPathPrefix) || DEFAULT_PATH_PREFIX; const compressionMethod = yield utils.getCompressionMethod(); const cachePaths = yield utils.resolvePaths(paths); diff --git a/dist/restore/index.js b/dist/restore/index.js index c896769..37406ce 100644 --- a/dist/restore/index.js +++ b/dist/restore/index.js @@ -78328,6 +78328,7 @@ exports.isValidEvent = isValidEvent; exports.getInputAsArray = getInputAsArray; exports.getInputAsInt = getInputAsInt; exports.getInputAsBool = getInputAsBool; +exports.getGCSBucket = getGCSBucket; exports.isGCSAvailable = isGCSAvailable; exports.isCacheFeatureAvailable = isCacheFeatureAvailable; const cache = __importStar(__nccwpck_require__(5116)); @@ -78374,10 +78375,13 @@ function getInputAsBool(name, options) { const result = core.getInput(name, options); return result.toLowerCase() === "true"; } +function getGCSBucket() { + return core.getInput(constants_1.Inputs.GCSBucket) || process.env["CULA_CACHE_GCS_BUCKET"] || ""; +} // Check if GCS is configured and available function isGCSAvailable() { try { - const bucket = core.getInput(constants_1.Inputs.GCSBucket); + const bucket = getGCSBucket(); if (!bucket) { core.info("GCS bucket name not provided, falling back to GitHub cache"); return false; @@ -78548,7 +78552,7 @@ function restoreFromGCS(_paths_1, primaryKey_1) { if (!storage) { return undefined; } - const bucket = core.getInput(constants_1.Inputs.GCSBucket); + const bucket = (0, actionUtils_1.getGCSBucket)(); const pathPrefix = core.getInput(constants_1.Inputs.GCSPathPrefix) || DEFAULT_PATH_PREFIX; const compressionMethod = yield utils.getCompressionMethod(); const archiveFolder = yield utils.createTempDirectory(); @@ -78605,7 +78609,7 @@ function saveToGCS(paths, key) { if (!storage) { return undefined; } - const bucket = core.getInput(constants_1.Inputs.GCSBucket); + const bucket = (0, actionUtils_1.getGCSBucket)(); const pathPrefix = core.getInput(constants_1.Inputs.GCSPathPrefix) || DEFAULT_PATH_PREFIX; const compressionMethod = yield utils.getCompressionMethod(); const cachePaths = yield utils.resolvePaths(paths); diff --git a/dist/save-only/index.js b/dist/save-only/index.js index b368925..72e3ba2 100644 --- a/dist/save-only/index.js +++ b/dist/save-only/index.js @@ -78341,6 +78341,7 @@ exports.isValidEvent = isValidEvent; exports.getInputAsArray = getInputAsArray; exports.getInputAsInt = getInputAsInt; exports.getInputAsBool = getInputAsBool; +exports.getGCSBucket = getGCSBucket; exports.isGCSAvailable = isGCSAvailable; exports.isCacheFeatureAvailable = isCacheFeatureAvailable; const cache = __importStar(__nccwpck_require__(5116)); @@ -78387,10 +78388,13 @@ function getInputAsBool(name, options) { const result = core.getInput(name, options); return result.toLowerCase() === "true"; } +function getGCSBucket() { + return core.getInput(constants_1.Inputs.GCSBucket) || process.env["CULA_CACHE_GCS_BUCKET"] || ""; +} // Check if GCS is configured and available function isGCSAvailable() { try { - const bucket = core.getInput(constants_1.Inputs.GCSBucket); + const bucket = getGCSBucket(); if (!bucket) { core.info("GCS bucket name not provided, falling back to GitHub cache"); return false; @@ -78561,7 +78565,7 @@ function restoreFromGCS(_paths_1, primaryKey_1) { if (!storage) { return undefined; } - const bucket = core.getInput(constants_1.Inputs.GCSBucket); + const bucket = (0, actionUtils_1.getGCSBucket)(); const pathPrefix = core.getInput(constants_1.Inputs.GCSPathPrefix) || DEFAULT_PATH_PREFIX; const compressionMethod = yield utils.getCompressionMethod(); const archiveFolder = yield utils.createTempDirectory(); @@ -78618,7 +78622,7 @@ function saveToGCS(paths, key) { if (!storage) { return undefined; } - const bucket = core.getInput(constants_1.Inputs.GCSBucket); + const bucket = (0, actionUtils_1.getGCSBucket)(); const pathPrefix = core.getInput(constants_1.Inputs.GCSPathPrefix) || DEFAULT_PATH_PREFIX; const compressionMethod = yield utils.getCompressionMethod(); const cachePaths = yield utils.resolvePaths(paths); diff --git a/dist/save/index.js b/dist/save/index.js index 6d8cf55..cd40ceb 100644 --- a/dist/save/index.js +++ b/dist/save/index.js @@ -78341,6 +78341,7 @@ exports.isValidEvent = isValidEvent; exports.getInputAsArray = getInputAsArray; exports.getInputAsInt = getInputAsInt; exports.getInputAsBool = getInputAsBool; +exports.getGCSBucket = getGCSBucket; exports.isGCSAvailable = isGCSAvailable; exports.isCacheFeatureAvailable = isCacheFeatureAvailable; const cache = __importStar(__nccwpck_require__(5116)); @@ -78387,10 +78388,13 @@ function getInputAsBool(name, options) { const result = core.getInput(name, options); return result.toLowerCase() === "true"; } +function getGCSBucket() { + return core.getInput(constants_1.Inputs.GCSBucket) || process.env["CULA_CACHE_GCS_BUCKET"] || ""; +} // Check if GCS is configured and available function isGCSAvailable() { try { - const bucket = core.getInput(constants_1.Inputs.GCSBucket); + const bucket = getGCSBucket(); if (!bucket) { core.info("GCS bucket name not provided, falling back to GitHub cache"); return false; @@ -78561,7 +78565,7 @@ function restoreFromGCS(_paths_1, primaryKey_1) { if (!storage) { return undefined; } - const bucket = core.getInput(constants_1.Inputs.GCSBucket); + const bucket = (0, actionUtils_1.getGCSBucket)(); const pathPrefix = core.getInput(constants_1.Inputs.GCSPathPrefix) || DEFAULT_PATH_PREFIX; const compressionMethod = yield utils.getCompressionMethod(); const archiveFolder = yield utils.createTempDirectory(); @@ -78618,7 +78622,7 @@ function saveToGCS(paths, key) { if (!storage) { return undefined; } - const bucket = core.getInput(constants_1.Inputs.GCSBucket); + const bucket = (0, actionUtils_1.getGCSBucket)(); const pathPrefix = core.getInput(constants_1.Inputs.GCSPathPrefix) || DEFAULT_PATH_PREFIX; const compressionMethod = yield utils.getCompressionMethod(); const cachePaths = yield utils.resolvePaths(paths); diff --git a/restore/action.yml b/restore/action.yml index ef3a6a1..15ae1ab 100644 --- a/restore/action.yml +++ b/restore/action.yml @@ -24,7 +24,7 @@ inputs: default: 'false' required: false gcs-bucket: - description: 'Google Cloud Storage bucket name to use for caching. When provided, GCS will be used as the cache backend.' + description: 'Google Cloud Storage bucket name to use for caching. When provided, GCS will be used as the cache backend. Defaults to the CULA_CACHE_GCS_BUCKET environment variable when omitted.' required: false gcs-path-prefix: description: 'Optional prefix path within the GCS bucket for cache files' diff --git a/save/action.yml b/save/action.yml index 19a0fe2..c450da8 100644 --- a/save/action.yml +++ b/save/action.yml @@ -16,7 +16,7 @@ inputs: default: 'false' required: false gcs-bucket: - description: 'Google Cloud Storage bucket name to use for caching. When provided, GCS will be used as the cache backend.' + description: 'Google Cloud Storage bucket name to use for caching. When provided, GCS will be used as the cache backend. Defaults to the CULA_CACHE_GCS_BUCKET environment variable when omitted.' required: false gcs-path-prefix: description: 'Optional prefix path within the GCS bucket for cache files' diff --git a/src/utils/actionUtils.ts b/src/utils/actionUtils.ts index 26531d4..3206b0e 100644 --- a/src/utils/actionUtils.ts +++ b/src/utils/actionUtils.ts @@ -66,10 +66,14 @@ export function getInputAsBool( return result.toLowerCase() === "true"; } +export function getGCSBucket(): string { + return core.getInput(Inputs.GCSBucket) || process.env["CULA_CACHE_GCS_BUCKET"] || ""; +} + // Check if GCS is configured and available export function isGCSAvailable(): boolean { try { - const bucket = core.getInput(Inputs.GCSBucket); + const bucket = getGCSBucket(); if (!bucket) { core.info( "GCS bucket name not provided, falling back to GitHub cache" diff --git a/src/utils/gcsCache.ts b/src/utils/gcsCache.ts index e631fdd..c1ffd3e 100644 --- a/src/utils/gcsCache.ts +++ b/src/utils/gcsCache.ts @@ -12,7 +12,7 @@ import { Storage } from "@google-cloud/storage"; import * as path from "path"; import { Inputs } from "../constants"; -import { isGCSAvailable } from "./actionUtils"; +import { getGCSBucket, isGCSAvailable } from "./actionUtils"; const DEFAULT_PATH_PREFIX = "github-cache"; @@ -116,7 +116,7 @@ async function restoreFromGCS( return undefined; } - const bucket = core.getInput(Inputs.GCSBucket); + const bucket = getGCSBucket(); const pathPrefix = core.getInput(Inputs.GCSPathPrefix) || DEFAULT_PATH_PREFIX; const compressionMethod = await utils.getCompressionMethod(); @@ -202,7 +202,7 @@ async function saveToGCS( return undefined; } - const bucket = core.getInput(Inputs.GCSBucket); + const bucket = getGCSBucket(); const pathPrefix = core.getInput(Inputs.GCSPathPrefix) || DEFAULT_PATH_PREFIX; const compressionMethod = await utils.getCompressionMethod(); diff --git a/src/utils/testUtils.ts b/src/utils/testUtils.ts index ba0670b..2c06427 100644 --- a/src/utils/testUtils.ts +++ b/src/utils/testUtils.ts @@ -42,4 +42,5 @@ export function clearInputs(): void { delete process.env[getInputName(Inputs.EnableCrossOsArchive)]; delete process.env[getInputName(Inputs.FailOnCacheMiss)]; delete process.env[getInputName(Inputs.LookupOnly)]; + delete process.env[getInputName(Inputs.GCSBucket)]; }