Skip to content

Commit

Permalink
improve package cache
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed Jan 15, 2025
1 parent 1493bec commit a27c4d9
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 63 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ jobs:
with:
xmake-version: ${{ matrix.version }}
actions-cache-folder: '.xmake-cache'
actions-cache-key: ${{ matrix.version }}
build-cache: true
build-cache-path: 'tests/sample/build/.build_cache'
build-cache-key: ${{ matrix.version }}
package-cache: true
package-cache-key: ${{ matrix.version }}
project-path: 'tests/sample'

- name: Run tests
run: |
echo ${{ github.sha }}
xmake --version
cd tests/sample
xmake -y -vD
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ with:
xmake-version: '2.9.7'
package-cache: true
package-cache-key: 'archlinux-ci'
project-path: 'myproject' # we need to compute packages hashkey for project
```
### Cache build
Expand All @@ -90,6 +91,16 @@ with:
build-cache-key: 'archlinux-ci'
```
Cache build with the specific project path.
```yml
uses: xmake-io/github-action-setup-xmake@v1
with:
xmake-version: '2.9.7'
build-cache: true
project-path: 'myproject' # we can get the build cache path from project.
```
Cache build with the specific build path.
```yml
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ inputs:
package-cache-key:
description: The packages cache key.
default: ''
project-path:
description: The project root path.
default: ''
build-cache:
description: Enable build cache.
default: false
Expand Down
104 changes: 76 additions & 28 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82464,6 +82464,17 @@ const cache = __nccwpck_require__(6878);
const os = __nccwpck_require__(2037);
const path = __nccwpck_require__(1017);
const fsutils = __nccwpck_require__(4295);
function getProjectRootPath() {
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() {
var _a;
let buildCacheKey = core.getInput('build-cache-key');
Expand All @@ -82472,10 +82483,32 @@ function getBuildCacheKey() {
}
return `xmake-build-cache-${buildCacheKey}-${os.arch()}-${os.platform()}-${(_a = process.env.RUNNER_OS) !== null && _a !== void 0 ? _a : 'unknown'}`;
}
function getBuildCachePath() {
async function getBuildCachePath() {
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 = {};
options.cwd = projectRootPath;
options.listeners = {
stdout: (data) => {
buildCachePath += data.toString();
},
};
await (0, exec_1.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;
}
Expand All @@ -82491,7 +82524,7 @@ async function loadBuildCache() {
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');
Expand All @@ -82500,6 +82533,9 @@ async function loadBuildCache() {
await cache.restoreCache([buildCacheFolder], buildCacheKey);
}
if (fsutils.isFile(filepath)) {
if (fsutils.isDir(buildCachePath)) {
await io.rmRF(buildCachePath);
}
await io.cp(fullCachePath, buildCachePath, {
recursive: true,
});
Expand All @@ -82518,7 +82554,7 @@ async function saveBuildCache() {
}
const buildCacheFolder = getBuildCacheFolder();
const buildCacheKey = getBuildCacheKey();
const buildCachePath = getBuildCachePath();
const buildCachePath = await getBuildCachePath();
if (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}`);
Expand Down Expand Up @@ -82706,13 +82742,38 @@ const cache = __nccwpck_require__(6878);
const os = __nccwpck_require__(2037);
const path = __nccwpck_require__(1017);
const fsutils = __nccwpck_require__(4295);
function getPackageCacheKey() {
function getProjectRootPath() {
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() {
var _a;
let packageCacheKey = core.getInput('package-cache-key');
if (!packageCacheKey) {
packageCacheKey = '';
}
return `xmake-package-cache-${packageCacheKey}-${os.arch()}-${os.platform()}-${(_a = process.env.RUNNER_OS) !== null && _a !== void 0 ? _a : 'unknown'}`;
let packageCacheHash = '';
const projectRootPath = getProjectRootPath();
if (projectRootPath && projectRootPath !== '' && fsutils.isDir(projectRootPath)) {
const options = {};
options.cwd = projectRootPath;
options.listeners = {
stdout: (data) => {
packageCacheHash += data.toString();
},
};
await (0, exec_1.exec)('xmake', ['repo', '--update']);
await (0, exec_1.exec)('xmake', ['l', 'utils.ci.packageskey'], options);
packageCacheHash = packageCacheHash.trim();
}
return `xmake-package-cache-${packageCacheKey}-${packageCacheHash}-${os.arch()}-${os.platform()}-${(_a = process.env.RUNNER_OS) !== null && _a !== void 0 ? _a : 'unknown'}`;
}
async function getPackageCachePath() {
let packageCachePath = '';
Expand All @@ -82724,7 +82785,6 @@ async function getPackageCachePath() {
};
await (0, exec_1.exec)('xmake', ['l', '-c', 'import("core.package.package"); print(package.installdir())'], options);
packageCachePath = packageCachePath.trim();
core.info(`packageCachePath: ${packageCachePath}`);
return packageCachePath;
}
function getPackageCacheFolder() {
Expand All @@ -82736,7 +82796,7 @@ async function loadPackageCache() {
return;
}
const packageCacheFolder = getPackageCacheFolder();
const packageCacheKey = getPackageCacheKey();
const packageCacheKey = await getPackageCacheKey();
const packageCachePath = await getPackageCachePath();
if (!packageCachePath || packageCachePath === '') {
return;
Expand All @@ -82749,9 +82809,13 @@ async function loadPackageCache() {
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('hitCache', 'true');
}
else {
core.warning(`No cached files found at path "${fullCachePath}".`);
Expand All @@ -82766,18 +82830,18 @@ async function savePackageCache() {
return;
}
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 hitCache = !!core.getState('hitCache');
if (!hitCache && 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 (0, exec_1.exec)('xmake', ['l', 'os.touch', path.join(fullCachePath, 'package_cache_saved.txt')]);
await cache.saveCache([packageCacheFolder], packageCacheKey);
}
}
Expand All @@ -82792,28 +82856,12 @@ exports.savePackageCache = savePackageCache;
"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.SshKnownHostsPath = exports.SshKeyPath = exports.PostSetSafeDirectory = exports.RepositoryPath = exports.IsPost = void 0;
exports.IsPost = void 0;
const core = __nccwpck_require__(9093);
/**
* Indicates whether the POST action is running
*/
exports.IsPost = !!core.getState('isPost');
/**
* The repository path for the POST action. The value is empty during the MAIN action.
*/
exports.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.
*/
exports.PostSetSafeDirectory = core.getState('setSafeDirectory') === 'true';
/**
* The SSH key path for the POST action. The value is empty during the MAIN action.
*/
exports.SshKeyPath = core.getState('sshKeyPath');
/**
* The SSH known hosts path for the POST action. The value is empty during the MAIN action.
*/
exports.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 (!exports.IsPost) {
Expand Down
50 changes: 45 additions & 5 deletions src/build-cache.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
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 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(): string {
let buildCacheKey = core.getInput('build-cache-key');
if (!buildCacheKey) {
Expand All @@ -14,10 +26,35 @@ function getBuildCacheKey(): string {
return `xmake-build-cache-${buildCacheKey}-${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;
}
Expand All @@ -37,7 +74,7 @@ export async function loadBuildCache(): Promise<void> {

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);
Expand All @@ -47,6 +84,9 @@ export async function loadBuildCache(): Promise<void> {
await cache.restoreCache([buildCacheFolder], buildCacheKey);
}
if (fsutils.isFile(filepath)) {
if (fsutils.isDir(buildCachePath)) {
await io.rmRF(buildCachePath);
}
await io.cp(fullCachePath, buildCachePath, {
recursive: true,
});
Expand All @@ -65,7 +105,7 @@ 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 fullCachePath = path.join(process.env.GITHUB_WORKSPACE, buildCacheFolder);
Expand Down
Loading

0 comments on commit a27c4d9

Please sign in to comment.