Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache: allow to skip state to send cache directly to gha #573

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions __tests__/cache.test.itg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,27 @@ import os from 'os';
import path from 'path';

import {Cache} from '../src/cache';
import {Util} from '../src/util';

const fixturesDir = path.join(__dirname, '.fixtures');
const tmpDir = fs.mkdtempSync(path.join(process.env.TEMP || os.tmpdir(), 'cache-itg-'));

describe('cache', () => {
it('github-repo', async () => {
const r = (Math.random() + 1).toString(36).substring(7);
it('caches github-repo', async () => {
const r = Util.generateRandomString();
const htcName = `cache-test-github-repo-${r}`;
const c = new Cache({
htcName: htcName,
htcVersion: `v1.0.0+${r}`,
baseCacheDir: path.join(tmpDir, '.cache-test'),
cacheFile: 'github-repo.json'
});
expect(await c.save(path.join(fixturesDir, 'github-repo.json'), true)).not.toEqual('');
expect(await c.find()).not.toEqual('');
});

it('caches github-repo with post state', async () => {
const r = Util.generateRandomString();
const htcName = `cache-test-github-repo-${r}`;
const c = new Cache({
htcName: htcName,
Expand All @@ -35,6 +49,7 @@ describe('cache', () => {
cacheFile: 'github-repo.json'
});
expect(await c.save(path.join(fixturesDir, 'github-repo.json'))).not.toEqual('');
expect(await Cache.post()).not.toBeNull();
expect(await c.find()).not.toEqual('');
});
});
23 changes: 14 additions & 9 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,27 @@ export class Cache {
}
}

public async save(file: string): Promise<string> {
public async save(file: string, skipState?: boolean): Promise<string> {
core.debug(`Cache.save ${file}`);
const cachePath = this.copyToCache(file);

const htcPath = await tc.cacheDir(this.cacheDir, this.opts.htcName, this.opts.htcVersion, this.platform());
core.debug(`Cache.save cached to hosted tool cache ${htcPath}`);

if (!this.ghaNoCache && cache.isFeatureAvailable()) {
core.debug(`Cache.save sending ${this.ghaCacheKey} to post state`);
core.saveState(
Cache.POST_CACHE_KEY,
JSON.stringify({
dir: this.cacheDir,
key: this.ghaCacheKey
} as CachePostState)
);
if (skipState) {
core.debug(`Cache.save caching ${this.ghaCacheKey} to GitHub Actions cache`);
await cache.saveCache([this.cacheDir], this.ghaCacheKey);
} else {
core.debug(`Cache.save sending ${this.ghaCacheKey} to post state`);
core.saveState(
Cache.POST_CACHE_KEY,
JSON.stringify({
dir: this.cacheDir,
key: this.ghaCacheKey
} as CachePostState)
);
}
}

return cachePath;
Expand Down
Loading