feature: allow diversification using cache-key-suffix

pull/325/head
Herman 2 years ago
parent a3d889c34c
commit 9a34fefa1d
No known key found for this signature in database
GPG Key ID: 031BD73ED19A6BBA

@ -153,6 +153,33 @@ steps:
cache-dependency-path: subdir/go.sum cache-dependency-path: subdir/go.sum
- run: go run hello.go - 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 ## 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. 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.

@ -17,6 +17,8 @@ inputs:
default: false default: false
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'
cache-key-suffix:
description: 'Used to diversify the cache if you have multiple build targets in the same module - e.g. cmd1, cmd2'
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:

@ -63032,7 +63032,7 @@ const path_1 = __importDefault(__nccwpck_require__(1017));
const fs_1 = __importDefault(__nccwpck_require__(7147)); const fs_1 = __importDefault(__nccwpck_require__(7147));
const constants_1 = __nccwpck_require__(9042); const constants_1 = __nccwpck_require__(9042);
const cache_utils_1 = __nccwpck_require__(1678); 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 packageManagerInfo = yield cache_utils_1.getPackageManagerInfo(packageManager);
const platform = process.env.RUNNER_OS; const platform = process.env.RUNNER_OS;
const cachePaths = yield cache_utils_1.getCacheDirectoryPath(packageManagerInfo); const cachePaths = yield cache_utils_1.getCacheDirectoryPath(packageManagerInfo);
@ -63043,7 +63043,8 @@ const restoreCache = (versionSpec, packageManager, cacheDependencyPath) => __awa
if (!fileHash) { if (!fileHash) {
throw new Error('Some specified paths were not resolved, unable to cache dependencies.'); 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.debug(`primary key is ${primaryKey}`);
core.saveState(constants_1.State.CachePrimaryKey, primaryKey); core.saveState(constants_1.State.CachePrimaryKey, primaryKey);
const cacheKey = yield cache.restoreCache(cachePaths, primaryKey); const cacheKey = yield cache.restoreCache(cachePaths, primaryKey);
@ -63607,7 +63608,8 @@ function run() {
if (cache && cache_utils_1.isCacheFeatureAvailable()) { if (cache && cache_utils_1.isCacheFeatureAvailable()) {
const packageManager = 'default'; const packageManager = 'default';
const cacheDependencyPath = core.getInput('cache-dependency-path'); 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 // add problem matchers
const matchersPath = path_1.default.join(__dirname, '../..', 'matchers.json'); const matchersPath = path_1.default.join(__dirname, '../..', 'matchers.json');

@ -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,
cacheKeySuffix?: string
) => { ) => {
const packageManagerInfo = await getPackageManagerInfo(packageManager); const packageManagerInfo = await getPackageManagerInfo(packageManager);
const platform = process.env.RUNNER_OS; 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.debug(`primary key is ${primaryKey}`);
core.saveState(State.CachePrimaryKey, primaryKey); core.saveState(State.CachePrimaryKey, primaryKey);

@ -62,10 +62,12 @@ export async function run() {
if (cache && isCacheFeatureAvailable()) { if (cache && isCacheFeatureAvailable()) {
const packageManager = 'default'; const packageManager = 'default';
const cacheDependencyPath = core.getInput('cache-dependency-path'); const cacheDependencyPath = core.getInput('cache-dependency-path');
const cacheKeySuffix = core.getInput('cache-key-suffix');
await restoreCache( await restoreCache(
parseGoVersion(goVersion), parseGoVersion(goVersion),
packageManager, packageManager,
cacheDependencyPath cacheDependencyPath,
cacheKeySuffix
); );
} }

Loading…
Cancel
Save