diff --git a/README.md b/README.md index f3cea5d29..9b570e0b6 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,12 @@ 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 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 steps: diff --git a/action.yml b/action.yml index faad23c38..b6b9cf123 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-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 a890c4b63..f62a1b070 100644 --- a/src/cleanup-java.ts +++ b/src/cleanup-java.ts @@ -23,7 +23,9 @@ async function removePrivateKeyFromKeychain() { async function saveCache() { const jobStatus = isJobStatusSuccess(); const cache = core.getInput(constants.INPUT_CACHE); - return jobStatus && cache ? 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 94d7667f2..c39a043ea 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_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) {