Skip to content

Commit

Permalink
use highlevel actions cache api for save
Browse files Browse the repository at this point in the history
  • Loading branch information
dsame committed Jul 5, 2023
1 parent d8dfea6 commit 75127e6
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 94 deletions.
202 changes: 114 additions & 88 deletions dist/index.js
Expand Up @@ -36,92 +36,14 @@ 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;


/***/ }),

/***/ 5970:
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {

"use strict";

var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.uploadFileToActionsCache = void 0;
const fs_1 = __importDefault(__nccwpck_require__(7147));
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 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);
// TODO: better way to get repository?
const repo = process.env['GITHUB_REPOSITORY'];
core.debug(`remove cache "${cacheKey}"`);
try {
// TODO: replace with client.rest.
yield client.request(`DELETE /repos/${repo}/actions/caches?key=${cacheKey}`);
}
catch (error) {
if (error.status) {
core.debug(`Cache ${cacheKey} does not exist`);
}
else {
throw error;
}
}
});
const uploadFileToActionsCache = (filePath, cacheKey, 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);
});
exports.uploadFileToActionsCache = uploadFileToActionsCache;


/***/ }),

/***/ 7236:
Expand Down Expand Up @@ -1677,40 +1599,144 @@ 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));
const github_1 = __nccwpck_require__(5438);
const plugin_retry_1 = __nccwpck_require__(6298);
const cache = __importStar(__nccwpck_require__(7799));
/*
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}`);
}
}
});
}
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);
// TODO: better way to get repository?
const repo = process.env['GITHUB_REPOSITORY'];
core.debug(`remove cache "${cacheKey}"`);
try {
// TODO: replace with client.rest.
yield client.request(`DELETE /repos/${repo}/actions/caches?key=${cacheKey}`);
}
catch (error) {
if (error.status) {
core.debug(`Cache ${cacheKey} does not exist`);
}
else {
throw error;
}
}
});
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 filePath = path_1.default.join(tmpDir, STATE_FILE);
fs_1.default.writeFileSync(filePath, serializedState);
try {
yield (0, upload_1.uploadFileToActionsCache)(file, CACHE_KEY, CACHE_VERSION);
/*
core.debug(tmpDir);
core.debug(path.dirname(filePath));
core.debug(filePath);
core.debug(serializedState);
core.debug('1 ' + fs.readFileSync(filePath).toString());
await execCommands(['ls -la'], tmpDir);
fs.readdir(path.dirname(filePath), (err, files) => {
files.forEach(file => {
core.debug(file);
});
});
*/
yield resetCacheWithOctokit(CACHE_KEY);
const fileSize = fs_1.default.statSync(filePath).size;
if (fileSize === 0) {
core.info(`the cache ${CACHE_KEY} will be removed`);
return;
}
// core.debug('content: ' + fs.readFileSync(filePath).toString());
yield cache.saveCache([path_1.default.dirname(filePath)], CACHE_KEY);
/*
await uploadFileToActionsCache(filePath, 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(filePath);
}
});
}
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);
yield execCommands([`ls -la ${path_1.default.dirname(fileName)}`]);
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
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
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
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);
};

0 comments on commit 75127e6

Please sign in to comment.