Skip to content

Commit cd14c5b

Browse files
authored
Merge pull request #280 from crazy-max/post-cache
cache: move gha cache save to post state
2 parents cbcf885 + 9b446bf commit cd14c5b

File tree

3 files changed

+83
-5
lines changed

3 files changed

+83
-5
lines changed

__tests__/cache.test.itg.ts

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright 2024 actions-toolkit authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import path from 'path';
18+
import {describe, expect, it} from '@jest/globals';
19+
20+
import {Cache} from '../src/cache';
21+
22+
// prettier-ignore
23+
const tmpDir = path.join(process.env.TEMP || '/tmp', 'cache-jest');
24+
25+
const fixturesDir = path.join(__dirname, 'fixtures');
26+
27+
describe('cache', () => {
28+
it('github-repo', async () => {
29+
const r = (Math.random() + 1).toString(36).substring(7);
30+
const htcName = `cache-test-github-repo-${r}`;
31+
const c = new Cache({
32+
htcName: htcName,
33+
htcVersion: `v1.0.0+${r}`,
34+
baseCacheDir: path.join(tmpDir, '.cache-test'),
35+
cacheFile: 'github-repo.json'
36+
});
37+
expect(await c.save(path.join(fixturesDir, 'github-repo.json'))).not.toEqual('');
38+
expect(await c.find()).not.toEqual('');
39+
});
40+
});

src/cache.ts

+36-3
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,20 @@ export interface CacheOpts {
3030
ghaNoCache?: boolean;
3131
}
3232

33+
export interface CachePostState {
34+
dir: string;
35+
key: string;
36+
}
37+
3338
export class Cache {
3439
private readonly opts: CacheOpts;
3540
private readonly ghaCacheKey: string;
3641
private readonly ghaNoCache?: boolean;
3742
private readonly cacheDir: string;
3843
private readonly cachePath: string;
3944

45+
private static readonly POST_CACHE_KEY = 'postCache';
46+
4047
constructor(opts: CacheOpts) {
4148
this.opts = opts;
4249
this.ghaCacheKey = util.format('%s-%s-%s', this.opts.htcName, this.opts.htcVersion, this.platform());
@@ -56,8 +63,14 @@ export class Cache {
5663
core.debug(`Cache.save cached to hosted tool cache ${htcPath}`);
5764

5865
if (!this.ghaNoCache && cache.isFeatureAvailable()) {
59-
core.debug(`Cache.save caching ${this.ghaCacheKey} to GitHub Actions cache`);
60-
await cache.saveCache([this.cacheDir], this.ghaCacheKey);
66+
core.debug(`Cache.save sending ${this.ghaCacheKey} to post state`);
67+
core.saveState(
68+
Cache.POST_CACHE_KEY,
69+
JSON.stringify({
70+
dir: this.cacheDir,
71+
key: this.ghaCacheKey
72+
} as CachePostState)
73+
);
6174
}
6275

6376
return cachePath;
@@ -75,7 +88,7 @@ export class Cache {
7588
if (await cache.restoreCache([this.cacheDir], this.ghaCacheKey)) {
7689
core.info(`Restored ${this.ghaCacheKey} from GitHub Actions cache`);
7790
htcPath = await tc.cacheDir(this.cacheDir, this.opts.htcName, this.opts.htcVersion, this.platform());
78-
core.info(`Restored to hosted tool cache ${htcPath}`);
91+
core.info(`Cached to hosted tool cache ${htcPath}`);
7992
return this.copyToCache(`${htcPath}/${this.opts.cacheFile}`);
8093
}
8194
} else if (this.ghaNoCache) {
@@ -87,6 +100,26 @@ export class Cache {
87100
return '';
88101
}
89102

103+
public static async post(): Promise<CachePostState | undefined> {
104+
const state = core.getState(Cache.POST_CACHE_KEY);
105+
if (!state) {
106+
core.debug(`Cache.post no state`);
107+
return Promise.resolve(undefined);
108+
}
109+
let cacheState: CachePostState;
110+
try {
111+
cacheState = <CachePostState>JSON.parse(state);
112+
} catch (e) {
113+
throw new Error(`Failed to parse cache post state: ${e}`);
114+
}
115+
if (!cacheState.dir || !cacheState.key) {
116+
throw new Error(`Invalid cache post state: ${state}`);
117+
}
118+
core.info(`Caching ${cacheState.key} to GitHub Actions cache`);
119+
await cache.saveCache([cacheState.dir], cacheState.key);
120+
return cacheState;
121+
}
122+
90123
private copyToCache(file: string): string {
91124
core.debug(`Copying ${file} to ${this.cachePath}`);
92125
fs.copyFileSync(file, this.cachePath);

src/index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import * as core from '@actions/core';
1818

19+
import {Cache} from './cache';
20+
1921
const isPost = !!process.env['STATE_isPost'];
2022
if (!isPost) {
2123
core.saveState('isPost', 'true');
@@ -36,7 +38,10 @@ export async function run(main: () => Promise<void>, post?: () => Promise<void>)
3638
} catch (e) {
3739
core.setFailed(e.message);
3840
}
39-
} else if (post) {
40-
await post();
41+
} else {
42+
if (post) {
43+
await post();
44+
}
45+
await Cache.post();
4146
}
4247
}

0 commit comments

Comments
 (0)