Merge remote-tracking branch 'git@github.com-actions/setup-java.git/main' into include-buildSrc-in-cache-key

This commit is contained in:
Mario Schünadel
2022-04-25 12:43:44 +02:00
35 changed files with 7964 additions and 851 deletions

View File

@ -13,7 +13,7 @@ const CACHE_MATCHED_KEY = 'cache-matched-key';
const CACHE_KEY_PREFIX = 'setup-java';
interface PackageManager {
id: 'maven' | 'gradle';
id: 'maven' | 'gradle' | 'sbt';
/**
* Paths of the file that specify the files to cache.
*/
@ -32,9 +32,24 @@ const supportedPackageManager: PackageManager[] = [
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', 'buildSrc/**/*.kt']
},
{
id: 'sbt',
path: [
join(os.homedir(), '.ivy2', 'cache'),
join(os.homedir(), '.sbt'),
getCoursierCachePath()
],
pattern: ['**/*.sbt', '**/project/build.properties', '**/project/**.{scala,sbt}']
}
];
function getCoursierCachePath(): string {
if (os.type() === 'Linux') return join(os.homedir(), '.cache', 'coursier');
if (os.type() === 'Darwin') return join(os.homedir(), 'Library', 'Caches', 'Coursier');
return join(os.homedir(), 'AppData', 'Local', 'Coursier', 'Cache');
}
function findPackageManager(id: string): PackageManager {
const packageManager = supportedPackageManager.find(packageManager => packageManager.id === id);
if (packageManager === undefined) {
@ -77,8 +92,10 @@ export async function restore(id: string) {
]);
if (matchedKey) {
core.saveState(CACHE_MATCHED_KEY, matchedKey);
core.setOutput('cache-hit', matchedKey === primaryKey);
core.info(`Cache restored from key: ${matchedKey}`);
} else {
core.setOutput('cache-hit', false);
core.info(`${packageManager.id} cache is not found`);
}
}

View File

@ -1,6 +1,6 @@
import * as core from '@actions/core';
import * as auth from './auth';
import { getBooleanInput } from './util';
import { getBooleanInput, isCacheFeatureAvailable } from './util';
import * as constants from './constants';
import { restore } from './cache';
import * as path from 'path';
@ -42,7 +42,7 @@ async function run() {
core.info(`##[add-matcher]${path.join(matchersPath, 'java.json')}`);
await auth.configureAuthentication();
if (cache) {
if (cache && isCacheFeatureAvailable()) {
await restore(cache);
}
} catch (error) {

View File

@ -2,6 +2,7 @@ import os from 'os';
import path from 'path';
import * as fs from 'fs';
import * as semver from 'semver';
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';
@ -77,3 +78,24 @@ export function isJobStatusSuccess() {
return jobStatus === 'success';
}
export function isGhes(): boolean {
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
}
export function isCacheFeatureAvailable(): boolean {
if (!cache.isFeatureAvailable()) {
if (isGhes()) {
throw new Error(
'Caching is only supported on GHES version >= 3.5. If you are on a version >= 3.5, please check with your GHES admin if the Actions cache service is enabled or not.'
);
} else {
core.warning('The runner was not able to contact the cache service. Caching will be skipped');
}
return false;
}
return true;
}