Add support for read-only caching in v2

This commit is contained in:
Daz DeBoer 2021-08-24 12:57:17 -06:00
parent d9cc0aeccf
commit 6fca6b3929
No known key found for this signature in database
GPG Key ID: DD6B9F0B06683D5D
6 changed files with 53 additions and 35 deletions

View File

@ -17,15 +17,15 @@ inputs:
description: Gradle command line arguments, see gradle --help
required: false
distributions-cache-enabled:
description: Whether caching downloaded Gradle distributions is enabled or not, default to 'true'
description: Whether caching downloaded Gradle distributions is enabled or not. Valid values are 'true' (default), 'false', and 'read-only'.
required: false
default: true
gradle-user-home-cache-enabled:
description: Whether caching of state in Gradle User Home is enabled, default to 'true'
description: Whether caching of state in Gradle User Home is enabled. Valid values are 'true' (default), 'false', and 'read-only'.
required: false
default: true
project-dot-gradle-cache-enabled:
description: Whether caching of state in project .gradle dir is enabled, default to 'false'
description: Whether caching of state in project .gradle dir is enabled. Valid values are 'true' (default), 'false', and 'read-only'.
required: false
default: true

View File

@ -5,8 +5,13 @@ import os from 'os'
import * as core from '@actions/core'
import * as cache from '@actions/cache'
import * as github from '@actions/github'
import {truncateArgs} from './cache-utils'
import {
isCacheReadEnabled,
isCacheSaveEnabled,
truncateArgs
} from './cache-utils'
const CACHE_NAME = 'gradle-user-home'
const CACHE_PATH = [
'~/.gradle/caches/*', // All directories in 'caches'
'~/.gradle/notifications/*', // Prevent the re-rendering of first-use message for version
@ -16,7 +21,7 @@ const CACHE_KEY = 'GUH_CACHE_KEY'
const CACHE_RESULT = 'GUH_CACHE_RESULT'
export async function restore(): Promise<void> {
if (!isGradleUserHomeCacheEnabled()) return
if (!isCacheReadEnabled(CACHE_NAME)) return
if (gradleUserHomeExists()) {
core.debug('Gradle User Home already exists. Not restoring from cache.')
@ -50,7 +55,7 @@ export async function restore(): Promise<void> {
}
export async function save(): Promise<void> {
if (!isGradleUserHomeCacheEnabled()) return
if (!isCacheSaveEnabled(CACHE_NAME)) return
if (!gradleUserHomeExists()) {
core.debug('No Gradle User Home to cache.')
@ -90,10 +95,6 @@ export async function save(): Promise<void> {
return
}
export function isGradleUserHomeCacheEnabled(): boolean {
return core.getBooleanInput('gradle-user-home-cache-enabled')
}
function gradleUserHomeExists(): boolean {
// Need to check for 'caches' directory to avoid incorrect detection on MacOS agents
const dir = path.resolve(os.homedir(), '.gradle/caches')

View File

@ -4,8 +4,13 @@ import fs from 'fs'
import * as core from '@actions/core'
import * as cache from '@actions/cache'
import * as github from '@actions/github'
import {truncateArgs} from './cache-utils'
import {
isCacheReadEnabled,
isCacheSaveEnabled,
truncateArgs
} from './cache-utils'
const CACHE_NAME = 'project-dot-gradle'
const PATHS_TO_CACHE = [
'configuration-cache' // Only configuration-cache is stored at present
]
@ -13,7 +18,7 @@ const CACHE_KEY = 'PROJECT_CACHE_KEY'
const CACHE_RESULT = 'PROJECT_CACHE_RESULT'
export async function restore(rootDir: string): Promise<void> {
if (!isProjectDotGradleCacheEnabled()) return
if (!isCacheReadEnabled(CACHE_NAME)) return
if (projectDotGradleDirExists(rootDir)) {
core.debug(
@ -48,7 +53,7 @@ export async function restore(rootDir: string): Promise<void> {
}
export async function save(rootDir: string): Promise<void> {
if (!isProjectDotGradleCacheEnabled()) return
if (!isCacheSaveEnabled(CACHE_NAME)) return
if (!projectDotGradleDirExists(rootDir)) {
core.debug('No project .gradle dir to cache.')
@ -88,10 +93,6 @@ export async function save(rootDir: string): Promise<void> {
return
}
function isProjectDotGradleCacheEnabled(): boolean {
return core.getBooleanInput('project-dot-gradle-cache-enabled')
}
function getCachePath(rootDir: string): string[] {
const dir = getProjectDotGradleDir(rootDir)
return PATHS_TO_CACHE.map(x => path.resolve(dir, x))

View File

@ -1,3 +1,28 @@
import * as core from '@actions/core'
export function truncateArgs(args: string): string {
return args.trim().replace(/\s+/g, ' ').substr(0, 400)
}
export function isCacheReadEnabled(cacheName: string): boolean {
const configValue = getCacheEnabledValue(cacheName)
return configValue === 'true' || configValue === 'read-only'
}
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']`
)
}

View File

@ -1,16 +1,8 @@
import * as core from '@actions/core'
import * as caches from './caches'
// Invoked by GitHub Actions
export async function run(): Promise<void> {
if (isCacheReadOnly()) return
await caches.save()
}
function isCacheReadOnly(): boolean {
return core.getBooleanInput('cache-read-only')
}
run()

View File

@ -7,6 +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'
const gradleVersionsBaseUrl = 'https://services.gradle.org/versions'
@ -119,7 +120,7 @@ async function downloadAndCacheGradleDistribution(
`gradle-installations/downloads/gradle-${versionInfo.version}-bin.zip`
)
if (isDistributionsCacheDisabled()) {
if (!isCacheReadEnabled('distributions')) {
await downloadGradleDistribution(versionInfo, downloadPath)
return downloadPath
}
@ -130,12 +131,14 @@ async function downloadAndCacheGradleDistribution(
core.info(
`Restored Gradle distribution ${cacheKey} from cache to ${downloadPath}`
)
} else {
return downloadPath
}
core.info(
`Gradle distribution ${versionInfo.version} not found in cache. Will download.`
)
await downloadGradleDistribution(versionInfo, downloadPath)
if (isCacheSaveEnabled('distributions')) {
try {
await cache.saveCache([downloadPath], cacheKey)
} catch (error) {
@ -183,10 +186,6 @@ async function httpGetString(url: string): Promise<string> {
return response.readBody()
}
function isDistributionsCacheDisabled(): boolean {
return !core.getBooleanInput('distributions-cache-enabled')
}
interface GradleVersionInfo {
version: string
downloadUrl: string