Skip to content

Commit 659e62f

Browse files
committed
improve package cache
1 parent 1493bec commit 659e62f

File tree

8 files changed

+178
-63
lines changed

8 files changed

+178
-63
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ jobs:
2222
with:
2323
xmake-version: ${{ matrix.version }}
2424
actions-cache-folder: '.xmake-cache'
25+
actions-cache-key: ${{ matrix.version }}
2526
build-cache: true
26-
build-cache-path: 'tests/sample/build/.build_cache'
27+
build-cache-key: ${{ matrix.version }}
2728
package-cache: true
29+
package-cache-key: ${{ matrix.version }}
30+
project-path: 'tests/sample'
2831

2932
- name: Run tests
3033
run: |
34+
echo ${{ github.sha }}
3135
xmake --version
3236
cd tests/sample
3337
xmake -y -vD

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ with:
7070
xmake-version: '2.9.7'
7171
package-cache: true
7272
package-cache-key: 'archlinux-ci'
73+
project-path: 'myproject' # we need to compute packages hashkey for project
7374
```
7475
7576
### Cache build
@@ -90,6 +91,16 @@ with:
9091
build-cache-key: 'archlinux-ci'
9192
```
9293
94+
Cache build with the specific project path.
95+
96+
```yml
97+
uses: xmake-io/github-action-setup-xmake@v1
98+
with:
99+
xmake-version: '2.9.7'
100+
build-cache: true
101+
project-path: 'myproject' # we can get the build cache path from project.
102+
```
103+
93104
Cache build with the specific build path.
94105
95106
```yml

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ inputs:
2222
package-cache-key:
2323
description: The packages cache key.
2424
default: ''
25+
project-path:
26+
description: The project root path.
27+
default: ''
2528
build-cache:
2629
description: Enable build cache.
2730
default: false

dist/index.js

Lines changed: 76 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -82464,6 +82464,17 @@ const cache = __nccwpck_require__(6878);
8246482464
const os = __nccwpck_require__(2037);
8246582465
const path = __nccwpck_require__(1017);
8246682466
const fsutils = __nccwpck_require__(4295);
82467+
function getProjectRootPath() {
82468+
let projectRootPath = core.getInput('project-path');
82469+
if (!projectRootPath) {
82470+
projectRootPath = process.cwd();
82471+
}
82472+
projectRootPath = projectRootPath.trim();
82473+
if (projectRootPath && projectRootPath !== '' && !path.isAbsolute(projectRootPath)) {
82474+
projectRootPath = path.join(process.cwd(), projectRootPath);
82475+
}
82476+
return projectRootPath;
82477+
}
8246782478
function getBuildCacheKey() {
8246882479
var _a;
8246982480
let buildCacheKey = core.getInput('build-cache-key');
@@ -82472,10 +82483,32 @@ function getBuildCacheKey() {
8247282483
}
8247382484
return `xmake-build-cache-${buildCacheKey}-${os.arch()}-${os.platform()}-${(_a = process.env.RUNNER_OS) !== null && _a !== void 0 ? _a : 'unknown'}`;
8247482485
}
82475-
function getBuildCachePath() {
82486+
async function getBuildCachePath() {
8247682487
let buildCachePath = core.getInput('build-cache-path');
8247782488
if (!buildCachePath) {
82478-
buildCachePath = 'build/.build_cache';
82489+
buildCachePath = '';
82490+
const projectRootPath = getProjectRootPath();
82491+
if (projectRootPath && projectRootPath !== '' && fsutils.isDir(projectRootPath)) {
82492+
const options = {};
82493+
options.cwd = projectRootPath;
82494+
options.listeners = {
82495+
stdout: (data) => {
82496+
buildCachePath += data.toString();
82497+
},
82498+
};
82499+
await (0, exec_1.exec)('xmake', [
82500+
'l',
82501+
'-c',
82502+
'import("core.project.config"); import("private.cache.build_cache"); config.load(); print(build_cache.rootdir())',
82503+
], options);
82504+
buildCachePath = buildCachePath.trim();
82505+
if (buildCachePath !== '' && !path.isAbsolute(buildCachePath)) {
82506+
buildCachePath = path.join(projectRootPath, buildCachePath);
82507+
}
82508+
}
82509+
else {
82510+
buildCachePath = 'build/.build_cache';
82511+
}
8247982512
}
8248082513
return buildCachePath;
8248182514
}
@@ -82491,7 +82524,7 @@ async function loadBuildCache() {
8249182524
core.exportVariable('XMAKE_ACTION_BUILD_CACHE', 'true');
8249282525
const buildCacheFolder = getBuildCacheFolder();
8249382526
const buildCacheKey = getBuildCacheKey();
82494-
const buildCachePath = getBuildCachePath();
82527+
const buildCachePath = await getBuildCachePath();
8249582528
if (buildCacheFolder && process.env.GITHUB_WORKSPACE) {
8249682529
const fullCachePath = path.join(process.env.GITHUB_WORKSPACE, buildCacheFolder);
8249782530
const filepath = path.join(fullCachePath, 'build_cache_saved.txt');
@@ -82500,6 +82533,9 @@ async function loadBuildCache() {
8250082533
await cache.restoreCache([buildCacheFolder], buildCacheKey);
8250182534
}
8250282535
if (fsutils.isFile(filepath)) {
82536+
if (fsutils.isDir(buildCachePath)) {
82537+
await io.rmRF(buildCachePath);
82538+
}
8250382539
await io.cp(fullCachePath, buildCachePath, {
8250482540
recursive: true,
8250582541
});
@@ -82518,7 +82554,7 @@ async function saveBuildCache() {
8251882554
}
8251982555
const buildCacheFolder = getBuildCacheFolder();
8252082556
const buildCacheKey = getBuildCacheKey();
82521-
const buildCachePath = getBuildCachePath();
82557+
const buildCachePath = await getBuildCachePath();
8252282558
if (buildCacheFolder && process.env.GITHUB_WORKSPACE && fsutils.isDir(buildCachePath)) {
8252382559
const fullCachePath = path.join(process.env.GITHUB_WORKSPACE, buildCacheFolder);
8252482560
core.info(`Save build cache path: ${buildCachePath} to ${fullCachePath}, key: ${buildCacheKey}`);
@@ -82706,13 +82742,38 @@ const cache = __nccwpck_require__(6878);
8270682742
const os = __nccwpck_require__(2037);
8270782743
const path = __nccwpck_require__(1017);
8270882744
const fsutils = __nccwpck_require__(4295);
82709-
function getPackageCacheKey() {
82745+
function getProjectRootPath() {
82746+
let projectRootPath = core.getInput('project-path');
82747+
if (!projectRootPath) {
82748+
projectRootPath = process.cwd();
82749+
}
82750+
projectRootPath = projectRootPath.trim();
82751+
if (projectRootPath && projectRootPath !== '' && !path.isAbsolute(projectRootPath)) {
82752+
projectRootPath = path.join(process.cwd(), projectRootPath);
82753+
}
82754+
return projectRootPath;
82755+
}
82756+
async function getPackageCacheKey() {
8271082757
var _a;
8271182758
let packageCacheKey = core.getInput('package-cache-key');
8271282759
if (!packageCacheKey) {
8271382760
packageCacheKey = '';
8271482761
}
82715-
return `xmake-package-cache-${packageCacheKey}-${os.arch()}-${os.platform()}-${(_a = process.env.RUNNER_OS) !== null && _a !== void 0 ? _a : 'unknown'}`;
82762+
let packageCacheHash = '';
82763+
const projectRootPath = getProjectRootPath();
82764+
if (projectRootPath && projectRootPath !== '' && fsutils.isDir(projectRootPath)) {
82765+
const options = {};
82766+
options.cwd = projectRootPath;
82767+
options.listeners = {
82768+
stdout: (data) => {
82769+
packageCacheHash += data.toString();
82770+
},
82771+
};
82772+
await (0, exec_1.exec)('xmake', ['repo', '--update']);
82773+
await (0, exec_1.exec)('xmake', ['l', 'utils.ci.packageskey'], options);
82774+
packageCacheHash = packageCacheHash.trim();
82775+
}
82776+
return `xmake-package-cache-${packageCacheKey}-${packageCacheHash}-${os.arch()}-${os.platform()}-${(_a = process.env.RUNNER_OS) !== null && _a !== void 0 ? _a : 'unknown'}`;
8271682777
}
8271782778
async function getPackageCachePath() {
8271882779
let packageCachePath = '';
@@ -82724,7 +82785,6 @@ async function getPackageCachePath() {
8272482785
};
8272582786
await (0, exec_1.exec)('xmake', ['l', '-c', 'import("core.package.package"); print(package.installdir())'], options);
8272682787
packageCachePath = packageCachePath.trim();
82727-
core.info(`packageCachePath: ${packageCachePath}`);
8272882788
return packageCachePath;
8272982789
}
8273082790
function getPackageCacheFolder() {
@@ -82736,7 +82796,7 @@ async function loadPackageCache() {
8273682796
return;
8273782797
}
8273882798
const packageCacheFolder = getPackageCacheFolder();
82739-
const packageCacheKey = getPackageCacheKey();
82799+
const packageCacheKey = await getPackageCacheKey();
8274082800
const packageCachePath = await getPackageCachePath();
8274182801
if (!packageCachePath || packageCachePath === '') {
8274282802
return;
@@ -82749,9 +82809,13 @@ async function loadPackageCache() {
8274982809
await cache.restoreCache([packageCacheFolder], packageCacheKey);
8275082810
}
8275182811
if (fsutils.isFile(filepath)) {
82812+
if (fsutils.isDir(packageCachePath)) {
82813+
await io.rmRF(packageCachePath);
82814+
}
8275282815
await io.cp(fullCachePath, packageCachePath, {
8275382816
recursive: true,
8275482817
});
82818+
core.saveState('hitCache', 'true');
8275582819
}
8275682820
else {
8275782821
core.warning(`No cached files found at path "${fullCachePath}".`);
@@ -82766,18 +82830,18 @@ async function savePackageCache() {
8276682830
return;
8276782831
}
8276882832
const packageCacheFolder = getPackageCacheFolder();
82769-
const packageCacheKey = getPackageCacheKey();
82833+
const packageCacheKey = await getPackageCacheKey();
8277082834
const packageCachePath = await getPackageCachePath();
8277182835
if (!packageCachePath || packageCachePath === '') {
8277282836
return;
8277382837
}
82774-
if (packageCacheFolder && process.env.GITHUB_WORKSPACE && fsutils.isDir(packageCachePath)) {
82838+
const hitCache = !!core.getState('hitCache');
82839+
if (!hitCache && packageCacheFolder && process.env.GITHUB_WORKSPACE && fsutils.isDir(packageCachePath)) {
8277582840
const fullCachePath = path.join(process.env.GITHUB_WORKSPACE, packageCacheFolder);
8277682841
core.info(`Save package cache path: ${packageCachePath} to ${fullCachePath}, key: ${packageCacheKey}`);
8277782842
await io.cp(packageCachePath, fullCachePath, {
8277882843
recursive: true,
8277982844
});
82780-
await (0, exec_1.exec)('xmake', ['l', 'os.touch', path.join(fullCachePath, 'package_cache_saved.txt')]);
8278182845
await cache.saveCache([packageCacheFolder], packageCacheKey);
8278282846
}
8278382847
}
@@ -82792,28 +82856,12 @@ exports.savePackageCache = savePackageCache;
8279282856
"use strict";
8279382857

8279482858
Object.defineProperty(exports, "__esModule", ({ value: true }));
82795-
exports.SshKnownHostsPath = exports.SshKeyPath = exports.PostSetSafeDirectory = exports.RepositoryPath = exports.IsPost = void 0;
82859+
exports.IsPost = void 0;
8279682860
const core = __nccwpck_require__(9093);
8279782861
/**
8279882862
* Indicates whether the POST action is running
8279982863
*/
8280082864
exports.IsPost = !!core.getState('isPost');
82801-
/**
82802-
* The repository path for the POST action. The value is empty during the MAIN action.
82803-
*/
82804-
exports.RepositoryPath = core.getState('repositoryPath');
82805-
/**
82806-
* The set-safe-directory for the POST action. The value is set if input: 'safe-directory' is set during the MAIN action.
82807-
*/
82808-
exports.PostSetSafeDirectory = core.getState('setSafeDirectory') === 'true';
82809-
/**
82810-
* The SSH key path for the POST action. The value is empty during the MAIN action.
82811-
*/
82812-
exports.SshKeyPath = core.getState('sshKeyPath');
82813-
/**
82814-
* The SSH known hosts path for the POST action. The value is empty during the MAIN action.
82815-
*/
82816-
exports.SshKnownHostsPath = core.getState('sshKnownHostsPath');
8281782865
// Publish a variable so that when the POST action runs, it can determine it should run the cleanup logic.
8281882866
// This is necessary since we don't have a separate entry point.
8281982867
if (!exports.IsPost) {

src/build-cache.ts

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
import * as core from '@actions/core';
2-
import { exec } from '@actions/exec';
2+
import { exec, ExecOptions } from '@actions/exec';
33
import * as io from '@actions/io';
44
import * as cache from '@actions/cache';
55
import * as os from 'os';
66
import * as path from 'path';
77
import * as fsutils from './fsutils';
88

9+
function getProjectRootPath(): string {
10+
let projectRootPath = core.getInput('project-path');
11+
if (!projectRootPath) {
12+
projectRootPath = process.cwd();
13+
}
14+
projectRootPath = projectRootPath.trim();
15+
if (projectRootPath && projectRootPath !== '' && !path.isAbsolute(projectRootPath)) {
16+
projectRootPath = path.join(process.cwd(), projectRootPath);
17+
}
18+
return projectRootPath;
19+
}
20+
921
function getBuildCacheKey(): string {
1022
let buildCacheKey = core.getInput('build-cache-key');
1123
if (!buildCacheKey) {
@@ -14,10 +26,35 @@ function getBuildCacheKey(): string {
1426
return `xmake-build-cache-${buildCacheKey}-${os.arch()}-${os.platform()}-${process.env.RUNNER_OS ?? 'unknown'}`;
1527
}
1628

17-
function getBuildCachePath(): string {
29+
async function getBuildCachePath(): Promise<string> {
1830
let buildCachePath = core.getInput('build-cache-path');
1931
if (!buildCachePath) {
20-
buildCachePath = 'build/.build_cache';
32+
buildCachePath = '';
33+
const projectRootPath = getProjectRootPath();
34+
if (projectRootPath && projectRootPath !== '' && fsutils.isDir(projectRootPath)) {
35+
const options: ExecOptions = {};
36+
options.cwd = projectRootPath;
37+
options.listeners = {
38+
stdout: (data: Buffer) => {
39+
buildCachePath += data.toString();
40+
},
41+
};
42+
await exec(
43+
'xmake',
44+
[
45+
'l',
46+
'-c',
47+
'import("core.project.config"); import("private.cache.build_cache"); config.load(); print(build_cache.rootdir())',
48+
],
49+
options,
50+
);
51+
buildCachePath = buildCachePath.trim();
52+
if (buildCachePath !== '' && !path.isAbsolute(buildCachePath)) {
53+
buildCachePath = path.join(projectRootPath, buildCachePath);
54+
}
55+
} else {
56+
buildCachePath = 'build/.build_cache';
57+
}
2158
}
2259
return buildCachePath;
2360
}
@@ -37,7 +74,7 @@ export async function loadBuildCache(): Promise<void> {
3774

3875
const buildCacheFolder = getBuildCacheFolder();
3976
const buildCacheKey = getBuildCacheKey();
40-
const buildCachePath = getBuildCachePath();
77+
const buildCachePath = await getBuildCachePath();
4178

4279
if (buildCacheFolder && process.env.GITHUB_WORKSPACE) {
4380
const fullCachePath = path.join(process.env.GITHUB_WORKSPACE, buildCacheFolder);
@@ -47,6 +84,9 @@ export async function loadBuildCache(): Promise<void> {
4784
await cache.restoreCache([buildCacheFolder], buildCacheKey);
4885
}
4986
if (fsutils.isFile(filepath)) {
87+
if (fsutils.isDir(buildCachePath)) {
88+
await io.rmRF(buildCachePath);
89+
}
5090
await io.cp(fullCachePath, buildCachePath, {
5191
recursive: true,
5292
});
@@ -65,7 +105,7 @@ export async function saveBuildCache(): Promise<void> {
65105

66106
const buildCacheFolder = getBuildCacheFolder();
67107
const buildCacheKey = getBuildCacheKey();
68-
const buildCachePath = getBuildCachePath();
108+
const buildCachePath = await getBuildCachePath();
69109

70110
if (buildCacheFolder && process.env.GITHUB_WORKSPACE && fsutils.isDir(buildCachePath)) {
71111
const fullCachePath = path.join(process.env.GITHUB_WORKSPACE, buildCacheFolder);

0 commit comments

Comments
 (0)