1
0
mirror of https://github.com/pnpm/action-setup.git synced 2026-06-17 15:53:45 +08:00

add pnpm store caching

This commit is contained in:
Jeremiasz Major
2025-11-02 12:23:09 +01:00
parent 41ff726559
commit 0bc9da32cd
11 changed files with 709 additions and 16 deletions

View File

@@ -0,0 +1,19 @@
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import { runRestoreCache } from './run';
import { Inputs } from '../inputs';
export async function restoreCache(inputs: Inputs) {
if (!inputs.cache) return
if (!cache.isFeatureAvailable()) {
core.warning('Cache is not available, skipping cache restoration')
return
}
core.startGroup('Restoring cache...')
await runRestoreCache(inputs);
core.endGroup();
}
export default restoreCache

39
src/cache-restore/run.ts Normal file
View File

@@ -0,0 +1,39 @@
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as glob from '@actions/glob';
import os from 'os';
import { Inputs } from '../inputs';
export async function runRestoreCache(inputs: Inputs) {
const cachePath = await getCacheDirectory();
core.saveState('cache_path', cachePath);
const fileHash = await glob.hashFiles(inputs.cacheDependencyPath);
if (!fileHash) {
throw new Error('Some specified paths were not resolved, unable to cache dependencies.');
}
const primaryKey = `pnpm-cache-${process.env.RUNNER_OS}-${os.arch()}-${fileHash}`;
core.debug(`Primary key is ${primaryKey}`);
core.saveState('cache_primary_key', primaryKey);
let cacheKey = await cache.restoreCache([cachePath], primaryKey);
core.setOutput('cache-hit', Boolean(cacheKey));
if (!cacheKey) {
core.info(`Cache is not found`);
return;
}
core.saveState('cache_restored_key', cacheKey)
core.info(`Cache restored from key: ${cacheKey}`)
}
async function getCacheDirectory() {
const { stdout } = await exec.getExecOutput('pnpm store path --silent')
const cacheFolderPath = stdout.trim()
core.debug(`Cache folder is set to "${cacheFolderPath}"`)
return cacheFolderPath;
};

15
src/cache-save/index.ts Normal file
View File

@@ -0,0 +1,15 @@
import * as core from '@actions/core';
import { Inputs } from '../inputs';
import { runSaveCache } from './run';
export async function saveCache(inputs: Inputs) {
if (!inputs.cache) return
try {
await runSaveCache();
} catch (error) {
core.setFailed((error as Error).message);
}
}
export default saveCache

18
src/cache-save/run.ts Normal file
View File

@@ -0,0 +1,18 @@
import * as core from '@actions/core';
import * as cache from '@actions/cache';
export async function runSaveCache() {
const state = core.getState('cache_restored_key');
const primaryKey = core.getState('cache_primary_key');
const cachePath = core.getState('cache_path');
if (primaryKey === state) {
core.info(`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`);
return;
}
const cacheId = await cache.saveCache([cachePath], primaryKey);
if (cacheId == -1) return;
core.info(`Cache saved with the key: ${primaryKey}`);
}

View File

@@ -1,21 +1,39 @@
import { setFailed, saveState, getState } from '@actions/core'
import getInputs from './inputs'
import getInputs, { Inputs } from './inputs'
import installPnpm from './install-pnpm'
import setOutputs from './outputs'
import pnpmInstall from './pnpm-install'
import pruneStore from './pnpm-store-prune'
import restoreCache from './cache-restore'
import saveCache from './cache-save'
async function main() {
const inputs = getInputs()
const isPost = getState('is_post')
if (isPost === 'true') return pruneStore(inputs)
if (getState('is_post') === 'true') {
await runPost(inputs)
} else {
await runMain(inputs)
}
}
async function runMain(inputs: Inputs) {
saveState('is_post', 'true')
await installPnpm(inputs)
console.log('Installation Completed!')
setOutputs(inputs)
await restoreCache(inputs);
pnpmInstall(inputs)
}
async function runPost(inputs: Inputs) {
pruneStore(inputs)
await saveCache(inputs)
}
main().catch(error => {
console.error(error)
setFailed(error)

View File

@@ -5,6 +5,8 @@ import { RunInstall, parseRunInstall } from './run-install'
export interface Inputs {
readonly version?: string
readonly dest: string
readonly cache: boolean
readonly cacheDependencyPath: string
readonly runInstall: RunInstall[]
readonly packageJsonFile: string
readonly standalone: boolean
@@ -19,6 +21,8 @@ const parseInputPath = (name: string) => expandTilde(getInput(name, options))
export const getInputs = (): Inputs => ({
version: getInput('version'),
dest: parseInputPath('dest'),
cache: getBooleanInput('cache'),
cacheDependencyPath: parseInputPath('cache_dependency_path'),
runInstall: parseRunInstall('run_install'),
packageJsonFile: parseInputPath('package_json_file'),
standalone: getBooleanInput('standalone'),