From 089c1d90415194a316aa8c3f4d34ff7954387f4d Mon Sep 17 00:00:00 2001 From: erezrokah Date: Wed, 5 Apr 2023 16:19:37 +0300 Subject: [PATCH 1/6] feat: Add `cache-key-prefix` option --- README.md | 25 +++++++++++++++++++++++++ action.yml | 2 ++ dist/setup/index.js | 3 ++- src/cache-restore.ts | 3 ++- 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index eb4f9dd23..c14da1836 100644 --- a/README.md +++ b/README.md @@ -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 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); From acb2fd804097c1dffc2eb1075b3696da4b4bb27c Mon Sep 17 00:00:00 2001 From: Erez Rokah Date: Wed, 5 Apr 2023 16:29:18 +0300 Subject: [PATCH 2/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c14da1836..b0e14e08a 100644 --- a/README.md +++ b/README.md @@ -170,7 +170,7 @@ steps: - run: go run hello.go ``` -**Caching wwith multiple concurrent jobs** +**Caching with multiple concurrent jobs** ```yaml jobs: From 847b9da5d213f56681dd4ca3cfcf7b69980df2a7 Mon Sep 17 00:00:00 2001 From: Erez Rokah Date: Fri, 2 Jun 2023 15:46:36 +0300 Subject: [PATCH 3/6] Update src/cache-restore.ts Co-authored-by: Alex Shcherbakov --- src/cache-restore.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cache-restore.ts b/src/cache-restore.ts index adfc9581e..0a8ee9c4d 100644 --- a/src/cache-restore.ts +++ b/src/cache-restore.ts @@ -29,8 +29,8 @@ export const restoreCache = async ( ); } - const cacheKeyPrefix = core.getInput('cache-key-prefix') || ''; - const primaryKey = `${cacheKeyPrefix}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); From cb7e1d2c4a50e8da6cb3dc1c3b46f5f2d31ba474 Mon Sep 17 00:00:00 2001 From: erezrokah Date: Fri, 2 Jun 2023 14:50:24 +0200 Subject: [PATCH 4/6] fix: Update dist --- dist/setup/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dist/setup/index.js b/dist/setup/index.js index c575a6a42..4804d2e53 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -63043,8 +63043,8 @@ const restoreCache = (versionSpec, packageManager, cacheDependencyPath) => __awa if (!fileHash) { throw new Error('Some specified paths were not resolved, unable to cache dependencies.'); } - const cacheKeyPrefix = core.getInput('cache-key-prefix') || ''; - const primaryKey = `${cacheKeyPrefix}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); From 3c24beb7c813f7ab9207e10d94b6e2b365dd5fd7 Mon Sep 17 00:00:00 2001 From: erezrokah Date: Fri, 2 Jun 2023 14:56:37 +0200 Subject: [PATCH 5/6] docs: Update docs and action.yml --- README.md | 4 ++-- action.yml | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b0e14e08a..44a3c8fda 100644 --- a/README.md +++ b/README.md @@ -181,7 +181,7 @@ jobs: - uses: actions/setup-go@v4 with: go-version: "1.20" - cache-key-prefix: build-cache- + cache-key-prefix: build-cache - run: go build test: runs-on: ubuntu-latest @@ -190,7 +190,7 @@ jobs: - uses: actions/setup-go@v4 with: go-version: "1.20" - cache-key-prefix: test-cache- + cache-key-prefix: test-cache - run: go test ./... ``` diff --git a/action.yml b/action.yml index 3633fc2e0..5744d8b69 100644 --- a/action.yml +++ b/action.yml @@ -19,6 +19,7 @@ inputs: 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: From ab4b1f7578bbd50ef75f9ad9a0a30a4bf707ad11 Mon Sep 17 00:00:00 2001 From: erezrokah Date: Fri, 2 Jun 2023 16:49:46 +0200 Subject: [PATCH 6/6] style: Format --- src/cache-restore.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cache-restore.ts b/src/cache-restore.ts index 0a8ee9c4d..52bc52d06 100644 --- a/src/cache-restore.ts +++ b/src/cache-restore.ts @@ -29,7 +29,7 @@ export const restoreCache = async ( ); } - const cacheKeyPrefix = core.getInput('cache-key-prefix') || 'setup-go' + const cacheKeyPrefix = core.getInput('cache-key-prefix') || 'setup-go'; const primaryKey = `${cacheKeyPrefix}-${platform}-go-${versionSpec}-${fileHash}`; core.debug(`primary key is ${primaryKey}`);