Skip to content

Commit 856f207

Browse files
committed
Make caching more verbose
- Print cache size when saving cache similarly to restoring - Print restore success similarly to saving - Print cached file list if debug logging is enabled See also: actions/cache#471
1 parent 73d5917 commit 856f207

4 files changed

Lines changed: 112 additions & 1 deletion

File tree

packages/cache/__tests__/tar.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,75 @@ test('gzip create tar', async () => {
186186
}
187187
)
188188
})
189+
190+
test('zstd list tar', async () => {
191+
const execMock = jest.spyOn(exec, 'exec')
192+
193+
const archivePath = IS_WINDOWS
194+
? `${process.env['windir']}\\fakepath\\cache.tar`
195+
: 'cache.tar'
196+
const tarPath = 'tar'
197+
198+
await tar.listTar(archivePath, CompressionMethod.Zstd)
199+
200+
expect(execMock).toHaveBeenCalledTimes(1)
201+
expect(execMock).toHaveBeenCalledWith(
202+
`"${tarPath}"`,
203+
[
204+
'--use-compress-program',
205+
'zstd -d --long=30',
206+
'-tf',
207+
IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath,
208+
'-P'
209+
].concat(IS_WINDOWS ? ['--force-local'] : []),
210+
{cwd: undefined}
211+
)
212+
})
213+
214+
test('zstdWithoutLong list tar', async () => {
215+
const execMock = jest.spyOn(exec, 'exec')
216+
217+
const archivePath = IS_WINDOWS
218+
? `${process.env['windir']}\\fakepath\\cache.tar`
219+
: 'cache.tar'
220+
const tarPath = 'tar'
221+
222+
await tar.listTar(archivePath, CompressionMethod.zstdWithoutLong)
223+
224+
expect(execMock).toHaveBeenCalledTimes(1)
225+
expect(execMock).toHaveBeenCalledWith(
226+
`"${tarPath}"`,
227+
[
228+
'--use-compress-program',
229+
'zstd -d',
230+
'-tf',
231+
IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath,
232+
'-P'
233+
].concat(IS_WINDOWS ? ['--force-local'] : []),
234+
{cwd: undefined}
235+
)
236+
})
237+
238+
test('gzip list tar', async () => {
239+
const execMock = jest.spyOn(exec, 'exec')
240+
const archivePath = IS_WINDOWS
241+
? `${process.env['windir']}\\fakepath\\cache.tar`
242+
: 'cache.tar'
243+
244+
await tar.listTar(archivePath, CompressionMethod.Gzip)
245+
246+
const tarPath = IS_WINDOWS
247+
? `${process.env['windir']}\\System32\\tar.exe`
248+
: 'tar'
249+
expect(execMock).toHaveBeenCalledTimes(1)
250+
expect(execMock).toHaveBeenCalledWith(
251+
`"${tarPath}"`,
252+
[
253+
'-z',
254+
'-tf',
255+
IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath,
256+
'-P'
257+
],
258+
{cwd: undefined}
259+
)
260+
})

packages/cache/src/cache.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as core from '@actions/core'
22
import * as path from 'path'
33
import * as utils from './internal/cacheUtils'
44
import * as cacheHttpClient from './internal/cacheHttpClient'
5-
import {createTar, extractTar} from './internal/tar'
5+
import {createTar, extractTar, listTar} from './internal/tar'
66
import {DownloadOptions, UploadOptions} from './options'
77

88
export class ValidationError extends Error {
@@ -100,6 +100,10 @@ export async function restoreCache(
100100
options
101101
)
102102

103+
if (core.isDebug()) {
104+
await listTar(archivePath, compressionMethod)
105+
}
106+
103107
const archiveFileSize = utils.getArchiveFileSizeIsBytes(archivePath)
104108
core.info(
105109
`Cache Size: ~${Math.round(
@@ -108,6 +112,7 @@ export async function restoreCache(
108112
)
109113

110114
await extractTar(archivePath, compressionMethod)
115+
core.info('Cache restored successfully')
111116
} finally {
112117
// Try to delete the archive to save space
113118
try {
@@ -162,6 +167,9 @@ export async function saveCache(
162167
core.debug(`Archive Path: ${archivePath}`)
163168

164169
await createTar(archiveFolder, cachePaths, compressionMethod)
170+
if (core.isDebug()) {
171+
await listTar(archivePath, compressionMethod)
172+
}
165173

166174
const fileSizeLimit = 5 * 1024 * 1024 * 1024 // 5GB per repo limit
167175
const archiveFileSize = utils.getArchiveFileSizeIsBytes(archivePath)

packages/cache/src/internal/cacheHttpClient.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,10 @@ export async function saveCache(
301301
// Commit Cache
302302
core.debug('Commiting cache')
303303
const cacheSize = utils.getArchiveFileSizeIsBytes(archivePath)
304+
core.info(
305+
`Cache Size: ~${Math.round(cacheSize / (1024 * 1024))} MB (${cacheSize} B)`
306+
)
307+
304308
const commitCacheResponse = await commitCache(httpClient, cacheId, cacheSize)
305309
if (!isSuccessStatusCode(commitCacheResponse.statusCode)) {
306310
throw new Error(

packages/cache/src/internal/tar.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,30 @@ export async function createTar(
113113
]
114114
await execTar(args, compressionMethod, archiveFolder)
115115
}
116+
117+
export async function listTar(
118+
archivePath: string,
119+
compressionMethod: CompressionMethod
120+
): Promise<void> {
121+
// --d: Decompress.
122+
// --long=#: Enables long distance matching with # bits.
123+
// Maximum is 30 (1GB) on 32-bit OS and 31 (2GB) on 64-bit.
124+
// Using 30 here because we also support 32-bit self-hosted runners.
125+
function getCompressionProgram(): string[] {
126+
switch (compressionMethod) {
127+
case CompressionMethod.Zstd:
128+
return ['--use-compress-program', 'zstd -d --long=30']
129+
case CompressionMethod.ZstdWithoutLong:
130+
return ['--use-compress-program', 'zstd -d']
131+
default:
132+
return ['-z']
133+
}
134+
}
135+
const args = [
136+
...getCompressionProgram(),
137+
'-tf',
138+
archivePath.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
139+
'-P'
140+
]
141+
await execTar(args, compressionMethod)
142+
}

0 commit comments

Comments
 (0)