diff --git a/README.md b/README.md index eb4f9dd23..bb7ece069 100644 --- a/README.md +++ b/README.md @@ -153,6 +153,8 @@ 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-suffix` 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 +171,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-suffix: 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-suffix: 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 diff --git a/action.yml b/action.yml index e3a21c1f3..3633fc2e0 100644 --- a/action.yml +++ b/action.yml @@ -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: diff --git a/dist/setup/index.js b/dist/setup/index.js index f298d5c32..c575a6a42 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -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); diff --git a/src/cache-restore.ts b/src/cache-restore.ts index 14f84c448..adfc9581e 100644 --- a/src/cache-restore.ts +++ b/src/cache-restore.ts @@ -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);