Skip to content

Commit

Permalink
use highlevel actions cache api
Browse files Browse the repository at this point in the history
  • Loading branch information
dsame committed Jul 5, 2023
1 parent d8dfea6 commit 8f7bb07
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 15 deletions.
94 changes: 85 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.downloadFileFromActionsCache = void 0;
const cache = __importStar(__nccwpck_require__(7799));
const path_1 = __importDefault(__nccwpck_require__(1017));
const downloadFileFromActionsCache = (destFileName, cacheKey, cacheVersion) => cache.restoreCache([path_1.default.dirname(destFileName)], cacheKey, [
const downloadFileFromActionsCache = (destFileName, cacheKey,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
cacheVersion) => cache.restoreCache([path_1.default.dirname(destFileName)], cacheKey, [
cacheKey
]);
exports.downloadFileFromActionsCache = downloadFileFromActionsCache;
Expand Down Expand Up @@ -91,6 +93,7 @@ const core = __importStar(__nccwpck_require__(2186));
const cache = __importStar(__nccwpck_require__(7799));
const github_1 = __nccwpck_require__(5438);
const plugin_retry_1 = __nccwpck_require__(6298);
const path_1 = __importDefault(__nccwpck_require__(1017));
const resetCacheWithOctokit = (cacheKey) => __awaiter(void 0, void 0, void 0, function* () {
const token = core.getInput('repo-token');
const client = (0, github_1.getOctokit)(token, undefined, plugin_retry_1.retry);
Expand All @@ -110,14 +113,17 @@ const resetCacheWithOctokit = (cacheKey) => __awaiter(void 0, void 0, void 0, fu
}
}
});
const uploadFileToActionsCache = (filePath, cacheKey, cacheVersion) => __awaiter(void 0, void 0, void 0, function* () {
const uploadFileToActionsCache = (filePath, cacheKey,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
cacheVersion) => __awaiter(void 0, void 0, void 0, function* () {
yield resetCacheWithOctokit(cacheKey);
const fileSize = fs_1.default.statSync(filePath).size;
if (fileSize === 0) {
core.info(`the cache ${cacheKey} will be removed`);
return;
}
cache.saveCache([filePath], cacheKey);
core.debug('content: ' + fs_1.default.readFileSync(filePath).toString());
cache.saveCache([path_1.default.dirname(filePath)], cacheKey);
});
exports.uploadFileToActionsCache = uploadFileToActionsCache;

Expand Down Expand Up @@ -1677,40 +1683,110 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.StateCacheStorage = void 0;
exports.StateCacheStorage = exports.getCommandOutput = void 0;
const fs_1 = __importDefault(__nccwpck_require__(7147));
const path_1 = __importDefault(__nccwpck_require__(1017));
const os_1 = __importDefault(__nccwpck_require__(2037));
const core = __importStar(__nccwpck_require__(2186));
const download_1 = __nccwpck_require__(8802);
const upload_1 = __nccwpck_require__(5970);
const exec = __importStar(__nccwpck_require__(1514));
/*
import {uploadFileToActionsCache} from '../actions-cache-internal/upload';
import {downloadFileFromActionsCache} from '../actions-cache-internal/download';
*/
const CACHE_KEY = '_state';
const CACHE_VERSION = '1';
const STATE_FILE = 'state.txt';
const STALE_DIR = '56acbeaa-1fef-4c79-8f84-7565e560fb03';
const mkTempDir = () => {
const tmpDir = path_1.default.join(os_1.default.tmpdir(), STALE_DIR);
fs_1.default.mkdirSync(tmpDir, { recursive: true });
return tmpDir;
};
const unlinkSafely = (filePath) => {
try {
fs_1.default.unlinkSync(filePath);
}
catch (foo) {
/* ignore */
}
};
const getCommandOutput = (toolCommand, cwd) => __awaiter(void 0, void 0, void 0, function* () {
let { stdout, stderr, exitCode } = yield exec.getExecOutput(toolCommand, undefined, Object.assign({ ignoreReturnCode: true }, (cwd && { cwd })));
if (exitCode) {
stderr = !stderr.trim()
? `The '${toolCommand}' command failed with exit code: ${exitCode}`
: stderr;
throw new Error(stderr);
}
return stdout.trim();
});
exports.getCommandOutput = getCommandOutput;
function execCommands(commands, cwd) {
return __awaiter(this, void 0, void 0, function* () {
for (const command of commands) {
try {
yield exec.exec(command, undefined, { cwd });
}
catch (error) {
throw new Error(`${command.split(' ')[0]} failed with error: ${error === null || error === void 0 ? void 0 : error.message}`);
}
}
});
}
class StateCacheStorage {
save(serializedState) {
return __awaiter(this, void 0, void 0, function* () {
const tmpDir = fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), 'state-'));
const file = path_1.default.join(tmpDir, STATE_FILE);
fs_1.default.writeFileSync(file, serializedState);
const tmpDir = mkTempDir();
const fileName = path_1.default.join(tmpDir, STATE_FILE);
fs_1.default.writeFileSync(fileName, serializedState);
try {
yield (0, upload_1.uploadFileToActionsCache)(file, CACHE_KEY, CACHE_VERSION);
core.debug(tmpDir);
core.debug(path_1.default.dirname(fileName));
core.debug(fileName);
core.debug(serializedState);
core.debug('1 ' + fs_1.default.readFileSync(fileName).toString());
yield execCommands(['ls -la'], tmpDir);
fs_1.default.readdir(path_1.default.dirname(fileName), (err, files) => {
files.forEach(file => {
core.debug(file);
});
});
yield (0, upload_1.uploadFileToActionsCache)(fileName, CACHE_KEY, CACHE_VERSION);
yield execCommands([
'pwd',
'ls -la',
'find /home/runner/work/ -name cache.tzst',
'find /home/runner/work/ -name manifest.txt'
], tmpDir);
const tar = yield (0, exports.getCommandOutput)('find /home/runner/work/ -name cache.tzst');
yield execCommands([`tar -tvf ${tar} --use-compress-program zstdmt`]);
const manifest = yield (0, exports.getCommandOutput)('find /home/runner/work/ -name manifest.txt');
yield execCommands([`cat ${manifest}`]);
}
catch (error) {
core.warning(`Saving the state was not successful due to "${error.message || 'unknown reason'}"`);
}
finally {
unlinkSafely(fileName);
}
});
}
restore() {
return __awaiter(this, void 0, void 0, function* () {
const tmpDir = fs_1.default.mkdtempSync('state-');
const tmpDir = mkTempDir(); //fs.mkdtempSync('state-');
const fileName = path_1.default.join(tmpDir, STATE_FILE);
unlinkSafely(fileName);
try {
yield (0, download_1.downloadFileFromActionsCache)(fileName, CACHE_KEY, CACHE_VERSION);
core.debug(tmpDir);
core.debug(path_1.default.dirname(fileName));
fs_1.default.readdir(path_1.default.dirname(fileName), (err, files) => {
files.forEach(file => {
core.debug(file);
});
});
if (!fs_1.default.existsSync(fileName)) {
core.info('The stored state has not been found, probably because of the very first run or the previous run failed');
return '';
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"dependencies": {
"@actions/cache": "^3.2.1",
"@actions/core": "^1.10.0",
"@actions/exec": "^1.1.1",
"@actions/github": "^5.1.1",
"@actions/http-client": "^2.1.0",
"@octokit/core": "^4.2.0",
Expand Down
1 change: 1 addition & 0 deletions src/classes/actions-cache-hilevel/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'path';
export const downloadFileFromActionsCache = (
destFileName: string,
cacheKey: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
cacheVersion: string
): Promise<void> =>
cache.restoreCache([path.dirname(destFileName)], cacheKey, [
Expand Down
5 changes: 4 additions & 1 deletion src/classes/actions-cache-hilevel/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as core from '@actions/core';
import * as cache from '@actions/cache';
import {getOctokit} from '@actions/github';
import {retry as octokitRetry} from '@octokit/plugin-retry';
import path from 'path';

const resetCacheWithOctokit = async (cacheKey: string): Promise<void> => {
const token = core.getInput('repo-token');
Expand All @@ -26,6 +27,7 @@ const resetCacheWithOctokit = async (cacheKey: string): Promise<void> => {
export const uploadFileToActionsCache = async (
filePath: string,
cacheKey: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
cacheVersion: string
) => {
await resetCacheWithOctokit(cacheKey);
Expand All @@ -36,5 +38,6 @@ export const uploadFileToActionsCache = async (
return;
}

cache.saveCache([filePath], cacheKey);
core.debug('content: ' + fs.readFileSync(filePath).toString());
cache.saveCache([path.dirname(filePath)], cacheKey);
};
97 changes: 92 additions & 5 deletions src/classes/state/state-cache-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import os from 'os';
import * as core from '@actions/core';
import {downloadFileFromActionsCache} from '../actions-cache-hilevel/download';
import {uploadFileToActionsCache} from '../actions-cache-hilevel/upload';
import * as exec from '@actions/exec';
/*
import {uploadFileToActionsCache} from '../actions-cache-internal/upload';
import {downloadFileFromActionsCache} from '../actions-cache-internal/download';
Expand All @@ -13,28 +14,114 @@ import {downloadFileFromActionsCache} from '../actions-cache-internal/download';
const CACHE_KEY = '_state';
const CACHE_VERSION = '1';
const STATE_FILE = 'state.txt';
const STALE_DIR = '56acbeaa-1fef-4c79-8f84-7565e560fb03';

const mkTempDir = (): string => {
const tmpDir = path.join(os.tmpdir(), STALE_DIR);
fs.mkdirSync(tmpDir, {recursive: true});
return tmpDir;
};

const unlinkSafely = (filePath: string) => {
try {
fs.unlinkSync(filePath);
} catch (foo) {
/* ignore */
}
};

export const getCommandOutput = async (
toolCommand: string,
cwd?: string
): Promise<string> => {
let {stdout, stderr, exitCode} = await exec.getExecOutput(
toolCommand,
undefined,
{ignoreReturnCode: true, ...(cwd && {cwd})}
);

if (exitCode) {
stderr = !stderr.trim()
? `The '${toolCommand}' command failed with exit code: ${exitCode}`
: stderr;
throw new Error(stderr);
}

return stdout.trim();
};
async function execCommands(commands: string[], cwd?: string): Promise<void> {
for (const command of commands) {
try {
await exec.exec(command, undefined, {cwd});
} catch (error) {
throw new Error(
`${command.split(' ')[0]} failed with error: ${error?.message}`
);
}
}
}
export class StateCacheStorage implements IStateStorage {
async save(serializedState: string): Promise<void> {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'state-'));
const file = path.join(tmpDir, STATE_FILE);
fs.writeFileSync(file, serializedState);
const tmpDir = mkTempDir();
const fileName = path.join(tmpDir, STATE_FILE);
fs.writeFileSync(fileName, serializedState);

try {
await uploadFileToActionsCache(file, CACHE_KEY, CACHE_VERSION);
core.debug(tmpDir);
core.debug(path.dirname(fileName));
core.debug(fileName);
core.debug(serializedState);
core.debug('1 ' + fs.readFileSync(fileName).toString());
await execCommands(['ls -la'], tmpDir);
fs.readdir(path.dirname(fileName), (err, files) => {
files.forEach(file => {
core.debug(file);
});
});
await uploadFileToActionsCache(fileName, CACHE_KEY, CACHE_VERSION);
await execCommands(
[
'pwd',
'ls -la',
'find /home/runner/work/ -name cache.tzst',
'find /home/runner/work/ -name manifest.txt'
],
tmpDir
);
const tar = await getCommandOutput(
'find /home/runner/work/ -name cache.tzst'
);
await execCommands([`tar -tvf ${tar} --use-compress-program zstdmt`]);
const manifest = await getCommandOutput(
'find /home/runner/work/ -name manifest.txt'
);
await execCommands([`cat ${manifest}`]);
} catch (error) {
core.warning(
`Saving the state was not successful due to "${
error.message || 'unknown reason'
}"`
);
} finally {
unlinkSafely(fileName);
}
}

async restore(): Promise<string> {
const tmpDir = fs.mkdtempSync('state-');
const tmpDir = mkTempDir(); //fs.mkdtempSync('state-');
const fileName = path.join(tmpDir, STATE_FILE);
unlinkSafely(fileName);
try {
await downloadFileFromActionsCache(fileName, CACHE_KEY, CACHE_VERSION);

core.debug(tmpDir);
core.debug(path.dirname(fileName));
fs.readdir(path.dirname(fileName), (err, files) => {
files.forEach(file => {
core.debug(file);
});
});

if (!fs.existsSync(fileName)) {
core.info(
'The stored state has not been found, probably because of the very first run or the previous run failed'
Expand Down

0 comments on commit 8f7bb07

Please sign in to comment.