use highlevel actions cache api

This commit is contained in:
Sergey Dolin
2023-07-05 10:37:35 +02:00
parent 56c9f4224b
commit 19f709fe58
9 changed files with 76 additions and 355 deletions

View File

@@ -0,0 +1,9 @@
import * as cache from '@actions/cache';
import path from 'path';
export const downloadFileFromActionsCache = (
destFileName: string,
cacheKey: string,
cacheVersion: string
): Promise<void> =>
cache.restoreCache([path.dirname(destFileName)], cacheKey) as Promise<void>;

View File

@@ -0,0 +1,40 @@
import fs from 'fs';
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';
const resetCacheWithOctokit = async (cacheKey: string): Promise<void> => {
const token = core.getInput('repo-token');
const client = getOctokit(token, undefined, octokitRetry);
// TODO: better way to get repository?
const repo = process.env['GITHUB_REPOSITORY'];
core.debug(`remove cache "${cacheKey}"`);
try {
// TODO: replace with client.rest.
await client.request(
`DELETE /repos/${repo}/actions/caches?key=${cacheKey}`
);
} catch (error) {
if (error.status) {
core.debug(`Cache ${cacheKey} does not exist`);
} else {
throw error;
}
}
};
export const uploadFileToActionsCache = async (
filePath: string,
cacheKey: string,
cacheVersion: string
) => {
await resetCacheWithOctokit(cacheKey);
const fileSize = fs.statSync(filePath).size;
if (fileSize === 0) {
core.info(`the cache ${cacheKey} will be removed`);
return;
}
cache.saveCache([filePath], cacheKey);
};

View File

@@ -3,8 +3,12 @@ import fs from 'fs';
import path from 'path';
import os from 'os';
import * as core from '@actions/core';
import {uploadFileToActionsCache} from '../actions-cache/upload';
import {downloadFileFromActionsCache} from '../actions-cache/download';
import {downloadFileFromActionsCache} from '../actions-cache-hilevel/download';
import {uploadFileToActionsCache} from '../actions-cache-hilevel/upload';
/*
import {uploadFileToActionsCache} from '../actions-cache-internal/upload';
import {downloadFileFromActionsCache} from '../actions-cache-internal/download';
*/
const CACHE_KEY = '_state';
const CACHE_VERSION = '1';