Improve cache service availability determination and change warnings to errors
- Update isFeatureAvailable() to leverage ACTIONS_CACHE_SERVICE_V2 feature flag - For v2: check ACTIONS_RESULTS_URL availability - For v1: check either ACTIONS_CACHE_URL or ACTIONS_RESULTS_URL availability - Change warning logs to error logs for cache failures - Add comprehensive tests covering all scenarios Co-authored-by: Link- <[email protected]>
This commit is contained in:
co-authored by
Link-
parent
be5a2ce677
commit
3c90578c30
+63
-8
@@ -1,14 +1,69 @@
|
|||||||
import * as cache from '../src/cache'
|
import * as cache from '../src/cache'
|
||||||
|
|
||||||
test('isFeatureAvailable returns true if server url is set', () => {
|
describe('isFeatureAvailable', () => {
|
||||||
try {
|
const originalEnv = process.env
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.resetModules()
|
||||||
|
process.env = {...originalEnv}
|
||||||
|
// Clean cache-related environment variables
|
||||||
|
delete process.env['ACTIONS_CACHE_URL']
|
||||||
|
delete process.env['ACTIONS_RESULTS_URL']
|
||||||
|
delete process.env['ACTIONS_CACHE_SERVICE_V2']
|
||||||
|
delete process.env['GITHUB_SERVER_URL']
|
||||||
|
})
|
||||||
|
|
||||||
|
afterAll(() => {
|
||||||
|
process.env = originalEnv
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns true for cache service v1 when ACTIONS_CACHE_URL is set', () => {
|
||||||
process.env['ACTIONS_CACHE_URL'] = 'http://cache.com'
|
process.env['ACTIONS_CACHE_URL'] = 'http://cache.com'
|
||||||
expect(cache.isFeatureAvailable()).toBe(true)
|
expect(cache.isFeatureAvailable()).toBe(true)
|
||||||
} finally {
|
})
|
||||||
delete process.env['ACTIONS_CACHE_URL']
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
test('isFeatureAvailable returns false if server url is not set', () => {
|
test('returns true for cache service v1 when ACTIONS_RESULTS_URL is set', () => {
|
||||||
expect(cache.isFeatureAvailable()).toBe(false)
|
process.env['ACTIONS_RESULTS_URL'] = 'http://results.com'
|
||||||
|
expect(cache.isFeatureAvailable()).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns true for cache service v1 when both URLs are set', () => {
|
||||||
|
process.env['ACTIONS_CACHE_URL'] = 'http://cache.com'
|
||||||
|
process.env['ACTIONS_RESULTS_URL'] = 'http://results.com'
|
||||||
|
expect(cache.isFeatureAvailable()).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns true for cache service v2 when ACTIONS_RESULTS_URL is set', () => {
|
||||||
|
process.env['ACTIONS_CACHE_SERVICE_V2'] = 'true'
|
||||||
|
process.env['ACTIONS_RESULTS_URL'] = 'http://results.com'
|
||||||
|
expect(cache.isFeatureAvailable()).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns false for cache service v2 when only ACTIONS_CACHE_URL is set', () => {
|
||||||
|
process.env['ACTIONS_CACHE_SERVICE_V2'] = 'true'
|
||||||
|
process.env['ACTIONS_CACHE_URL'] = 'http://cache.com'
|
||||||
|
expect(cache.isFeatureAvailable()).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns false when no cache URLs are set', () => {
|
||||||
|
expect(cache.isFeatureAvailable()).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns false for cache service v2 when no URLs are set', () => {
|
||||||
|
process.env['ACTIONS_CACHE_SERVICE_V2'] = 'true'
|
||||||
|
expect(cache.isFeatureAvailable()).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns true for GHES with v1 even when v2 flag is set', () => {
|
||||||
|
process.env['GITHUB_SERVER_URL'] = 'https://my-enterprise.github.com'
|
||||||
|
process.env['ACTIONS_CACHE_SERVICE_V2'] = 'true'
|
||||||
|
process.env['ACTIONS_CACHE_URL'] = 'http://cache.com'
|
||||||
|
expect(cache.isFeatureAvailable()).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns true for GHES with ACTIONS_RESULTS_URL', () => {
|
||||||
|
process.env['GITHUB_SERVER_URL'] = 'https://my-enterprise.github.com'
|
||||||
|
process.env['ACTIONS_RESULTS_URL'] = 'http://results.com'
|
||||||
|
expect(cache.isFeatureAvailable()).toBe(true)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Vendored
+20
-7
@@ -57,7 +57,20 @@ function checkKey(key: string): void {
|
|||||||
* @returns boolean return true if Actions cache service feature is available, otherwise false
|
* @returns boolean return true if Actions cache service feature is available, otherwise false
|
||||||
*/
|
*/
|
||||||
export function isFeatureAvailable(): boolean {
|
export function isFeatureAvailable(): boolean {
|
||||||
return !!process.env['ACTIONS_CACHE_URL']
|
const cacheServiceVersion = getCacheServiceVersion()
|
||||||
|
|
||||||
|
// Check availability based on cache service version
|
||||||
|
switch (cacheServiceVersion) {
|
||||||
|
case 'v2':
|
||||||
|
// For v2, we need ACTIONS_RESULTS_URL
|
||||||
|
return !!process.env['ACTIONS_RESULTS_URL']
|
||||||
|
case 'v1':
|
||||||
|
default:
|
||||||
|
// For v1, we need either ACTIONS_CACHE_URL or ACTIONS_RESULTS_URL
|
||||||
|
return !!(
|
||||||
|
process.env['ACTIONS_CACHE_URL'] || process.env['ACTIONS_RESULTS_URL']
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -186,8 +199,8 @@ async function restoreCacheV1(
|
|||||||
if (typedError.name === ValidationError.name) {
|
if (typedError.name === ValidationError.name) {
|
||||||
throw error
|
throw error
|
||||||
} else {
|
} else {
|
||||||
// Supress all non-validation cache related errors because caching should be optional
|
// Log cache related errors
|
||||||
core.warning(`Failed to restore: ${(error as Error).message}`)
|
core.error(`Failed to restore: ${(error as Error).message}`)
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
// Try to delete the archive to save space
|
// Try to delete the archive to save space
|
||||||
@@ -304,8 +317,8 @@ async function restoreCacheV2(
|
|||||||
if (typedError.name === ValidationError.name) {
|
if (typedError.name === ValidationError.name) {
|
||||||
throw error
|
throw error
|
||||||
} else {
|
} else {
|
||||||
// Supress all non-validation cache related errors because caching should be optional
|
// Log cache related errors
|
||||||
core.warning(`Failed to restore: ${(error as Error).message}`)
|
core.error(`Failed to restore: ${(error as Error).message}`)
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
@@ -437,7 +450,7 @@ async function saveCacheV1(
|
|||||||
} else if (typedError.name === ReserveCacheError.name) {
|
} else if (typedError.name === ReserveCacheError.name) {
|
||||||
core.info(`Failed to save: ${typedError.message}`)
|
core.info(`Failed to save: ${typedError.message}`)
|
||||||
} else {
|
} else {
|
||||||
core.warning(`Failed to save: ${typedError.message}`)
|
core.error(`Failed to save: ${typedError.message}`)
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
// Try to delete the archive to save space
|
// Try to delete the archive to save space
|
||||||
@@ -576,7 +589,7 @@ async function saveCacheV2(
|
|||||||
} else if (typedError.name === ReserveCacheError.name) {
|
} else if (typedError.name === ReserveCacheError.name) {
|
||||||
core.info(`Failed to save: ${typedError.message}`)
|
core.info(`Failed to save: ${typedError.message}`)
|
||||||
} else {
|
} else {
|
||||||
core.warning(`Failed to save: ${typedError.message}`)
|
core.error(`Failed to save: ${typedError.message}`)
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
// Try to delete the archive to save space
|
// Try to delete the archive to save space
|
||||||
|
|||||||
Reference in New Issue
Block a user