From 6fca6b3929ddc1c6145651e26d1995b6946b965e Mon Sep 17 00:00:00 2001 From: Daz DeBoer Date: Tue, 24 Aug 2021 12:57:17 -0600 Subject: [PATCH] Add support for read-only caching in v2 --- action.yml | 6 +++--- src/cache-gradle-user-home.ts | 15 ++++++++------- src/cache-project-dot-gradle.ts | 15 ++++++++------- src/cache-utils.ts | 25 +++++++++++++++++++++++++ src/post.ts | 8 -------- src/provision.ts | 19 +++++++++---------- 6 files changed, 53 insertions(+), 35 deletions(-) diff --git a/action.yml b/action.yml index 40758cc..9f14d80 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/src/cache-gradle-user-home.ts b/src/cache-gradle-user-home.ts index 5d15578..b69a5dc 100644 --- a/src/cache-gradle-user-home.ts +++ b/src/cache-gradle-user-home.ts @@ -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 { - 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 { } export async function save(): Promise { - 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 { 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') diff --git a/src/cache-project-dot-gradle.ts b/src/cache-project-dot-gradle.ts index 8777e4f..7aa3197 100644 --- a/src/cache-project-dot-gradle.ts +++ b/src/cache-project-dot-gradle.ts @@ -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 { - if (!isProjectDotGradleCacheEnabled()) return + if (!isCacheReadEnabled(CACHE_NAME)) return if (projectDotGradleDirExists(rootDir)) { core.debug( @@ -48,7 +53,7 @@ export async function restore(rootDir: string): Promise { } export async function save(rootDir: string): Promise { - 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 { 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)) diff --git a/src/cache-utils.ts b/src/cache-utils.ts index 17a61ff..4f78055 100644 --- a/src/cache-utils.ts +++ b/src/cache-utils.ts @@ -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']` + ) +} diff --git a/src/post.ts b/src/post.ts index 2b4c83a..698ddf3 100644 --- a/src/post.ts +++ b/src/post.ts @@ -1,16 +1,8 @@ -import * as core from '@actions/core' - import * as caches from './caches' // Invoked by GitHub Actions export async function run(): Promise { - if (isCacheReadOnly()) return - await caches.save() } -function isCacheReadOnly(): boolean { - return core.getBooleanInput('cache-read-only') -} - run() diff --git a/src/provision.ts b/src/provision.ts index de420d0..a4932ae 100644 --- a/src/provision.ts +++ b/src/provision.ts @@ -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 { - core.info( - `Gradle distribution ${versionInfo.version} not found in cache. Will download.` - ) - await downloadGradleDistribution(versionInfo, downloadPath) + 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 { return response.readBody() } -function isDistributionsCacheDisabled(): boolean { - return !core.getBooleanInput('distributions-cache-enabled') -} - interface GradleVersionInfo { version: string downloadUrl: string