1
0
Fork 0

Allow changing default cache file size limit

Fixes #1932

Signed-off-by: Voplica <admin@voplica.com>
pull/1933/head
Voplica 2025-01-17 01:17:52 +00:00
parent 1f7c2c79e0
commit d044035b4f
3 changed files with 62 additions and 6 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@ packages/*/__tests__/_temp/
.DS_Store .DS_Store
*.xar *.xar
packages/*/audit.json packages/*/audit.json
.idea

View File

@ -1,10 +1,10 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import * as path from 'path' import * as path from 'path'
import {saveCache} from '../src/cache' import {saveCache, setFileSizeLimit} from '../src/cache'
import * as cacheHttpClient from '../src/internal/cacheHttpClient' import * as cacheHttpClient from '../src/internal/cacheHttpClient'
import * as cacheUtils from '../src/internal/cacheUtils' import * as cacheUtils from '../src/internal/cacheUtils'
import * as config from '../src/internal/config' import * as config from '../src/internal/config'
import {CacheFilename, CompressionMethod} from '../src/internal/constants' import {CacheFilename, CacheFileSizeLimit, CompressionMethod} from '../src/internal/constants'
import * as tar from '../src/internal/tar' import * as tar from '../src/internal/tar'
import {TypedResponse} from '@actions/http-client/lib/interfaces' import {TypedResponse} from '@actions/http-client/lib/interfaces'
import { import {
@ -19,6 +19,7 @@ jest.mock('../src/internal/config')
jest.mock('../src/internal/tar') jest.mock('../src/internal/tar')
beforeAll(() => { beforeAll(() => {
setFileSizeLimit(CacheFileSizeLimit)
jest.spyOn(console, 'log').mockImplementation(() => {}) jest.spyOn(console, 'log').mockImplementation(() => {})
jest.spyOn(core, 'debug').mockImplementation(() => {}) jest.spyOn(core, 'debug').mockImplementation(() => {})
jest.spyOn(core, 'info').mockImplementation(() => {}) jest.spyOn(core, 'info').mockImplementation(() => {})
@ -79,6 +80,42 @@ test('save with large cache outputs should fail', async () => {
expect(getCompressionMock).toHaveBeenCalledTimes(1) expect(getCompressionMock).toHaveBeenCalledTimes(1)
}) })
test('save with small cache outputs should fail on changed limit', async () => {
setFileSizeLimit(100 * 1024 * 1024) // set default limit to 100 MB
const filePath = 'node_modules'
const primaryKey = 'Linux-node-bb828da54c148048dd17899ba9fda624811cfb43'
const cachePaths = [path.resolve(filePath)]
const createTarMock = jest.spyOn(tar, 'createTar')
const logWarningMock = jest.spyOn(core, 'warning')
const cacheSize = 1024 * 1024 * 1024 //1GB, over the 100MB limit
jest
.spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
.mockReturnValueOnce(cacheSize)
const compression = CompressionMethod.Gzip
const getCompressionMock = jest
.spyOn(cacheUtils, 'getCompressionMethod')
.mockReturnValueOnce(Promise.resolve(compression))
const cacheId = await saveCache([filePath], primaryKey)
expect(cacheId).toBe(-1)
expect(logWarningMock).toHaveBeenCalledTimes(1)
expect(logWarningMock).toHaveBeenCalledWith(
'Failed to save: Cache size of ~1024 MB (1073741824 B) is over the 100MB limit, not saving cache.'
)
const archiveFolder = '/foo/bar'
expect(createTarMock).toHaveBeenCalledTimes(1)
expect(createTarMock).toHaveBeenCalledWith(
archiveFolder,
cachePaths,
compression
)
expect(getCompressionMock).toHaveBeenCalledTimes(1)
})
test('save with large cache outputs should fail in GHES with error message', async () => { test('save with large cache outputs should fail in GHES with error message', async () => {
const filePath = 'node_modules' const filePath = 'node_modules'
const primaryKey = 'Linux-node-bb828da54c148048dd17899ba9fda624811cfb43' const primaryKey = 'Linux-node-bb828da54c148048dd17899ba9fda624811cfb43'

View File

@ -51,6 +51,24 @@ function checkKey(key: string): void {
} }
} }
let fileSizeLimit = CacheFileSizeLimit // default 10GB per repo limit
let fileSizeLimitStr = formatBytes(fileSizeLimit)
export function setFileSizeLimit(newFileSizeLimit: number){
fileSizeLimit = newFileSizeLimit
fileSizeLimitStr = formatBytes(newFileSizeLimit)
}
export function formatBytes(bytes: number): string {
if (bytes === 0) return "0 Bytes";
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB"];
const i = Math.floor(Math.log(bytes) / Math.log(1024));
const value = bytes / Math.pow(1024, i);
return `${value.toFixed(0)}${sizes[i]}`;
}
/** /**
* isFeatureAvailable to check the presence of Actions cache service * isFeatureAvailable to check the presence of Actions cache service
* *
@ -385,7 +403,7 @@ async function saveCacheV1(
if (core.isDebug()) { if (core.isDebug()) {
await listTar(archivePath, compressionMethod) await listTar(archivePath, compressionMethod)
} }
const fileSizeLimit = 10 * 1024 * 1024 * 1024 // 10GB per repo limit
const archiveFileSize = utils.getArchiveFileSizeInBytes(archivePath) const archiveFileSize = utils.getArchiveFileSizeInBytes(archivePath)
core.debug(`File Size: ${archiveFileSize}`) core.debug(`File Size: ${archiveFileSize}`)
@ -394,7 +412,7 @@ async function saveCacheV1(
throw new Error( throw new Error(
`Cache size of ~${Math.round( `Cache size of ~${Math.round(
archiveFileSize / (1024 * 1024) archiveFileSize / (1024 * 1024)
)} MB (${archiveFileSize} B) is over the 10GB limit, not saving cache.` )} MB (${archiveFileSize} B) is over the ${fileSizeLimitStr} limit, not saving cache.`
) )
} }
@ -503,11 +521,11 @@ async function saveCacheV2(
core.debug(`File Size: ${archiveFileSize}`) core.debug(`File Size: ${archiveFileSize}`)
// For GHES, this check will take place in ReserveCache API with enterprise file size limit // For GHES, this check will take place in ReserveCache API with enterprise file size limit
if (archiveFileSize > CacheFileSizeLimit && !isGhes()) { if (archiveFileSize > fileSizeLimit && !isGhes()) {
throw new Error( throw new Error(
`Cache size of ~${Math.round( `Cache size of ~${Math.round(
archiveFileSize / (1024 * 1024) archiveFileSize / (1024 * 1024)
)} MB (${archiveFileSize} B) is over the 10GB limit, not saving cache.` )} MB (${archiveFileSize} B) is over the ${fileSizeLimitStr} limit, not saving cache.`
) )
} }