Unique primaryKey for cache based on buildTargetArch

This commit is contained in:
Anton Izmailov 2025-09-25 14:26:45 +02:00
parent c0137caad7
commit af7fba4129
4 changed files with 16 additions and 6 deletions

View File

@ -44,7 +44,8 @@ describe('restoreCache', () => {
await cacheRestore.restoreCache( await cacheRestore.restoreCache(
versionSpec, versionSpec,
packageManager, packageManager,
cacheDependencyPath cacheDependencyPath,
undefined
); );
}).rejects.toThrow( }).rejects.toThrow(
'Some specified paths were not resolved, unable to cache dependencies.' 'Some specified paths were not resolved, unable to cache dependencies.'
@ -69,7 +70,8 @@ describe('restoreCache', () => {
await cacheRestore.restoreCache( await cacheRestore.restoreCache(
versionSpec, versionSpec,
packageManager, packageManager,
cacheDependencyPath cacheDependencyPath,
undefined
); );
expect(infoSpy).toHaveBeenCalledWith(`Cache is not found`); expect(infoSpy).toHaveBeenCalledWith(`Cache is not found`);
}); });
@ -92,7 +94,8 @@ describe('restoreCache', () => {
await cacheRestore.restoreCache( await cacheRestore.restoreCache(
versionSpec, versionSpec,
packageManager, packageManager,
cacheDependencyPath cacheDependencyPath,
undefined
); );
expect(setOutputSpy).toHaveBeenCalledWith('cache-hit', true); expect(setOutputSpy).toHaveBeenCalledWith('cache-hit', true);
}); });

View File

@ -17,6 +17,8 @@ inputs:
default: true default: true
cache-dependency-path: cache-dependency-path:
description: 'Used to specify the path to a dependency file - go.sum' 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: architecture:
description: 'Target architecture for Go to use. Examples: x86, x64. Will use system architecture by default.' description: 'Target architecture for Go to use. Examples: x86, x64. Will use system architecture by default.'
outputs: outputs:

View File

@ -11,7 +11,8 @@ import {getCacheDirectoryPath, getPackageManagerInfo} from './cache-utils';
export const restoreCache = async ( export const restoreCache = async (
versionSpec: string, versionSpec: string,
packageManager: string, packageManager: string,
cacheDependencyPath?: string cacheDependencyPath?: string,
buildTarget?: string
) => { ) => {
const packageManagerInfo = await getPackageManagerInfo(packageManager); const packageManagerInfo = await getPackageManagerInfo(packageManager);
const platform = process.env.RUNNER_OS; const platform = process.env.RUNNER_OS;
@ -32,7 +33,9 @@ export const restoreCache = async (
const linuxVersion = const linuxVersion =
process.env.RUNNER_OS === 'Linux' ? `${process.env.ImageOS}-` : ''; 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.debug(`primary key is ${primaryKey}`);
core.saveState(State.CachePrimaryKey, primaryKey); core.saveState(State.CachePrimaryKey, primaryKey);

View File

@ -20,6 +20,7 @@ export async function run() {
setGoToolchain(); setGoToolchain();
const cache = core.getBooleanInput('cache'); const cache = core.getBooleanInput('cache');
const buildTarget = core.getInput('buildTarget');
core.info(`Setup go version spec ${versionSpec}`); core.info(`Setup go version spec ${versionSpec}`);
let arch = core.getInput('architecture') as Architecture; let arch = core.getInput('architecture') as Architecture;
@ -73,7 +74,8 @@ export async function run() {
await restoreCache( await restoreCache(
parseGoVersion(goVersion), parseGoVersion(goVersion),
packageManager, packageManager,
cacheDependencyPath cacheDependencyPath,
buildTarget
); );
} catch (error) { } catch (error) {
core.warning(`Restore cache failed: ${(error as Error).message}`); core.warning(`Restore cache failed: ${(error as Error).message}`);