mirror of
https://gitea.com/actions/setup-java.git
synced 2026-08-05 02:31:18 +00:00
Add dependency cache path overrides (#1175)
* Add dependency cache path overrides Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: dd650d36-9c97-4ca4-9ec8-39b37f99a07c * Clarify supported dependency cache managers Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: dd650d36-9c97-4ca4-9ec8-39b37f99a07c * Fix custom cache path CI checks Align the custom cache save and restore key inputs and use the workflow hash to avoid a previously populated cache entry. Rebuild the distribution bundles. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: dd650d36-9c97-4ca4-9ec8-39b37f99a07c --------- Copilot-Session: dd650d36-9c97-4ca4-9ec8-39b37f99a07c
This commit is contained in:
+38
-5
@@ -9,6 +9,7 @@ import * as core from '@actions/core';
|
||||
import * as glob from '@actions/glob';
|
||||
|
||||
const STATE_CACHE_PRIMARY_KEY = 'cache-primary-key';
|
||||
const STATE_CACHE_PATHS = 'cache-paths';
|
||||
const CACHE_MATCHED_KEY = 'cache-matched-key';
|
||||
const CACHE_KEY_PREFIX = 'setup-java';
|
||||
|
||||
@@ -136,6 +137,29 @@ function findPackageManager(id: string): PackageManager {
|
||||
return packageManager;
|
||||
}
|
||||
|
||||
function resolveCachePaths(
|
||||
packageManager: PackageManager,
|
||||
cachePaths: string[]
|
||||
): string[] {
|
||||
return cachePaths.length > 0 ? cachePaths : packageManager.path;
|
||||
}
|
||||
|
||||
function getCachePathsFromState(packageManager: PackageManager): string[] {
|
||||
const cachePathsState = core.getState(STATE_CACHE_PATHS);
|
||||
if (!cachePathsState) {
|
||||
return packageManager.path;
|
||||
}
|
||||
|
||||
const cachePaths: unknown = JSON.parse(cachePathsState);
|
||||
if (
|
||||
!Array.isArray(cachePaths) ||
|
||||
!cachePaths.every(cachePath => typeof cachePath === 'string')
|
||||
) {
|
||||
throw new Error('Invalid cache paths retrieved from state.');
|
||||
}
|
||||
return cachePaths;
|
||||
}
|
||||
|
||||
/**
|
||||
* State keys used to carry an additional cache's restore-time information over
|
||||
* to the post (save) action, scoped by the additional cache name.
|
||||
@@ -189,11 +213,17 @@ async function computeAdditionalCacheKey(
|
||||
|
||||
/**
|
||||
* Restore the dependency cache
|
||||
* @param id ID of the package manager, should be "maven" or "gradle"
|
||||
* @param id ID of the package manager, should be "maven", "gradle", or "sbt"
|
||||
* @param cacheDependencyPath The path to a dependency file
|
||||
* @param cachePaths Paths to cache instead of the package manager defaults
|
||||
*/
|
||||
export async function restore(id: string, cacheDependencyPath: string) {
|
||||
export async function restore(
|
||||
id: string,
|
||||
cacheDependencyPath: string,
|
||||
cachePaths: string[] = []
|
||||
) {
|
||||
const packageManager = findPackageManager(id);
|
||||
const resolvedCachePaths = resolveCachePaths(packageManager, cachePaths);
|
||||
const [primaryKey, preparedAdditionalCaches] = await Promise.all([
|
||||
computeCacheKey(packageManager, cacheDependencyPath),
|
||||
prepareAdditionalCaches(packageManager.additionalCaches ?? [])
|
||||
@@ -201,6 +231,7 @@ export async function restore(id: string, cacheDependencyPath: string) {
|
||||
|
||||
core.debug(`primary key is ${primaryKey}`);
|
||||
core.saveState(STATE_CACHE_PRIMARY_KEY, primaryKey);
|
||||
core.saveState(STATE_CACHE_PATHS, JSON.stringify(resolvedCachePaths));
|
||||
core.setOutput(STATE_CACHE_PRIMARY_KEY, primaryKey);
|
||||
|
||||
for (const preparedCache of preparedAdditionalCaches) {
|
||||
@@ -214,7 +245,7 @@ export async function restore(id: string, cacheDependencyPath: string) {
|
||||
}
|
||||
|
||||
await Promise.all([
|
||||
restorePrimaryCache(packageManager, primaryKey),
|
||||
restorePrimaryCache(packageManager, resolvedCachePaths, primaryKey),
|
||||
...preparedAdditionalCaches.map(preparedCache =>
|
||||
restoreAdditionalCache(preparedCache)
|
||||
)
|
||||
@@ -223,10 +254,11 @@ export async function restore(id: string, cacheDependencyPath: string) {
|
||||
|
||||
async function restorePrimaryCache(
|
||||
packageManager: PackageManager,
|
||||
cachePaths: string[],
|
||||
primaryKey: string
|
||||
) {
|
||||
// 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);
|
||||
const matchedKey = await cache.restoreCache(cachePaths, primaryKey);
|
||||
if (matchedKey) {
|
||||
core.saveState(CACHE_MATCHED_KEY, matchedKey);
|
||||
core.setOutput('cache-hit', matchedKey === primaryKey);
|
||||
@@ -286,6 +318,7 @@ async function restoreAdditionalCache(preparedCache: PreparedAdditionalCache) {
|
||||
*/
|
||||
export async function save(id: string) {
|
||||
const packageManager = findPackageManager(id);
|
||||
const cachePaths = getCachePathsFromState(packageManager);
|
||||
const matchedKey = core.getState(CACHE_MATCHED_KEY);
|
||||
|
||||
// Inputs are re-evaluated before the post action, so we want the original key used for restore
|
||||
@@ -313,7 +346,7 @@ export async function save(id: string) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const cacheId = await cache.saveCache(packageManager.path, primaryKey);
|
||||
const cacheId = await cache.saveCache(cachePaths, primaryKey);
|
||||
if (cacheId === -1) {
|
||||
// saveCache returns -1 without throwing when the cache was not saved,
|
||||
// e.g. a reserve collision or a read-only token (fork PR). @actions/cache
|
||||
|
||||
@@ -38,6 +38,7 @@ export const GPG_PASSPHRASE_PROFILE_ID = 'setup-java-gpg';
|
||||
|
||||
export const INPUT_CACHE = 'cache';
|
||||
export const INPUT_CACHE_DEPENDENCY_PATH = 'cache-dependency-path';
|
||||
export const INPUT_CACHE_PATH = 'cache-path';
|
||||
export const INPUT_CACHE_READ_ONLY = 'cache-read-only';
|
||||
export const INPUT_JOB_STATUS = 'job-status';
|
||||
|
||||
|
||||
+2
-1
@@ -28,6 +28,7 @@ export async function run() {
|
||||
const cacheDependencyPath = core.getInput(
|
||||
constants.INPUT_CACHE_DEPENDENCY_PATH
|
||||
);
|
||||
const cachePath = core.getMultilineInput(constants.INPUT_CACHE_PATH);
|
||||
const checkLatest = getBooleanInput(constants.INPUT_CHECK_LATEST, false);
|
||||
const forceDownload = getBooleanInput(
|
||||
constants.INPUT_FORCE_DOWNLOAD,
|
||||
@@ -132,7 +133,7 @@ export async function run() {
|
||||
await auth.configureAuthentication();
|
||||
configureMavenArgs();
|
||||
if (cache && isCacheFeatureAvailable()) {
|
||||
await restore(cache, cacheDependencyPath);
|
||||
await restore(cache, cacheDependencyPath, cachePath);
|
||||
}
|
||||
} catch (error) {
|
||||
core.setFailed((error as Error).message);
|
||||
|
||||
Reference in New Issue
Block a user