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 d7225dc
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 14 deletions.
57 changes: 49 additions & 8 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 @@ -1691,26 +1697,61 @@ 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 */
}
};
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(fs_1.default.readFileSync(fileName).toString());
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);
}
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 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);
};
47 changes: 42 additions & 5 deletions src/classes/state/state-cache-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,65 @@ 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 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(fs.readFileSync(fileName).toString());
fs.readdir(path.dirname(fileName), (err, files) => {
files.forEach(file => {
core.debug(file);
});
});
await uploadFileToActionsCache(fileName, CACHE_KEY, CACHE_VERSION);
} 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 d7225dc

Please sign in to comment.