Introduce the dependency caching for Maven and Gradle (#193)

* implement a core logic to cache dependnecies

* integrate the cache logic to entry points

* add a user doc about the dependency cache feature

* reflect changes to the dist dir

* add a prefix to the cache key

https://github.com/actions/setup-java/pull/193/files#r669521434

* test: extract build.gradle to a file in __tests__ dir

* run the restore e2e test on the specified OS

* add an e2e test for maven

* fix the dependency among workflows

* stabilize the cache on the Windows in e2e test

* add .gitignore files to __tests__/cache directories

* try to run restore after the authentication

* use the key in state to save caches in the post process

* suggest users to run without daemon if fail to save Gradle cache on Windows

* add missing description in the README.md

* run clean-up tasks in serial

* Add validation for post step (#3)

* work on fixing cache post step

* fix tests

* Update src/cleanup-java.ts

Co-authored-by: Konrad Pabjan <konradpabjan@github.com>

* Update src/cache.ts

Co-authored-by: Konrad Pabjan <konradpabjan@github.com>

* style: put the name of input to the constants.ts

* format: run `npm run build` to reflect changes to the dist dir

* chore: update licensed files by `licensed cache`

it still has three errors as follows:
>* setup-java.npm.sax
>  filename: /Users/kengo/GitHub/setup-java/.licenses/npm/sax.dep.yml
>    - license needs review: other
>
>* setup-java.npm.tslib-1.14.1
>  filename: /Users/kengo/GitHub/setup-java/.licenses/npm/tslib-1.14.1.dep.yml
>    - license needs review: 0bsd
>
>* setup-java.npm.tslib-2.3.0
>  filename: /Users/kengo/GitHub/setup-java/.licenses/npm/tslib-2.3.0.dep.yml
>    - license needs review: 0bsd

* fix: rerun ncc on macOS with node v12

* build: follow the suggestion at PR page

https://github.com/actions/setup-java/pull/193#issuecomment-901839546

* fix: throw error in case of no package manager file found

Co-authored-by: Dmitry Shibanov <dmitry-shibanov@github.com>
Co-authored-by: Konrad Pabjan <konradpabjan@github.com>
This commit is contained in:
Kengo TODA
2021-08-20 01:19:35 +08:00
committed by GitHub
parent 4b1b3d4a82
commit 08e4e813b8
67 changed files with 119120 additions and 733 deletions

134
src/cache.ts Normal file
View File

@ -0,0 +1,134 @@
/**
* @fileoverview this file provides methods handling dependency cache
*/
import { join } from 'path';
import os from 'os';
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import * as glob from '@actions/glob';
const STATE_CACHE_PRIMARY_KEY = 'cache-primary-key';
const CACHE_MATCHED_KEY = 'cache-matched-key';
const CACHE_KEY_PREFIX = 'setup-java';
interface PackageManager {
id: 'maven' | 'gradle';
/**
* Paths of the file that specify the files to cache.
*/
path: string[];
pattern: string[];
}
const supportedPackageManager: PackageManager[] = [
{
id: 'maven',
path: [join(os.homedir(), '.m2', 'repository')],
// https://github.com/actions/cache/blob/0638051e9af2c23d10bb70fa9beffcad6cff9ce3/examples.md#java---maven
pattern: ['**/pom.xml']
},
{
id: 'gradle',
path: [join(os.homedir(), '.gradle', 'caches'), join(os.homedir(), '.gradle', 'wrapper')],
// https://github.com/actions/cache/blob/0638051e9af2c23d10bb70fa9beffcad6cff9ce3/examples.md#java---gradle
pattern: ['**/*.gradle*', '**/gradle-wrapper.properties']
}
];
function findPackageManager(id: string): PackageManager {
const packageManager = supportedPackageManager.find(packageManager => packageManager.id === id);
if (packageManager === undefined) {
throw new Error(`unknown package manager specified: ${id}`);
}
return 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}`;
}
/**
* Restore the dependency cache
* @param id ID of the package manager, should be "maven" or "gradle"
*/
export async function restore(id: string) {
const packageManager = findPackageManager(id);
const primaryKey = await computeCacheKey(packageManager);
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`
);
}
const matchedKey = await cache.restoreCache(packageManager.path, primaryKey, [
`${CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${id}`
]);
if (matchedKey) {
core.saveState(CACHE_MATCHED_KEY, matchedKey);
core.info(`Cache restored from key: ${matchedKey}`);
} else {
core.info(`${packageManager.id} cache is not found`);
}
}
/**
* Save the dependency cache
* @param id ID of the package manager, should be "maven" or "gradle"
*/
export async function save(id: string) {
const packageManager = findPackageManager(id);
const matchedKey = core.getState(CACHE_MATCHED_KEY);
// Inputs are re-evaluted before the post action, so we want the original key used for restore
const primaryKey = core.getState(STATE_CACHE_PRIMARY_KEY);
if (!primaryKey) {
core.warning('Error retrieving key from state.');
return;
} else if (matchedKey === primaryKey) {
// no change in target directories
core.info(`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`);
return;
}
try {
await cache.saveCache(packageManager.path, primaryKey);
core.info(`Cache saved with the key: ${primaryKey}`);
} catch (error) {
if (error.name === cache.ReserveCacheError.name) {
core.info(error.message);
} else {
if (isProbablyGradleDaemonProblem(packageManager, error)) {
core.warning(
'Failed to save Gradle cache on Windows. If tar.exe reported "Permission denied", try to run Gradle with `--no-daemon` option. Refer to https://github.com/actions/cache/issues/454 for details.'
);
}
throw error;
}
}
}
/**
* @param packageManager the specified package manager by user
* @param error the error thrown by the saveCache
* @returns true if the given error seems related to the {@link https://github.com/actions/cache/issues/454|running Gradle Daemon issue}.
* @see {@link https://github.com/actions/cache/issues/454#issuecomment-840493935|why --no-daemon is necessary}
*/
function isProbablyGradleDaemonProblem(packageManager: PackageManager, error: Error) {
if (packageManager.id !== 'gradle' || process.env['RUNNER_OS'] !== 'Windows') {
return false;
}
const message = error.message || '';
return message.startsWith('Tar failed with error: ');
}

View File

@ -1,8 +1,10 @@
import * as core from '@actions/core';
import * as gpg from './gpg';
import * as constants from './constants';
import { isJobStatusSuccess } from './util';
import { save } from './cache';
async function run() {
async function removePrivateKeyFromKeychain() {
if (core.getInput(constants.INPUT_GPG_PRIVATE_KEY, { required: false })) {
core.info('Removing private key from keychain');
try {
@ -14,4 +16,41 @@ async function run() {
}
}
run();
/**
* Check given input and run a save process for the specified package manager
* @returns Promise that will be resolved when the save process finishes
*/
async function saveCache() {
const jobStatus = isJobStatusSuccess();
const cache = core.getInput(constants.INPUT_CACHE);
return jobStatus && cache ? save(cache) : Promise.resolve();
}
/**
* The save process is best-effort, and it should not make the workflow fail
* even though this process throws an error.
* @param promise the promise to ignore error from
* @returns Promise that will ignore error reported by the given promise
*/
async function ignoreError(promise: Promise<void>) {
return new Promise(resolve => {
promise
.catch(error => {
core.warning(error);
resolve(void 0);
})
.then(resolve);
});
}
export async function run() {
await removePrivateKeyFromKeychain();
await ignoreError(saveCache());
}
if (require.main === module) {
run();
} else {
// https://nodejs.org/api/modules.html#modules_accessing_the_main_module
core.info('the script is loaded as a module, so skipping the execution');
}

View File

@ -16,4 +16,7 @@ export const INPUT_GPG_PASSPHRASE = 'gpg-passphrase';
export const INPUT_DEFAULT_GPG_PRIVATE_KEY = undefined;
export const INPUT_DEFAULT_GPG_PASSPHRASE = 'GPG_PASSPHRASE';
export const INPUT_CACHE = 'cache';
export const INPUT_JOB_STATUS = 'job-status';
export const STATE_GPG_PRIVATE_KEY_FINGERPRINT = 'gpg-private-key-fingerprint';

View File

@ -2,6 +2,7 @@ import * as core from '@actions/core';
import * as auth from './auth';
import { getBooleanInput } from './util';
import * as constants from './constants';
import { restore } from './cache';
import * as path from 'path';
import { getJavaDistribution } from './distributions/distribution-factory';
import { JavaInstallerOptions } from './distributions/base-models';
@ -13,6 +14,7 @@ async function run() {
const architecture = core.getInput(constants.INPUT_ARCHITECTURE);
const packageType = core.getInput(constants.INPUT_JAVA_PACKAGE);
const jdkFile = core.getInput(constants.INPUT_JDK_FILE);
const cache = core.getInput(constants.INPUT_CACHE);
const checkLatest = getBooleanInput(constants.INPUT_CHECK_LATEST, false);
const installerOptions: JavaInstallerOptions = {
@ -40,6 +42,9 @@ async function run() {
core.info(`##[add-matcher]${path.join(matchersPath, 'java.json')}`);
await auth.configureAuthentication();
if (cache) {
await restore(cache);
}
} catch (error) {
core.setFailed(error.message);
}

View File

@ -5,6 +5,8 @@ import * as semver from 'semver';
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';
import { INPUT_JOB_STATUS } from './constants';
export function getTempDir() {
let tempDirectory = process.env['RUNNER_TEMP'] || os.tmpdir();
@ -69,3 +71,9 @@ export function getToolcachePath(toolName: string, version: string, architecture
return null;
}
export function isJobStatusSuccess() {
const jobStatus = core.getInput(INPUT_JOB_STATUS);
return jobStatus === 'success';
}