From af7fba4129722ab6a2786f8c90b08c87abde8bb7 Mon Sep 17 00:00:00 2001 From: Anton Izmailov Date: Thu, 25 Sep 2025 14:26:45 +0200 Subject: [PATCH] Unique primaryKey for cache based on buildTargetArch --- __tests__/cache-restore.test.ts | 9 ++++++--- action.yml | 2 ++ src/cache-restore.ts | 7 +++++-- src/main.ts | 4 +++- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/__tests__/cache-restore.test.ts b/__tests__/cache-restore.test.ts index 07dc97a..782e9c7 100644 --- a/__tests__/cache-restore.test.ts +++ b/__tests__/cache-restore.test.ts @@ -44,7 +44,8 @@ describe('restoreCache', () => { await cacheRestore.restoreCache( versionSpec, packageManager, - cacheDependencyPath + cacheDependencyPath, + undefined ); }).rejects.toThrow( 'Some specified paths were not resolved, unable to cache dependencies.' @@ -69,7 +70,8 @@ describe('restoreCache', () => { await cacheRestore.restoreCache( versionSpec, packageManager, - cacheDependencyPath + cacheDependencyPath, + undefined ); expect(infoSpy).toHaveBeenCalledWith(`Cache is not found`); }); @@ -92,7 +94,8 @@ describe('restoreCache', () => { await cacheRestore.restoreCache( versionSpec, packageManager, - cacheDependencyPath + cacheDependencyPath, + undefined ); expect(setOutputSpy).toHaveBeenCalledWith('cache-hit', true); }); diff --git a/action.yml b/action.yml index 9946e47..2a57a4e 100644 --- a/action.yml +++ b/action.yml @@ -17,6 +17,8 @@ inputs: default: true cache-dependency-path: description: 'Used to specify the path to a dependency file - go.sum' + buildTarget: + description: 'Optional build target architecture (for example arm64 or amd64) used to scope the cache key.' architecture: description: 'Target architecture for Go to use. Examples: x86, x64. Will use system architecture by default.' outputs: diff --git a/src/cache-restore.ts b/src/cache-restore.ts index 18d930b..b7c6647 100644 --- a/src/cache-restore.ts +++ b/src/cache-restore.ts @@ -11,7 +11,8 @@ import {getCacheDirectoryPath, getPackageManagerInfo} from './cache-utils'; export const restoreCache = async ( versionSpec: string, packageManager: string, - cacheDependencyPath?: string + cacheDependencyPath?: string, + buildTarget?: string ) => { const packageManagerInfo = await getPackageManagerInfo(packageManager); const platform = process.env.RUNNER_OS; @@ -32,7 +33,9 @@ export const restoreCache = async ( const linuxVersion = process.env.RUNNER_OS === 'Linux' ? `${process.env.ImageOS}-` : ''; - const primaryKey = `setup-go-${platform}-${arch}-${linuxVersion}go-${versionSpec}-${fileHash}`; + const sanitizedBuildTarget = buildTarget?.trim(); + const targetSegment = sanitizedBuildTarget ? `${sanitizedBuildTarget}-` : ''; + const primaryKey = `setup-go-${platform}-${arch}-${targetSegment}${linuxVersion}go-${versionSpec}-${fileHash}`; core.debug(`primary key is ${primaryKey}`); core.saveState(State.CachePrimaryKey, primaryKey); diff --git a/src/main.ts b/src/main.ts index 26939ee..817d802 100644 --- a/src/main.ts +++ b/src/main.ts @@ -20,6 +20,7 @@ export async function run() { setGoToolchain(); const cache = core.getBooleanInput('cache'); + const buildTarget = core.getInput('buildTarget'); core.info(`Setup go version spec ${versionSpec}`); let arch = core.getInput('architecture') as Architecture; @@ -73,7 +74,8 @@ export async function run() { await restoreCache( parseGoVersion(goVersion), packageManager, - cacheDependencyPath + cacheDependencyPath, + buildTarget ); } catch (error) { core.warning(`Restore cache failed: ${(error as Error).message}`);