cache: gracefully handle cache restore failures with warning

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2025-11-05 09:52:09 +01:00
parent 7ab28f9678
commit d8def31251

View File

@@ -64,8 +64,12 @@ export class Cache {
if (!this.ghaNoCache && cache.isFeatureAvailable()) { if (!this.ghaNoCache && cache.isFeatureAvailable()) {
if (skipState) { if (skipState) {
try {
core.debug(`Cache.save caching ${this.ghaCacheKey} to GitHub Actions cache`); core.debug(`Cache.save caching ${this.ghaCacheKey} to GitHub Actions cache`);
await cache.saveCache([this.cacheDir], this.ghaCacheKey); await cache.saveCache([this.cacheDir], this.ghaCacheKey);
} catch (e) {
core.warning(`Failed to save cache: ${e}`);
}
} else { } else {
core.debug(`Cache.save sending ${this.ghaCacheKey} to post state`); core.debug(`Cache.save sending ${this.ghaCacheKey} to post state`);
core.saveState( core.saveState(
@@ -82,12 +86,12 @@ export class Cache {
} }
public async find(): Promise<string> { public async find(): Promise<string> {
try {
let htcPath = tc.find(this.opts.htcName, this.opts.htcVersion, this.platform()); let htcPath = tc.find(this.opts.htcName, this.opts.htcVersion, this.platform());
if (htcPath) { if (htcPath) {
core.info(`Restored from hosted tool cache ${htcPath}`); core.info(`Restored from hosted tool cache ${htcPath}`);
return this.copyToCache(`${htcPath}/${this.opts.cacheFile}`); return this.copyToCache(`${htcPath}/${this.opts.cacheFile}`);
} }
if (!this.ghaNoCache && cache.isFeatureAvailable()) { if (!this.ghaNoCache && cache.isFeatureAvailable()) {
core.debug(`GitHub Actions cache feature available`); core.debug(`GitHub Actions cache feature available`);
if (await cache.restoreCache([this.cacheDir], this.ghaCacheKey)) { if (await cache.restoreCache([this.cacheDir], this.ghaCacheKey)) {
@@ -101,7 +105,9 @@ export class Cache {
} else { } else {
core.info(`GitHub Actions cache feature not available`); core.info(`GitHub Actions cache feature not available`);
} }
} catch (e) {
core.warning(`Failed to restore cache: ${e}`);
}
return ''; return '';
} }
@@ -120,13 +126,17 @@ export class Cache {
if (!cacheState.dir || !cacheState.key) { if (!cacheState.dir || !cacheState.key) {
throw new Error(`Invalid cache post state: ${state}`); throw new Error(`Invalid cache post state: ${state}`);
} }
try {
core.info(`Caching ${cacheState.key} to GitHub Actions cache`); core.info(`Caching ${cacheState.key} to GitHub Actions cache`);
await cache.saveCache([cacheState.dir], cacheState.key); await cache.saveCache([cacheState.dir], cacheState.key);
} catch (e) {
core.warning(`Failed to save cache: ${e}`);
}
return cacheState; return cacheState;
} }
private copyToCache(file: string): string { private copyToCache(file: string): string {
core.debug(`Copying ${file} to ${this.cachePath}`); core.info(`Copying ${file} to ${this.cachePath}`);
fs.copyFileSync(file, this.cachePath); fs.copyFileSync(file, this.cachePath);
return this.cachePath; return this.cachePath;
} }