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

@ -34,4 +34,4 @@ jobs:
with: with:
build-root-directory: __tests__/samples/no-wrapper build-root-directory: __tests__/samples/no-wrapper
arguments: help arguments: help
gradle-cache-enabled: no cache-disabled: yes

View file

@ -51,21 +51,21 @@ jobs:
gradle-version: 6.9 gradle-version: 6.9
build-root-directory: __tests__/samples/no-wrapper build-root-directory: __tests__/samples/no-wrapper
arguments: help -DgradleVersionCheck=6.9 arguments: help -DgradleVersionCheck=6.9
gradle-cache-enabled: read-only cache-read-only: true
- name: Test use Gradle version alias - name: Test use Gradle version alias
uses: ./ uses: ./
with: with:
gradle-version: release-candidate gradle-version: release-candidate
build-root-directory: __tests__/samples/no-wrapper build-root-directory: __tests__/samples/no-wrapper
arguments: help -DgradleVersionCheck=7.2 arguments: help -DgradleVersionCheck=7.2
gradle-cache-enabled: read-only cache-read-only: true
- name: Test use defined Gradle executable - name: Test use defined Gradle executable
uses: ./ uses: ./
with: with:
gradle-executable: __tests__/samples/basic/gradlew${{ matrix.script-suffix }} gradle-executable: __tests__/samples/basic/gradlew${{ matrix.script-suffix }}
build-root-directory: __tests__/samples/no-wrapper build-root-directory: __tests__/samples/no-wrapper
arguments: help -DgradleVersionCheck=7.1.1 arguments: help -DgradleVersionCheck=7.1.1
gradle-cache-enabled: read-only cache-read-only: true
# Test that the gradle-user-home cache will cache dependencies, by running build with --offline # Test that the gradle-user-home cache will cache dependencies, by running build with --offline
dependencies-cache: dependencies-cache:
@ -82,7 +82,7 @@ jobs:
with: with:
build-root-directory: __tests__/samples/basic build-root-directory: __tests__/samples/basic
arguments: test --offline arguments: test --offline
gradle-cache-enabled: read-only cache-read-only: true
# Test that the gradle-user-home cache will cache and restore local build-cache # Test that the gradle-user-home cache will cache and restore local build-cache
build-cache: build-cache:
@ -99,7 +99,7 @@ jobs:
with: with:
build-root-directory: __tests__/samples/basic build-root-directory: __tests__/samples/basic
arguments: test -DverifyCachedBuild=true arguments: test -DverifyCachedBuild=true
gradle-cache-enabled: read-only cache-read-only: true
# Test that the project-dot-gradle cache will cache and restore configuration-cache # Test that the project-dot-gradle cache will cache and restore configuration-cache
configuration-cache: configuration-cache:
@ -118,4 +118,4 @@ jobs:
with: with:
build-root-directory: __tests__/samples/basic build-root-directory: __tests__/samples/basic
arguments: test --configuration-cache arguments: test --configuration-cache
gradle-cache-enabled: read-only cache-read-only: true

View file

@ -16,14 +16,14 @@ inputs:
arguments: arguments:
description: Gradle command line arguments, see gradle --help description: Gradle command line arguments, see gradle --help
required: false required: false
distributions-cache-enabled: cache-disabled:
description: Whether caching downloaded Gradle distributions is enabled or not. Valid values are 'true' (default), 'false', and 'read-only'. description: When 'true', all caching is disabled. No entries will be written to or read from the cache.
required: false required: false
default: true default: false
gradle-cache-enabled: cache-read-only:
description: Whether caching of Gradle User Home and project .gradle directory is enabled. Valid values are 'true' (default), 'false', and 'read-only'. description: When 'true', existing entries will be read from the cache but no entries will be written
required: false required: false
default: true default: false
workflow-job-context: workflow-job-context:
description: Used to uniquely identify the current job invocation. Defaults to the matrix values for this job; this should not be overridden by users. description: Used to uniquely identify the current job invocation. Defaults to the matrix values for this job; this should not be overridden by users.
required: false required: false

View file

@ -3,27 +3,12 @@ import * as cache from '@actions/cache'
import * as github from '@actions/github' import * as github from '@actions/github'
import * as crypto from 'crypto' import * as crypto from 'crypto'
export function isCacheReadEnabled(cacheName: string): boolean { export function isCacheDisabled(): boolean {
const configValue = getCacheEnabledValue(cacheName) return core.getBooleanInput('cache-disabled')
return configValue === 'true' || configValue === 'read-only'
} }
export function isCacheSaveEnabled(cacheName: string): boolean { export function isCacheReadOnly(): boolean {
const configValue = getCacheEnabledValue(cacheName) return core.getBooleanInput('cache-read-only')
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 isCacheDebuggingEnabled(): boolean { export function isCacheDebuggingEnabled(): boolean {

View file

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

View file

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