From a8e0c559af113b2d7f3b5adb340c09cdc29d4dfa Mon Sep 17 00:00:00 2001 From: itchyny Date: Tue, 25 Jul 2023 08:17:29 +0900 Subject: [PATCH] feat: implement cache-dependency-path option to control caching dependency --- README.md | 12 ++++++ __tests__/cache.test.ts | 82 +++++++++++++++++++++++++++++++++++++---- dist/cleanup/index.js | 24 +++++++----- dist/setup/index.js | 27 ++++++++------ src/cache.ts | 31 +++++++++------- src/constants.ts | 1 + src/setup-java.ts | 5 ++- 7 files changed, 138 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 2f5b022..f6ad0c4 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,8 @@ This action allows you to work with Java and Scala projects. - `cache`: Quick [setup caching](#caching-packages-dependencies) for the dependencies managed through one of the predifined package managers. It can be one of "maven", "gradle" or "sbt". + - `cache-dependency-path`: The path to a dependency file: pom.xml, build.gradle, build.sbt, etc. This option can be used with the `cache` option. If this option is omitted, the action searches for the dependency file in the entire repository. This option supports wildcards and a list of file names for caching multiple dependencies. + #### Maven options The action has a bunch of inputs to generate maven's [settings.xml](https://maven.apache.org/settings.html) on the fly and pass the values to Apache Maven GPG Plugin as well as Apache Maven Toolchains. See [advanced usage](docs/advanced-usage.md) for more. @@ -114,10 +116,13 @@ Currently, the following distributions are supported: ### Caching packages dependencies The action has a built-in functionality for caching and restoring dependencies. It uses [toolkit/cache](https://github.com/actions/toolkit/tree/main/packages/cache) under hood for caching dependencies but requires less configuration settings. Supported package managers are gradle, maven and sbt. The format of the used cache key is `setup-java-${{ platform }}-${{ packageManager }}-${{ fileHash }}`, where the hash is based on the following files: + - gradle: `**/*.gradle*`, `**/gradle-wrapper.properties`, `buildSrc/**/Versions.kt`, `buildSrc/**/Dependencies.kt`, `gradle/*.versions.toml`, and `**/versions.properties` - maven: `**/pom.xml` - sbt: all sbt build definition files `**/*.sbt`, `**/project/build.properties`, `**/project/**.scala`, `**/project/**.sbt` +When the option `cache-dependency-path` is specified, the hash is based on the matching file. This option supports wildcards and a list of file names, and is especially useful for monorepos. + The workflow output `cache-hit` is set to indicate if an exact match was found for the key [as actions/cache does](https://github.com/actions/cache/tree/main#outputs). The cache input is optional, and caching is turned off by default. @@ -131,6 +136,9 @@ steps: distribution: 'temurin' java-version: '17' cache: 'gradle' + cache-dependency-path: | # optional + sub-project/*.gradle* + sub-project/**/gradle-wrapper.properties - run: ./gradlew build --no-daemon ``` @@ -143,6 +151,7 @@ steps: distribution: 'temurin' java-version: '17' cache: 'maven' + cache-dependency-path: 'sub-project/pom.xml' # optional - name: Build with Maven run: mvn -B package --file pom.xml ``` @@ -156,6 +165,9 @@ steps: distribution: 'temurin' java-version: '17' cache: 'sbt' + cache-dependency-path: | # optional + sub-project/build.sbt + sub-project/project/build.properties - name: Build with SBT run: sbt package ``` diff --git a/__tests__/cache.test.ts b/__tests__/cache.test.ts index f6932a7..a259917 100644 --- a/__tests__/cache.test.ts +++ b/__tests__/cache.test.ts @@ -6,6 +6,7 @@ import * as fs from 'fs'; import * as os from 'os'; import * as core from '@actions/core'; import * as cache from '@actions/cache'; +import * as glob from '@actions/glob'; describe('dependency cache', () => { const ORIGINAL_RUNNER_OS = process.env['RUNNER_OS']; @@ -64,6 +65,10 @@ describe('dependency cache', () => { ReturnType, Parameters >; + let spyGlobHashFiles: jest.SpyInstance< + ReturnType, + Parameters + >; beforeEach(() => { spyCacheRestore = jest @@ -71,6 +76,7 @@ describe('dependency cache', () => { .mockImplementation((paths: string[], primaryKey: string) => Promise.resolve(undefined) ); + spyGlobHashFiles = jest.spyOn(glob, 'hashFiles'); spyWarning.mockImplementation(() => null); }); @@ -93,6 +99,7 @@ describe('dependency cache', () => { await restore('maven'); expect(spyCacheRestore).toHaveBeenCalled(); + expect(spyGlobHashFiles).toHaveBeenCalledWith('**/pom.xml'); expect(spyWarning).not.toHaveBeenCalled(); expect(spyInfo).toHaveBeenCalledWith('maven cache is not found'); }); @@ -110,6 +117,9 @@ describe('dependency cache', () => { await restore('gradle'); expect(spyCacheRestore).toHaveBeenCalled(); + expect(spyGlobHashFiles).toHaveBeenCalledWith( + '**/*.gradle*\n**/gradle-wrapper.properties\nbuildSrc/**/Versions.kt\nbuildSrc/**/Dependencies.kt\ngradle/*.versions.toml\n**/versions.properties' + ); expect(spyWarning).not.toHaveBeenCalled(); expect(spyInfo).toHaveBeenCalledWith('gradle cache is not found'); }); @@ -118,6 +128,9 @@ describe('dependency cache', () => { await restore('gradle'); expect(spyCacheRestore).toHaveBeenCalled(); + expect(spyGlobHashFiles).toHaveBeenCalledWith( + '**/*.gradle*\n**/gradle-wrapper.properties\nbuildSrc/**/Versions.kt\nbuildSrc/**/Dependencies.kt\ngradle/*.versions.toml\n**/versions.properties' + ); expect(spyWarning).not.toHaveBeenCalled(); expect(spyInfo).toHaveBeenCalledWith('gradle cache is not found'); }); @@ -127,18 +140,24 @@ describe('dependency cache', () => { await restore('gradle'); expect(spyCacheRestore).toHaveBeenCalled(); + expect(spyGlobHashFiles).toHaveBeenCalledWith( + '**/*.gradle*\n**/gradle-wrapper.properties\nbuildSrc/**/Versions.kt\nbuildSrc/**/Dependencies.kt\ngradle/*.versions.toml\n**/versions.properties' + ); expect(spyWarning).not.toHaveBeenCalled(); expect(spyInfo).toHaveBeenCalledWith('gradle cache is not found'); }); - }); - it('downloads cache based on buildSrc/Versions.kt', async () => { - createDirectory(join(workspace, 'buildSrc')); - createFile(join(workspace, 'buildSrc', 'Versions.kt')); + it('downloads cache based on buildSrc/Versions.kt', async () => { + createDirectory(join(workspace, 'buildSrc')); + createFile(join(workspace, 'buildSrc', 'Versions.kt')); - await restore('gradle'); - expect(spyCacheRestore).toHaveBeenCalled(); - expect(spyWarning).not.toHaveBeenCalled(); - expect(spyInfo).toHaveBeenCalledWith('gradle cache is not found'); + await restore('gradle'); + expect(spyCacheRestore).toHaveBeenCalled(); + expect(spyGlobHashFiles).toHaveBeenCalledWith( + '**/*.gradle*\n**/gradle-wrapper.properties\nbuildSrc/**/Versions.kt\nbuildSrc/**/Dependencies.kt\ngradle/*.versions.toml\n**/versions.properties' + ); + expect(spyWarning).not.toHaveBeenCalled(); + expect(spyInfo).toHaveBeenCalledWith('gradle cache is not found'); + }); }); describe('for sbt', () => { it('throws error if no build.sbt found', async () => { @@ -153,6 +172,9 @@ describe('dependency cache', () => { await restore('sbt'); expect(spyCacheRestore).toHaveBeenCalled(); + expect(spyGlobHashFiles).toHaveBeenCalledWith( + '**/*.sbt\n**/project/build.properties\n**/project/**.scala\n**/project/**.sbt' + ); expect(spyWarning).not.toHaveBeenCalled(); expect(spyInfo).toHaveBeenCalledWith('sbt cache is not found'); }); @@ -184,9 +206,53 @@ describe('dependency cache', () => { await restore('gradle'); expect(spyCacheRestore).toHaveBeenCalled(); + expect(spyGlobHashFiles).toHaveBeenCalledWith( + '**/*.gradle*\n**/gradle-wrapper.properties\nbuildSrc/**/Versions.kt\nbuildSrc/**/Dependencies.kt\ngradle/*.versions.toml\n**/versions.properties' + ); expect(spyWarning).not.toHaveBeenCalled(); expect(spyInfo).toHaveBeenCalledWith('gradle cache is not found'); }); + describe('cache-dependency-path', () => { + it('throws error if no matching dependency file found', async () => { + createFile(join(workspace, 'build.gradle.kts')); + await expect( + restore('gradle', 'sub-project/**/build.gradle.kts') + ).rejects.toThrow( + `No file in ${projectRoot( + workspace + )} matched to [sub-project/**/build.gradle.kts], make sure you have checked out the target repository` + ); + }); + it('downloads cache based on the specified pattern', async () => { + createFile(join(workspace, 'build.gradle.kts')); + createDirectory(join(workspace, 'sub-project1')); + createFile(join(workspace, 'sub-project1', 'build.gradle.kts')); + createDirectory(join(workspace, 'sub-project2')); + createFile(join(workspace, 'sub-project2', 'build.gradle.kts')); + + await restore('gradle', 'build.gradle.kts'); + expect(spyCacheRestore).toHaveBeenCalled(); + expect(spyGlobHashFiles).toHaveBeenCalledWith('build.gradle.kts'); + expect(spyWarning).not.toHaveBeenCalled(); + expect(spyInfo).toHaveBeenCalledWith('gradle cache is not found'); + + await restore('gradle', 'sub-project1/**/*.gradle*\n'); + expect(spyCacheRestore).toHaveBeenCalled(); + expect(spyGlobHashFiles).toHaveBeenCalledWith( + 'sub-project1/**/*.gradle*' + ); + expect(spyWarning).not.toHaveBeenCalled(); + expect(spyInfo).toHaveBeenCalledWith('gradle cache is not found'); + + await restore('gradle', '*.gradle*\nsub-project2/**/*.gradle*\n'); + expect(spyCacheRestore).toHaveBeenCalled(); + expect(spyGlobHashFiles).toHaveBeenCalledWith( + '*.gradle*\nsub-project2/**/*.gradle*' + ); + expect(spyWarning).not.toHaveBeenCalled(); + expect(spyInfo).toHaveBeenCalledWith('gradle cache is not found'); + }); + }); }); describe('save', () => { let spyCacheSave: jest.SpyInstance< diff --git a/dist/cleanup/index.js b/dist/cleanup/index.js index 56df7de..dd1af01 100644 --- a/dist/cleanup/index.js +++ b/dist/cleanup/index.js @@ -66672,28 +66672,31 @@ function findPackageManager(id) { /** * A function that generates a cache key to use. * Format of the generated key will be "${{ platform }}-${{ id }}-${{ fileHash }}"". - * If there is no file matched to {@link PackageManager.path}, the generated key ends with a dash (-). * @see {@link https://docs.github.com/en/actions/guides/caching-dependencies-to-speed-up-workflows#matching-a-cache-key|spec of cache key} */ -function computeCacheKey(packageManager) { +function computeCacheKey(packageManager, cacheDependencyPath) { return __awaiter(this, void 0, void 0, function* () { - const hash = yield glob.hashFiles(packageManager.pattern.join('\n')); - return `${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${packageManager.id}-${hash}`; + const pattern = cacheDependencyPath + ? cacheDependencyPath.trim().split('\n') + : packageManager.pattern; + const fileHash = yield glob.hashFiles(pattern.join('\n')); + if (!fileHash) { + throw new Error(`No file in ${process.cwd()} matched to [${pattern}], make sure you have checked out the target repository`); + } + return `${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${packageManager.id}-${fileHash}`; }); } /** * Restore the dependency cache * @param id ID of the package manager, should be "maven" or "gradle" + * @param cacheDependencyPath The path to a dependency file */ -function restore(id) { +function restore(id, cacheDependencyPath) { return __awaiter(this, void 0, void 0, function* () { const packageManager = findPackageManager(id); - const primaryKey = yield computeCacheKey(packageManager); + const primaryKey = yield computeCacheKey(packageManager, cacheDependencyPath); core.debug(`primary key is ${primaryKey}`); core.saveState(STATE_CACHE_PRIMARY_KEY, primaryKey); - if (primaryKey.endsWith('-')) { - throw new Error(`No file in ${process.cwd()} matched to [${packageManager.pattern}], make sure you have checked out the target repository`); - } // No "restoreKeys" is set, to start with a clear cache after dependency update (see https://github.com/actions/setup-java/issues/269) const matchedKey = yield cache.restoreCache(packageManager.path, primaryKey); if (matchedKey) { @@ -66870,7 +66873,7 @@ else { "use strict"; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.DISTRIBUTIONS_ONLY_MAJOR_VERSION = exports.INPUT_MVN_TOOLCHAIN_VENDOR = exports.INPUT_MVN_TOOLCHAIN_ID = exports.MVN_TOOLCHAINS_FILE = exports.MVN_SETTINGS_FILE = exports.M2_DIR = exports.STATE_GPG_PRIVATE_KEY_FINGERPRINT = exports.INPUT_JOB_STATUS = exports.INPUT_CACHE = exports.INPUT_DEFAULT_GPG_PASSPHRASE = exports.INPUT_DEFAULT_GPG_PRIVATE_KEY = exports.INPUT_GPG_PASSPHRASE = exports.INPUT_GPG_PRIVATE_KEY = exports.INPUT_OVERWRITE_SETTINGS = exports.INPUT_SETTINGS_PATH = exports.INPUT_SERVER_PASSWORD = exports.INPUT_SERVER_USERNAME = exports.INPUT_SERVER_ID = exports.INPUT_CHECK_LATEST = exports.INPUT_JDK_FILE = exports.INPUT_DISTRIBUTION = exports.INPUT_JAVA_PACKAGE = exports.INPUT_ARCHITECTURE = exports.INPUT_JAVA_VERSION_FILE = exports.INPUT_JAVA_VERSION = exports.MACOS_JAVA_CONTENT_POSTFIX = void 0; +exports.DISTRIBUTIONS_ONLY_MAJOR_VERSION = exports.INPUT_MVN_TOOLCHAIN_VENDOR = exports.INPUT_MVN_TOOLCHAIN_ID = exports.MVN_TOOLCHAINS_FILE = exports.MVN_SETTINGS_FILE = exports.M2_DIR = exports.STATE_GPG_PRIVATE_KEY_FINGERPRINT = exports.INPUT_JOB_STATUS = exports.INPUT_CACHE_DEPENDENCY_PATH = exports.INPUT_CACHE = exports.INPUT_DEFAULT_GPG_PASSPHRASE = exports.INPUT_DEFAULT_GPG_PRIVATE_KEY = exports.INPUT_GPG_PASSPHRASE = exports.INPUT_GPG_PRIVATE_KEY = exports.INPUT_OVERWRITE_SETTINGS = exports.INPUT_SETTINGS_PATH = exports.INPUT_SERVER_PASSWORD = exports.INPUT_SERVER_USERNAME = exports.INPUT_SERVER_ID = exports.INPUT_CHECK_LATEST = exports.INPUT_JDK_FILE = exports.INPUT_DISTRIBUTION = exports.INPUT_JAVA_PACKAGE = exports.INPUT_ARCHITECTURE = exports.INPUT_JAVA_VERSION_FILE = exports.INPUT_JAVA_VERSION = exports.MACOS_JAVA_CONTENT_POSTFIX = void 0; exports.MACOS_JAVA_CONTENT_POSTFIX = 'Contents/Home'; exports.INPUT_JAVA_VERSION = 'java-version'; exports.INPUT_JAVA_VERSION_FILE = 'java-version-file'; @@ -66889,6 +66892,7 @@ exports.INPUT_GPG_PASSPHRASE = 'gpg-passphrase'; exports.INPUT_DEFAULT_GPG_PRIVATE_KEY = undefined; exports.INPUT_DEFAULT_GPG_PASSPHRASE = 'GPG_PASSPHRASE'; exports.INPUT_CACHE = 'cache'; +exports.INPUT_CACHE_DEPENDENCY_PATH = 'cache-dependency-path'; exports.INPUT_JOB_STATUS = 'job-status'; exports.STATE_GPG_PRIVATE_KEY_FINGERPRINT = 'gpg-private-key-fingerprint'; exports.M2_DIR = '.m2'; diff --git a/dist/setup/index.js b/dist/setup/index.js index cf4bf07..e77f5a1 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -101877,28 +101877,31 @@ function findPackageManager(id) { /** * A function that generates a cache key to use. * Format of the generated key will be "${{ platform }}-${{ id }}-${{ fileHash }}"". - * If there is no file matched to {@link PackageManager.path}, the generated key ends with a dash (-). * @see {@link https://docs.github.com/en/actions/guides/caching-dependencies-to-speed-up-workflows#matching-a-cache-key|spec of cache key} */ -function computeCacheKey(packageManager) { +function computeCacheKey(packageManager, cacheDependencyPath) { return __awaiter(this, void 0, void 0, function* () { - const hash = yield glob.hashFiles(packageManager.pattern.join('\n')); - return `${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${packageManager.id}-${hash}`; + const pattern = cacheDependencyPath + ? cacheDependencyPath.trim().split('\n') + : packageManager.pattern; + const fileHash = yield glob.hashFiles(pattern.join('\n')); + if (!fileHash) { + throw new Error(`No file in ${process.cwd()} matched to [${pattern}], make sure you have checked out the target repository`); + } + return `${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${packageManager.id}-${fileHash}`; }); } /** * Restore the dependency cache * @param id ID of the package manager, should be "maven" or "gradle" + * @param cacheDependencyPath The path to a dependency file */ -function restore(id) { +function restore(id, cacheDependencyPath) { return __awaiter(this, void 0, void 0, function* () { const packageManager = findPackageManager(id); - const primaryKey = yield computeCacheKey(packageManager); + const primaryKey = yield computeCacheKey(packageManager, cacheDependencyPath); core.debug(`primary key is ${primaryKey}`); core.saveState(STATE_CACHE_PRIMARY_KEY, primaryKey); - if (primaryKey.endsWith('-')) { - throw new Error(`No file in ${process.cwd()} matched to [${packageManager.pattern}], make sure you have checked out the target repository`); - } // No "restoreKeys" is set, to start with a clear cache after dependency update (see https://github.com/actions/setup-java/issues/269) const matchedKey = yield cache.restoreCache(packageManager.path, primaryKey); if (matchedKey) { @@ -101974,7 +101977,7 @@ function isProbablyGradleDaemonProblem(packageManager, error) { "use strict"; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.DISTRIBUTIONS_ONLY_MAJOR_VERSION = exports.INPUT_MVN_TOOLCHAIN_VENDOR = exports.INPUT_MVN_TOOLCHAIN_ID = exports.MVN_TOOLCHAINS_FILE = exports.MVN_SETTINGS_FILE = exports.M2_DIR = exports.STATE_GPG_PRIVATE_KEY_FINGERPRINT = exports.INPUT_JOB_STATUS = exports.INPUT_CACHE = exports.INPUT_DEFAULT_GPG_PASSPHRASE = exports.INPUT_DEFAULT_GPG_PRIVATE_KEY = exports.INPUT_GPG_PASSPHRASE = exports.INPUT_GPG_PRIVATE_KEY = exports.INPUT_OVERWRITE_SETTINGS = exports.INPUT_SETTINGS_PATH = exports.INPUT_SERVER_PASSWORD = exports.INPUT_SERVER_USERNAME = exports.INPUT_SERVER_ID = exports.INPUT_CHECK_LATEST = exports.INPUT_JDK_FILE = exports.INPUT_DISTRIBUTION = exports.INPUT_JAVA_PACKAGE = exports.INPUT_ARCHITECTURE = exports.INPUT_JAVA_VERSION_FILE = exports.INPUT_JAVA_VERSION = exports.MACOS_JAVA_CONTENT_POSTFIX = void 0; +exports.DISTRIBUTIONS_ONLY_MAJOR_VERSION = exports.INPUT_MVN_TOOLCHAIN_VENDOR = exports.INPUT_MVN_TOOLCHAIN_ID = exports.MVN_TOOLCHAINS_FILE = exports.MVN_SETTINGS_FILE = exports.M2_DIR = exports.STATE_GPG_PRIVATE_KEY_FINGERPRINT = exports.INPUT_JOB_STATUS = exports.INPUT_CACHE_DEPENDENCY_PATH = exports.INPUT_CACHE = exports.INPUT_DEFAULT_GPG_PASSPHRASE = exports.INPUT_DEFAULT_GPG_PRIVATE_KEY = exports.INPUT_GPG_PASSPHRASE = exports.INPUT_GPG_PRIVATE_KEY = exports.INPUT_OVERWRITE_SETTINGS = exports.INPUT_SETTINGS_PATH = exports.INPUT_SERVER_PASSWORD = exports.INPUT_SERVER_USERNAME = exports.INPUT_SERVER_ID = exports.INPUT_CHECK_LATEST = exports.INPUT_JDK_FILE = exports.INPUT_DISTRIBUTION = exports.INPUT_JAVA_PACKAGE = exports.INPUT_ARCHITECTURE = exports.INPUT_JAVA_VERSION_FILE = exports.INPUT_JAVA_VERSION = exports.MACOS_JAVA_CONTENT_POSTFIX = void 0; exports.MACOS_JAVA_CONTENT_POSTFIX = 'Contents/Home'; exports.INPUT_JAVA_VERSION = 'java-version'; exports.INPUT_JAVA_VERSION_FILE = 'java-version-file'; @@ -101993,6 +101996,7 @@ exports.INPUT_GPG_PASSPHRASE = 'gpg-passphrase'; exports.INPUT_DEFAULT_GPG_PRIVATE_KEY = undefined; exports.INPUT_DEFAULT_GPG_PASSPHRASE = 'GPG_PASSPHRASE'; exports.INPUT_CACHE = 'cache'; +exports.INPUT_CACHE_DEPENDENCY_PATH = 'cache-dependency-path'; exports.INPUT_JOB_STATUS = 'job-status'; exports.STATE_GPG_PRIVATE_KEY_FINGERPRINT = 'gpg-private-key-fingerprint'; exports.M2_DIR = '.m2'; @@ -103805,6 +103809,7 @@ function run() { const packageType = core.getInput(constants.INPUT_JAVA_PACKAGE); const jdkFile = core.getInput(constants.INPUT_JDK_FILE); const cache = core.getInput(constants.INPUT_CACHE); + const cacheDependencyPath = core.getInput(constants.INPUT_CACHE_DEPENDENCY_PATH); const checkLatest = util_1.getBooleanInput(constants.INPUT_CHECK_LATEST, false); let toolchainIds = core.getMultilineInput(constants.INPUT_MVN_TOOLCHAIN_ID); core.startGroup('Installed distributions'); @@ -103840,7 +103845,7 @@ function run() { core.info(`##[add-matcher]${path.join(matchersPath, 'java.json')}`); yield auth.configureAuthentication(); if (cache && util_1.isCacheFeatureAvailable()) { - yield cache_1.restore(cache); + yield cache_1.restore(cache, cacheDependencyPath); } } catch (error) { diff --git a/src/cache.ts b/src/cache.ts index 7dd8da9..5da8955 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -83,31 +83,34 @@ function findPackageManager(id: string): PackageManager { /** * A function that generates a cache key to use. * Format of the generated key will be "${{ platform }}-${{ id }}-${{ fileHash }}"". - * If there is no file matched to {@link PackageManager.path}, the generated key ends with a dash (-). * @see {@link https://docs.github.com/en/actions/guides/caching-dependencies-to-speed-up-workflows#matching-a-cache-key|spec of cache key} */ -async function computeCacheKey(packageManager: PackageManager) { - const hash = await glob.hashFiles(packageManager.pattern.join('\n')); - return `${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${packageManager.id}-${hash}`; +async function computeCacheKey( + packageManager: PackageManager, + cacheDependencyPath?: string +) { + const pattern = cacheDependencyPath + ? cacheDependencyPath.trim().split('\n') + : packageManager.pattern; + const fileHash = await glob.hashFiles(pattern.join('\n')); + if (!fileHash) { + throw new Error( + `No file in ${process.cwd()} matched to [${pattern}], make sure you have checked out the target repository` + ); + } + return `${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${packageManager.id}-${fileHash}`; } /** * Restore the dependency cache * @param id ID of the package manager, should be "maven" or "gradle" + * @param cacheDependencyPath The path to a dependency file */ -export async function restore(id: string) { +export async function restore(id: string, cacheDependencyPath?: string) { const packageManager = findPackageManager(id); - const primaryKey = await computeCacheKey(packageManager); - + const primaryKey = await computeCacheKey(packageManager, cacheDependencyPath); core.debug(`primary key is ${primaryKey}`); core.saveState(STATE_CACHE_PRIMARY_KEY, primaryKey); - if (primaryKey.endsWith('-')) { - throw new Error( - `No file in ${process.cwd()} matched to [${ - packageManager.pattern - }], make sure you have checked out the target repository` - ); - } // No "restoreKeys" is set, to start with a clear cache after dependency update (see https://github.com/actions/setup-java/issues/269) const matchedKey = await cache.restoreCache(packageManager.path, primaryKey); diff --git a/src/constants.ts b/src/constants.ts index 9aa213f..93af286 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -18,6 +18,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_DEPENDENCY_PATH = 'cache-dependency-path'; 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 4168d53..68d234f 100644 --- a/src/setup-java.ts +++ b/src/setup-java.ts @@ -24,6 +24,9 @@ async function run() { const packageType = core.getInput(constants.INPUT_JAVA_PACKAGE); const jdkFile = core.getInput(constants.INPUT_JDK_FILE); const cache = core.getInput(constants.INPUT_CACHE); + const cacheDependencyPath = core.getInput( + constants.INPUT_CACHE_DEPENDENCY_PATH + ); const checkLatest = getBooleanInput(constants.INPUT_CHECK_LATEST, false); let toolchainIds = core.getMultilineInput(constants.INPUT_MVN_TOOLCHAIN_ID); @@ -73,7 +76,7 @@ async function run() { await auth.configureAuthentication(); if (cache && isCacheFeatureAvailable()) { - await restore(cache); + await restore(cache, cacheDependencyPath); } } catch (error) { core.setFailed(error.message);