@@ -30,13 +30,20 @@ export interface CacheOpts {
30
30
ghaNoCache ?: boolean ;
31
31
}
32
32
33
+ export interface CachePostState {
34
+ dir : string ;
35
+ key : string ;
36
+ }
37
+
33
38
export class Cache {
34
39
private readonly opts : CacheOpts ;
35
40
private readonly ghaCacheKey : string ;
36
41
private readonly ghaNoCache ?: boolean ;
37
42
private readonly cacheDir : string ;
38
43
private readonly cachePath : string ;
39
44
45
+ private static readonly POST_CACHE_KEY = 'postCache' ;
46
+
40
47
constructor ( opts : CacheOpts ) {
41
48
this . opts = opts ;
42
49
this . ghaCacheKey = util . format ( '%s-%s-%s' , this . opts . htcName , this . opts . htcVersion , this . platform ( ) ) ;
@@ -56,8 +63,14 @@ export class Cache {
56
63
core . debug ( `Cache.save cached to hosted tool cache ${ htcPath } ` ) ;
57
64
58
65
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
+ ) ;
61
74
}
62
75
63
76
return cachePath ;
@@ -75,7 +88,7 @@ export class Cache {
75
88
if ( await cache . restoreCache ( [ this . cacheDir ] , this . ghaCacheKey ) ) {
76
89
core . info ( `Restored ${ this . ghaCacheKey } from GitHub Actions cache` ) ;
77
90
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 } ` ) ;
79
92
return this . copyToCache ( `${ htcPath } /${ this . opts . cacheFile } ` ) ;
80
93
}
81
94
} else if ( this . ghaNoCache ) {
@@ -87,6 +100,26 @@ export class Cache {
87
100
return '' ;
88
101
}
89
102
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
+
90
123
private copyToCache ( file : string ) : string {
91
124
core . debug ( `Copying ${ file } to ${ this . cachePath } ` ) ;
92
125
fs . copyFileSync ( file , this . cachePath ) ;
0 commit comments