From 9a34fefa1dfa8e495ad272e7b7ee73fa7ee3552c Mon Sep 17 00:00:00 2001 From: Herman Date: Wed, 1 Feb 2023 08:40:47 +0100 Subject: [PATCH] feature: allow diversification using cache-key-suffix --- README.md | 27 +++++++++++++++++++++++++++ action.yml | 2 ++ dist/setup/index.js | 8 +++++--- src/cache-restore.ts | 6 ++++-- src/main.ts | 4 +++- 5 files changed, 41 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d551d2c..bbfe929 100644 --- a/README.md +++ b/README.md @@ -153,6 +153,33 @@ steps: cache-dependency-path: subdir/go.sum - run: go run hello.go ``` + +**Caching for different build profiles** +Suppose you have multiple builds that depend on different dependencies, spread over multiple workflows or jobs. +If the same go.sum file is used, they will fight for the cache. Set `cache-key-suffix` to distinguish these caches. + +```yaml +steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v3 + with: + go-version: '1.17' + check-latest: true + cache: true + cache-key-suffix: hello + - run: go run hello.go + +# another workflow job +steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v3 + with: + go-version: '1.17' + check-latest: true + cache: true + cache-key-suffix: server + - run: go build -o /server ./cmd/ + ``` ## Getting go version from the go.mod file The `go-version-file` input accepts a path to a `go.mod` file or a `go.work` file that contains the version of Go to be used by a project. As the `go.mod` file contains only major and minor (e.g. 1.18) tags, the action will search for the latest available patch version sequentially in the runner's directory with the cached tools, in the [versions-manifest.json](https://github.com/actions/go-versions/blob/main/versions-manifest.json) file or at the go servers. diff --git a/action.yml b/action.yml index 64e3271..78dfe85 100644 --- a/action.yml +++ b/action.yml @@ -17,6 +17,8 @@ inputs: default: false cache-dependency-path: description: 'Used to specify the path to a dependency file - go.sum' + cache-key-suffix: + description: 'Used to diversify the cache if you have multiple build targets in the same module - e.g. cmd1, cmd2' architecture: description: 'Target architecture for Go to use. Examples: x86, x64. Will use system architecture by default.' outputs: diff --git a/dist/setup/index.js b/dist/setup/index.js index 40f31c5..3a6dd8e 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -63032,7 +63032,7 @@ const path_1 = __importDefault(__nccwpck_require__(1017)); const fs_1 = __importDefault(__nccwpck_require__(7147)); const constants_1 = __nccwpck_require__(9042); const cache_utils_1 = __nccwpck_require__(1678); -const restoreCache = (versionSpec, packageManager, cacheDependencyPath) => __awaiter(void 0, void 0, void 0, function* () { +const restoreCache = (versionSpec, packageManager, cacheDependencyPath, cacheKeySuffix) => __awaiter(void 0, void 0, void 0, function* () { const packageManagerInfo = yield cache_utils_1.getPackageManagerInfo(packageManager); const platform = process.env.RUNNER_OS; const cachePaths = yield cache_utils_1.getCacheDirectoryPath(packageManagerInfo); @@ -63043,7 +63043,8 @@ const restoreCache = (versionSpec, packageManager, cacheDependencyPath) => __awa if (!fileHash) { throw new Error('Some specified paths were not resolved, unable to cache dependencies.'); } - const primaryKey = `setup-go-${platform}-go-${versionSpec}-${fileHash}`; + const primaryKey = `setup-go-${platform}-go-${versionSpec}-${fileHash}${cacheKeySuffix || + ''}`; core.debug(`primary key is ${primaryKey}`); core.saveState(constants_1.State.CachePrimaryKey, primaryKey); const cacheKey = yield cache.restoreCache(cachePaths, primaryKey); @@ -63607,7 +63608,8 @@ function run() { if (cache && cache_utils_1.isCacheFeatureAvailable()) { const packageManager = 'default'; const cacheDependencyPath = core.getInput('cache-dependency-path'); - yield cache_restore_1.restoreCache(parseGoVersion(goVersion), packageManager, cacheDependencyPath); + const cacheKeySuffix = core.getInput('cache-key-suffix'); + yield cache_restore_1.restoreCache(parseGoVersion(goVersion), packageManager, cacheDependencyPath, cacheKeySuffix); } // add problem matchers const matchersPath = path_1.default.join(__dirname, '../..', 'matchers.json'); diff --git a/src/cache-restore.ts b/src/cache-restore.ts index 930122c..c761306 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, + cacheKeySuffix?: string ) => { const packageManagerInfo = await getPackageManagerInfo(packageManager); const platform = process.env.RUNNER_OS; @@ -29,7 +30,8 @@ export const restoreCache = async ( ); } - const primaryKey = `setup-go-${platform}-go-${versionSpec}-${fileHash}`; + const primaryKey = `setup-go-${platform}-go-${versionSpec}-${fileHash}${cacheKeySuffix || + ''}`; core.debug(`primary key is ${primaryKey}`); core.saveState(State.CachePrimaryKey, primaryKey); diff --git a/src/main.ts b/src/main.ts index 6cdb2e6..e36dd93 100644 --- a/src/main.ts +++ b/src/main.ts @@ -62,10 +62,12 @@ export async function run() { if (cache && isCacheFeatureAvailable()) { const packageManager = 'default'; const cacheDependencyPath = core.getInput('cache-dependency-path'); + const cacheKeySuffix = core.getInput('cache-key-suffix'); await restoreCache( parseGoVersion(goVersion), packageManager, - cacheDependencyPath + cacheDependencyPath, + cacheKeySuffix ); }