Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add cache-key-prefix option #366

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -169,6 +170,30 @@ steps:
- run: go run hello.go
```

**Caching with 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
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ 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.'
default: 'setup-go'
architecture:
description: 'Target architecture for Go to use. Examples: x86, x64. Will use system architecture by default.'
outputs:
Expand Down
3 changes: 2 additions & 1 deletion dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') || 'setup-go';
const primaryKey = `${cacheKeyPrefix}-${platform}-go-${versionSpec}-${fileHash}`;
core.debug(`primary key is ${primaryKey}`);
core.saveState(constants_1.State.CachePrimaryKey, primaryKey);
const cacheKey = yield cache.restoreCache(cachePaths, primaryKey);
Expand Down
3 changes: 2 additions & 1 deletion src/cache-restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export const restoreCache = async (
);
}

const primaryKey = `setup-go-${platform}-go-${versionSpec}-${fileHash}`;
const cacheKeyPrefix = core.getInput('cache-key-prefix') || 'setup-go';
const primaryKey = `${cacheKeyPrefix}-${platform}-go-${versionSpec}-${fileHash}`;
core.debug(`primary key is ${primaryKey}`);

core.saveState(State.CachePrimaryKey, primaryKey);
Expand Down