improve package cache

This commit is contained in:
ruki
2025-01-15 23:42:48 +08:00
Unverified
parent 1493bec260
commit cf24e61b4e
8 changed files with 280 additions and 88 deletions
+97 -17
View File
@@ -1,23 +1,83 @@
import * as core from '@actions/core';
import { exec } from '@actions/exec';
import { exec, ExecOptions } from '@actions/exec';
import * as io from '@actions/io';
import * as cache from '@actions/cache';
import * as os from 'os';
import * as path from 'path';
import * as fsutils from './fsutils';
function getBuildCacheKey(): string {
function getBuildTime(hours?: string): string {
let key = 'BuildTime';
if (hours && hours !== '') {
key = key + hours;
}
let buildTime = core.getState(key);
if (!buildTime || buildTime === '') {
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
if (!hours || hours === '') {
hours = String(now.getHours()).padStart(2, '0');
}
buildTime = `${year}${month}${day}_${hours}`;
core.saveState(key, buildTime);
}
return buildTime;
}
function getProjectRootPath(): string {
let projectRootPath = core.getInput('project-path');
if (!projectRootPath) {
projectRootPath = process.cwd();
}
projectRootPath = projectRootPath.trim();
if (projectRootPath && projectRootPath !== '' && !path.isAbsolute(projectRootPath)) {
projectRootPath = path.join(process.cwd(), projectRootPath);
}
return projectRootPath;
}
function getBuildCacheKey(buildCacheTime?: string): string {
let buildCacheKey = core.getInput('build-cache-key');
if (!buildCacheKey) {
buildCacheKey = '';
}
return `xmake-build-cache-${buildCacheKey}-${os.arch()}-${os.platform()}-${process.env.RUNNER_OS ?? 'unknown'}`;
if (!buildCacheTime || buildCacheTime === '') {
buildCacheTime = getBuildTime();
}
return `xmake-build-cache-${buildCacheKey}-${buildCacheTime}-${os.arch()}-${os.platform()}-${process.env.RUNNER_OS ?? 'unknown'}`;
}
function getBuildCachePath(): string {
async function getBuildCachePath(): Promise<string> {
let buildCachePath = core.getInput('build-cache-path');
if (!buildCachePath) {
buildCachePath = 'build/.build_cache';
buildCachePath = '';
const projectRootPath = getProjectRootPath();
if (projectRootPath && projectRootPath !== '' && fsutils.isDir(projectRootPath)) {
const options: ExecOptions = {};
options.cwd = projectRootPath;
options.listeners = {
stdout: (data: Buffer) => {
buildCachePath += data.toString();
},
};
await exec(
'xmake',
[
'l',
'-c',
'import("core.project.config"); import("private.cache.build_cache"); config.load(); print(build_cache.rootdir())',
],
options,
);
buildCachePath = buildCachePath.trim();
if (buildCachePath !== '' && !path.isAbsolute(buildCachePath)) {
buildCachePath = path.join(projectRootPath, buildCachePath);
}
} else {
buildCachePath = 'build/.build_cache';
}
}
return buildCachePath;
}
@@ -36,21 +96,40 @@ export async function loadBuildCache(): Promise<void> {
core.exportVariable('XMAKE_ACTION_BUILD_CACHE', 'true');
const buildCacheFolder = getBuildCacheFolder();
const buildCacheKey = getBuildCacheKey();
const buildCachePath = getBuildCachePath();
const buildCachePath = await getBuildCachePath();
if (buildCacheFolder && process.env.GITHUB_WORKSPACE) {
const fullCachePath = path.join(process.env.GITHUB_WORKSPACE, buildCacheFolder);
const filepath = path.join(fullCachePath, 'build_cache_saved.txt');
if (!fsutils.isFile(filepath)) {
core.info(`Restore build cache path: ${fullCachePath} to ${buildCachePath}, key: ${buildCacheKey}`);
await cache.restoreCache([buildCacheFolder], buildCacheKey);
// Since action/cache cannot overwrite updates, we try to restore the cache from the last 24 hours.
const now = new Date();
for (let i = 0; i < 24; i++) {
const hours = now.getHours() - i;
if (hours < 0) {
break;
}
const buildCacheKey = getBuildCacheKey(getBuildTime(String(hours).padStart(2, '0')));
if (!fsutils.isFile(filepath)) {
core.info(`Restore build cache path: ${fullCachePath} to ${buildCachePath}, key: ${buildCacheKey}`);
await cache.restoreCache([buildCacheFolder], buildCacheKey);
}
if (fsutils.isFile(filepath)) {
if (fsutils.isDir(buildCachePath)) {
await io.rmRF(buildCachePath);
}
await io.cp(fullCachePath, buildCachePath, {
recursive: true,
});
/* Even if the historical cache is hit, we need to update to the latest cache
* Therefore, only when the latest cache is hit, it is a real hit, and we no longer need to update the cache.
*/
if (i === 0) {
core.saveState('hitBuildCache', 'true');
}
break;
}
}
if (fsutils.isFile(filepath)) {
await io.cp(fullCachePath, buildCachePath, {
recursive: true,
});
} else {
if (!fsutils.isFile(filepath)) {
core.warning(`No cached files found at path "${fullCachePath}".`);
await io.rmRF(fullCachePath);
}
@@ -65,9 +144,10 @@ export async function saveBuildCache(): Promise<void> {
const buildCacheFolder = getBuildCacheFolder();
const buildCacheKey = getBuildCacheKey();
const buildCachePath = getBuildCachePath();
const buildCachePath = await getBuildCachePath();
if (buildCacheFolder && process.env.GITHUB_WORKSPACE && fsutils.isDir(buildCachePath)) {
const hitBuildCache = !!core.getState('hitBuildCache');
if (!hitBuildCache && buildCacheFolder && process.env.GITHUB_WORKSPACE && fsutils.isDir(buildCachePath)) {
const fullCachePath = path.join(process.env.GITHUB_WORKSPACE, buildCacheFolder);
core.info(`Save build cache path: ${buildCachePath} to ${fullCachePath}, key: ${buildCacheKey}`);
await io.cp(buildCachePath, fullCachePath, {
+36 -7
View File
@@ -6,12 +6,38 @@ import * as os from 'os';
import * as path from 'path';
import * as fsutils from './fsutils';
function getPackageCacheKey(): string {
function getProjectRootPath(): string {
let projectRootPath = core.getInput('project-path');
if (!projectRootPath) {
projectRootPath = process.cwd();
}
projectRootPath = projectRootPath.trim();
if (projectRootPath && projectRootPath !== '' && !path.isAbsolute(projectRootPath)) {
projectRootPath = path.join(process.cwd(), projectRootPath);
}
return projectRootPath;
}
async function getPackageCacheKey(): Promise<string> {
let packageCacheKey = core.getInput('package-cache-key');
if (!packageCacheKey) {
packageCacheKey = '';
}
return `xmake-package-cache-${packageCacheKey}-${os.arch()}-${os.platform()}-${process.env.RUNNER_OS ?? 'unknown'}`;
let packageCacheHash = '';
const projectRootPath = getProjectRootPath();
if (projectRootPath && projectRootPath !== '' && fsutils.isDir(projectRootPath)) {
const options: ExecOptions = {};
options.cwd = projectRootPath;
options.listeners = {
stdout: (data: Buffer) => {
packageCacheHash += data.toString();
},
};
await exec('xmake', ['repo', '--update']);
await exec('xmake', ['l', 'utils.ci.packageskey'], options);
packageCacheHash = packageCacheHash.trim();
}
return `xmake-package-cache-${packageCacheKey}-${packageCacheHash}-${os.arch()}-${os.platform()}-${process.env.RUNNER_OS ?? 'unknown'}`;
}
async function getPackageCachePath(): Promise<string> {
@@ -24,7 +50,6 @@ async function getPackageCachePath(): Promise<string> {
};
await exec('xmake', ['l', '-c', 'import("core.package.package"); print(package.installdir())'], options);
packageCachePath = packageCachePath.trim();
core.info(`packageCachePath: ${packageCachePath}`);
return packageCachePath;
}
@@ -39,7 +64,7 @@ export async function loadPackageCache(): Promise<void> {
}
const packageCacheFolder = getPackageCacheFolder();
const packageCacheKey = getPackageCacheKey();
const packageCacheKey = await getPackageCacheKey();
const packageCachePath = await getPackageCachePath();
if (!packageCachePath || packageCachePath === '') {
return;
@@ -53,9 +78,13 @@ export async function loadPackageCache(): Promise<void> {
await cache.restoreCache([packageCacheFolder], packageCacheKey);
}
if (fsutils.isFile(filepath)) {
if (fsutils.isDir(packageCachePath)) {
await io.rmRF(packageCachePath);
}
await io.cp(fullCachePath, packageCachePath, {
recursive: true,
});
core.saveState('hitPackageCache', 'true');
} else {
core.warning(`No cached files found at path "${fullCachePath}".`);
await io.rmRF(fullCachePath);
@@ -70,19 +99,19 @@ export async function savePackageCache(): Promise<void> {
}
const packageCacheFolder = getPackageCacheFolder();
const packageCacheKey = getPackageCacheKey();
const packageCacheKey = await getPackageCacheKey();
const packageCachePath = await getPackageCachePath();
if (!packageCachePath || packageCachePath === '') {
return;
}
if (packageCacheFolder && process.env.GITHUB_WORKSPACE && fsutils.isDir(packageCachePath)) {
const hitPackageCache = !!core.getState('hitPackageCache');
if (!hitPackageCache && packageCacheFolder && process.env.GITHUB_WORKSPACE && fsutils.isDir(packageCachePath)) {
const fullCachePath = path.join(process.env.GITHUB_WORKSPACE, packageCacheFolder);
core.info(`Save package cache path: ${packageCachePath} to ${fullCachePath}, key: ${packageCacheKey}`);
await io.cp(packageCachePath, fullCachePath, {
recursive: true,
});
await exec('xmake', ['l', 'os.touch', path.join(fullCachePath, 'package_cache_saved.txt')]);
await cache.saveCache([packageCacheFolder], packageCacheKey);
}
}
-20
View File
@@ -5,26 +5,6 @@ import * as core from '@actions/core';
*/
export const IsPost = !!core.getState('isPost');
/**
* The repository path for the POST action. The value is empty during the MAIN action.
*/
export const RepositoryPath = core.getState('repositoryPath');
/**
* The set-safe-directory for the POST action. The value is set if input: 'safe-directory' is set during the MAIN action.
*/
export const PostSetSafeDirectory = core.getState('setSafeDirectory') === 'true';
/**
* The SSH key path for the POST action. The value is empty during the MAIN action.
*/
export const SshKeyPath = core.getState('sshKeyPath');
/**
* The SSH known hosts path for the POST action. The value is empty during the MAIN action.
*/
export const SshKnownHostsPath = core.getState('sshKnownHostsPath');
// Publish a variable so that when the POST action runs, it can determine it should run the cleanup logic.
// This is necessary since we don't have a separate entry point.
if (!IsPost) {