feat: Add `cache-key-prefix` option

This commit is contained in:
erezrokah 2023-04-05 16:19:37 +03:00
parent 41c2024c46
commit 089c1d9041
No known key found for this signature in database
GPG Key ID: EA610DCA9B4C557B
4 changed files with 31 additions and 2 deletions

View File

@ -153,6 +153,7 @@ The `cache` input is optional, and caching is turned on by default.
The action defaults to search for the dependency file - go.sum in the repository root, and uses its hash as a part of
the cache key. Use `cache-dependency-path` input for cases when multiple dependency files are used, or they are located
in different subdirectories.
If you use `setup-go` with caching from multiple concurrent jobs, you might want to specify the `cache-key-prefix` input to ensure that the cache is not shared between jobs.
If some problem that prevents success caching happens then the action issues the warning in the log and continues the execution of the pipeline.
@ -169,6 +170,30 @@ steps:
- run: go run hello.go
```
**Caching wwith multiple concurrent jobs**
```yaml
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: "1.20"
cache-key-prefix: build-cache-
- run: go build
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: "1.20"
cache-key-prefix: test-cache-
- run: go test ./...
```
## 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

View File

@ -17,6 +17,8 @@ inputs:
default: true
cache-dependency-path:
description: 'Used to specify the path to a dependency file - go.sum'
cache-key-prefix:
description: 'A prefix to add to the cache key. Useful if you have multiple concurrent jobs that need different caches.'
architecture:
description: 'Target architecture for Go to use. Examples: x86, x64. Will use system architecture by default.'
outputs:

3
dist/setup/index.js vendored
View File

@ -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 cacheKeyPrefix = core.getInput('cache-key-prefix') || '';
const primaryKey = `${cacheKeyPrefix}setup-go-${platform}-go-${versionSpec}-${fileHash}`;
core.debug(`primary key is ${primaryKey}`);
core.saveState(constants_1.State.CachePrimaryKey, primaryKey);
const cacheKey = yield cache.restoreCache(cachePaths, primaryKey);

View File

@ -29,7 +29,8 @@ export const restoreCache = async (
);
}
const primaryKey = `setup-go-${platform}-go-${versionSpec}-${fileHash}`;
const cacheKeyPrefix = core.getInput('cache-key-prefix') || '';
const primaryKey = `${cacheKeyPrefix}setup-go-${platform}-go-${versionSpec}-${fileHash}`;
core.debug(`primary key is ${primaryKey}`);
core.saveState(State.CachePrimaryKey, primaryKey);