From f58da941092534481b1972f45086faadd3335124 Mon Sep 17 00:00:00 2001 From: Al Sutton Date: Tue, 21 Dec 2021 13:04:44 +0000 Subject: [PATCH 1/5] Allow users to specify a read-only cache This can reduce workflow runtime for situations, such as PRs, where the build artifacts may be disposable and of little use to other workflow runs. --- src/cleanup-java.ts | 3 ++- src/constants.ts | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/cleanup-java.ts b/src/cleanup-java.ts index a890c4b63..e52de8ef1 100644 --- a/src/cleanup-java.ts +++ b/src/cleanup-java.ts @@ -23,7 +23,8 @@ async function removePrivateKeyFromKeychain() { async function saveCache() { const jobStatus = isJobStatusSuccess(); const cache = core.getInput(constants.INPUT_CACHE); - return jobStatus && cache ? save(cache) : Promise.resolve(); + const readOnlyCache = core.getInput(constants.INPUT_CACHE_READ_ONLY) + return jobStatus && cache && !readOnlyCache ? save(cache) : Promise.resolve(); } /** diff --git a/src/constants.ts b/src/constants.ts index 94d7667f2..a5fdfeea2 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -17,6 +17,7 @@ export const INPUT_DEFAULT_GPG_PRIVATE_KEY = undefined; export const INPUT_DEFAULT_GPG_PASSPHRASE = 'GPG_PASSPHRASE'; export const INPUT_CACHE = 'cache'; +export const INPUT_CACHE_READ_ONLY = "cache-is-read-only"; export const INPUT_JOB_STATUS = 'job-status'; export const STATE_GPG_PRIVATE_KEY_FINGERPRINT = 'gpg-private-key-fingerprint'; From 22f4b2eeaae6ab7f259df0b26cc200d1f292ad4f Mon Sep 17 00:00:00 2001 From: Al Sutton Date: Tue, 21 Dec 2021 13:11:33 +0000 Subject: [PATCH 2/5] Update docs --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index f3cea5d29..bb580bd92 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,10 @@ Currently, the following distributions are supported: ### Caching packages dependencies The action has a built-in functionality for caching and restoring dependencies. It uses [actions/cache](https://github.com/actions/cache) under hood for caching dependencies but requires less configuration settings. Supported package managers are gradle and maven. The cache input is optional, and caching is turned off by default. +The option to use the cache in a read-only mode is available via the parameter `cache-is-read-only`, which +defaults to `false`. When set to `true`, and caching is enabled, this allows PR workflows to benefit from +cached build artifacts, but not taint the cache with PR specific changes which may be in a state of flux. + #### Caching gradle dependencies ```yaml steps: From cec4e330307e21b2412affe99962d8d83cff40ec Mon Sep 17 00:00:00 2001 From: Al Sutton Date: Thu, 23 Dec 2021 11:20:50 +0000 Subject: [PATCH 3/5] Add cache-is-read-only to action definition --- action.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/action.yml b/action.yml index faad23c38..07967e17b 100644 --- a/action.yml +++ b/action.yml @@ -56,6 +56,9 @@ inputs: cache: description: 'Name of the build platform to cache dependencies. It can be "maven" or "gradle".' required: false + cache-is-read-only: + description: 'Whether or not the build output should be cached. Default is true.' + default: true job-status: description: 'Workaround to pass job status to post job step. This variable is not intended for manual setting' default: ${{ job.status }} From 62c27dc501707489e5184d1fcf0f73cac6feb40f Mon Sep 17 00:00:00 2001 From: Al Sutton Date: Thu, 23 Dec 2021 11:51:34 +0000 Subject: [PATCH 4/5] Switch to mode setting so we can write, read, or both --- action.yml | 6 +++--- src/cleanup-java.ts | 5 +++-- src/constants.ts | 2 +- src/setup-java.ts | 4 +++- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/action.yml b/action.yml index 07967e17b..b6b9cf123 100644 --- a/action.yml +++ b/action.yml @@ -56,9 +56,9 @@ inputs: cache: description: 'Name of the build platform to cache dependencies. It can be "maven" or "gradle".' required: false - cache-is-read-only: - description: 'Whether or not the build output should be cached. Default is true.' - default: true + cache-mode: + description: 'Whether the cache should be read from pre-build, written to afterwards, or both' + default: both job-status: description: 'Workaround to pass job status to post job step. This variable is not intended for manual setting' default: ${{ job.status }} diff --git a/src/cleanup-java.ts b/src/cleanup-java.ts index e52de8ef1..f62a1b070 100644 --- a/src/cleanup-java.ts +++ b/src/cleanup-java.ts @@ -23,8 +23,9 @@ async function removePrivateKeyFromKeychain() { async function saveCache() { const jobStatus = isJobStatusSuccess(); const cache = core.getInput(constants.INPUT_CACHE); - const readOnlyCache = core.getInput(constants.INPUT_CACHE_READ_ONLY) - return jobStatus && cache && !readOnlyCache ? save(cache) : Promise.resolve(); + const cacheMode = core.getInput(constants.INPUT_CACHE_MODE) + const updateCache = Boolean(cacheMode === "both" || cacheMode === "write") + return jobStatus && cache && updateCache ? save(cache) : Promise.resolve(); } /** diff --git a/src/constants.ts b/src/constants.ts index a5fdfeea2..c39a043ea 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -17,7 +17,7 @@ export const INPUT_DEFAULT_GPG_PRIVATE_KEY = undefined; export const INPUT_DEFAULT_GPG_PASSPHRASE = 'GPG_PASSPHRASE'; export const INPUT_CACHE = 'cache'; -export const INPUT_CACHE_READ_ONLY = "cache-is-read-only"; +export const INPUT_CACHE_MODE = "cache-mode"; export const INPUT_JOB_STATUS = 'job-status'; export const STATE_GPG_PRIVATE_KEY_FINGERPRINT = 'gpg-private-key-fingerprint'; diff --git a/src/setup-java.ts b/src/setup-java.ts index c6b12875f..7a1e8d58c 100644 --- a/src/setup-java.ts +++ b/src/setup-java.ts @@ -42,7 +42,9 @@ async function run() { core.info(`##[add-matcher]${path.join(matchersPath, 'java.json')}`); await auth.configureAuthentication(); - if (cache) { + const cacheMode = core.getInput(constants.INPUT_CACHE_MODE) + const restoreCache = Boolean(cacheMode === "both" || cacheMode === "read") + if (cache && restoreCache) { await restore(cache); } } catch (error) { From d63cd1f34dd64706a6485a9bde99d5c240010761 Mon Sep 17 00:00:00 2001 From: Al Sutton Date: Thu, 23 Dec 2021 11:53:36 +0000 Subject: [PATCH 5/5] Update docs --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bb580bd92..9b570e0b6 100644 --- a/README.md +++ b/README.md @@ -68,9 +68,11 @@ Currently, the following distributions are supported: ### Caching packages dependencies The action has a built-in functionality for caching and restoring dependencies. It uses [actions/cache](https://github.com/actions/cache) under hood for caching dependencies but requires less configuration settings. Supported package managers are gradle and maven. The cache input is optional, and caching is turned off by default. -The option to use the cache in a read-only mode is available via the parameter `cache-is-read-only`, which -defaults to `false`. When set to `true`, and caching is enabled, this allows PR workflows to benefit from -cached build artifacts, but not taint the cache with PR specific changes which may be in a state of flux. +The interaction with the cache can be controlled by the `cache-mode` parameter. This allows for builds to +be performed with no cache, and the output cached (`write` mode), builds to read the cache before running +but not update the cache afterwards (`read` mode), or both (`both` mode). This feature allows you to +populate the cache from your `main` branch, and then let PR builds benefit from that cache without polluting +it with PR-specific build artifacts. #### Caching gradle dependencies ```yaml