integrate the cache logic to entry points

This commit is contained in:
Kengo TODA 2021-07-13 09:04:54 +08:00
parent fef7d5836a
commit 4a7bf9903c
8 changed files with 116152 additions and 578 deletions

View file

@ -1,8 +1,9 @@
import * as core from '@actions/core';
import * as gpg from './gpg';
import * as constants from './constants';
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 +15,39 @@ 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 cache = core.getInput(constants.INPUT_CACHE);
return cache ? save(cache) : Promise.resolve();
}
/**
* The save process is best-effort, and it should not make the workflow failed
* 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 Promise.all([removePrivateKeyFromKeychain(), 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,6 @@ 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 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 = {
@ -27,6 +29,7 @@ async function run() {
throw new Error(`No supported distribution was found for input ${distributionName}`);
}
const restoreResult = cache ? restore(cache) : Promise.resolve();
const result = await distribution.setupJava();
core.info('');
@ -39,7 +42,7 @@ async function run() {
const matchersPath = path.join(__dirname, '..', '..', '.github');
core.info(`##[add-matcher]${path.join(matchersPath, 'java.json')}`);
await auth.configureAuthentication();
await Promise.all([restoreResult, auth.configureAuthentication()]);
} catch (error) {
core.setFailed(error.message);
}