Simplify setting caches to disabled or read-only

This commit is contained in:
Daz DeBoer 2021-09-12 14:26:38 -06:00
parent 1c72a31463
commit 3390540145
No known key found for this signature in database
GPG key ID: DD6B9F0B06683D5D
6 changed files with 24 additions and 39 deletions

View file

@ -3,27 +3,12 @@ import * as cache from '@actions/cache'
import * as github from '@actions/github'
import * as crypto from 'crypto'
export function isCacheReadEnabled(cacheName: string): boolean {
const configValue = getCacheEnabledValue(cacheName)
return configValue === 'true' || configValue === 'read-only'
export function isCacheDisabled(): boolean {
return core.getBooleanInput('cache-disabled')
}
export function isCacheSaveEnabled(cacheName: string): boolean {
const configValue = getCacheEnabledValue(cacheName)
return configValue === 'true'
}
function getCacheEnabledValue(cacheName: string): string {
const configValue = core
.getInput(`${cacheName}-cache-enabled`)
.toLowerCase()
if (['true', 'false', 'read-only'].includes(configValue)) {
return configValue
}
throw new Error(
`Invalid cache-enabled parameter '${configValue}'. Valid values are ['true', 'false', 'read-only']`
)
export function isCacheReadOnly(): boolean {
return core.getBooleanInput('cache-read-only')
}
export function isCacheDebuggingEnabled(): boolean {

View file

@ -1,12 +1,12 @@
import {GradleUserHomeCache} from './cache-gradle-user-home'
import {ProjectDotGradleCache} from './cache-project-dot-gradle'
import * as core from '@actions/core'
import {isCacheReadEnabled, isCacheSaveEnabled} from './cache-utils'
import {isCacheDisabled, isCacheReadOnly} from './cache-utils'
const BUILD_ROOT_DIR = 'BUILD_ROOT_DIR'
export async function restore(buildRootDirectory: string): Promise<void> {
if (!isCacheReadEnabled('gradle')) {
if (isCacheDisabled()) {
core.debug('Cache read disabled')
return
}
@ -21,8 +21,8 @@ export async function restore(buildRootDirectory: string): Promise<void> {
}
export async function save(): Promise<void> {
if (!isCacheSaveEnabled('gradle')) {
core.debug('Cache save disabled')
if (isCacheReadOnly()) {
core.debug('Cache is read-only: not saving cache entry')
return
}

View file

@ -7,7 +7,7 @@ import * as cache from '@actions/cache'
import * as toolCache from '@actions/tool-cache'
import * as gradlew from './gradlew'
import {isCacheReadEnabled, isCacheSaveEnabled} from './cache-utils'
import {isCacheDisabled, isCacheReadOnly} from './cache-utils'
const gradleVersionsBaseUrl = 'https://services.gradle.org/versions'
@ -120,7 +120,7 @@ async function downloadAndCacheGradleDistribution(
`gradle-installations/downloads/gradle-${versionInfo.version}-bin.zip`
)
if (!isCacheReadEnabled('distributions')) {
if (isCacheDisabled()) {
await downloadGradleDistribution(versionInfo, downloadPath)
return downloadPath
}
@ -138,7 +138,7 @@ async function downloadAndCacheGradleDistribution(
)
await downloadGradleDistribution(versionInfo, downloadPath)
if (isCacheSaveEnabled('distributions')) {
if (!isCacheReadOnly()) {
try {
await cache.saveCache([downloadPath], cacheKey)
} catch (error) {