From 4d37378696cba14154aaff2f7eeac3bc69895f29 Mon Sep 17 00:00:00 2001 From: Daz DeBoer Date: Sun, 5 Sep 2021 19:55:49 -0600 Subject: [PATCH] Consolidate cache-enabled options --- .github/workflows/prod.yml | 20 +++++++------------- action.yml | 8 ++------ src/cache-gradle-user-home.ts | 13 ++----------- src/cache-project-dot-gradle.ts | 13 ++----------- src/caches.ts | 21 +++++++++++++-------- 5 files changed, 26 insertions(+), 49 deletions(-) diff --git a/.github/workflows/prod.yml b/.github/workflows/prod.yml index 450eaa5..a956b3a 100644 --- a/.github/workflows/prod.yml +++ b/.github/workflows/prod.yml @@ -51,24 +51,21 @@ jobs: gradle-version: 6.9 build-root-directory: __tests__/samples/no-wrapper arguments: help -DgradleVersionCheck=6.9 - gradle-user-home-cache-enabled: read-only - project-dot-gradle-cache-enabled: read-only + gradle-cache-enabled: read-only - name: Test use Gradle version alias uses: ./ with: gradle-version: release-candidate build-root-directory: __tests__/samples/no-wrapper arguments: help -DgradleVersionCheck=7.2 - gradle-user-home-cache-enabled: read-only - project-dot-gradle-cache-enabled: read-only + gradle-cache-enabled: read-only - name: Test use defined Gradle executable uses: ./ with: gradle-executable: __tests__/samples/basic/gradlew${{ matrix.script-suffix }} build-root-directory: __tests__/samples/no-wrapper arguments: help -DgradleVersionCheck=7.1.1 - gradle-user-home-cache-enabled: read-only - project-dot-gradle-cache-enabled: read-only + gradle-cache-enabled: read-only # Test that the gradle-user-home cache will cache dependencies, by running build with --offline dependencies-cache: @@ -85,8 +82,7 @@ jobs: with: build-root-directory: __tests__/samples/basic arguments: test --offline - gradle-user-home-cache-enabled: read-only - project-dot-gradle-cache-enabled: read-only + gradle-cache-enabled: read-only # Test that the gradle-user-home cache will cache and restore local build-cache build-cache: @@ -103,8 +99,7 @@ jobs: with: build-root-directory: __tests__/samples/basic arguments: test -DverifyCachedBuild=true - gradle-user-home-cache-enabled: read-only - project-dot-gradle-cache-enabled: read-only + gradle-cache-enabled: read-only # Test that the project-dot-gradle cache will cache and restore configuration-cache configuration-cache: @@ -123,8 +118,7 @@ jobs: with: build-root-directory: __tests__/samples/basic arguments: test --configuration-cache - gradle-user-home-cache-enabled: read-only - project-dot-gradle-cache-enabled: read-only + gradle-cache-enabled: read-only # These build invocations are informational only, and are expected to fail failures: @@ -145,4 +139,4 @@ jobs: with: build-root-directory: __tests__/samples/no-wrapper arguments: help - gradle-user-home-cache-enabled: no + gradle-cache-enabled: no diff --git a/action.yml b/action.yml index 9f14d80..10a44a8 100644 --- a/action.yml +++ b/action.yml @@ -20,12 +20,8 @@ inputs: 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. 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. Valid values are 'true' (default), 'false', and 'read-only'. + gradle-cache-enabled: + description: Whether caching of Gradle User Home and project .gradle directory 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 86d8fb0..c4bf854 100644 --- a/src/cache-gradle-user-home.ts +++ b/src/cache-gradle-user-home.ts @@ -4,13 +4,8 @@ import os from 'os' import * as core from '@actions/core' import * as cache from '@actions/cache' -import { - generateCacheKey, - isCacheReadEnabled, - isCacheSaveEnabled -} from './cache-utils' +import {generateCacheKey} 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 @@ -20,10 +15,8 @@ const CACHE_KEY = 'GUH_CACHE_KEY' const CACHE_RESULT = 'GUH_CACHE_RESULT' export async function restore(): Promise { - if (!isCacheReadEnabled(CACHE_NAME)) return - if (gradleUserHomeExists()) { - core.debug('Gradle User Home already exists. Not restoring from cache.') + core.info('Gradle User Home already exists. Not restoring from cache.') return } @@ -49,8 +42,6 @@ export async function restore(): Promise { } export async function save(): Promise { - if (!isCacheSaveEnabled(CACHE_NAME)) return - if (!gradleUserHomeExists()) { core.debug('No Gradle User Home to cache.') return diff --git a/src/cache-project-dot-gradle.ts b/src/cache-project-dot-gradle.ts index e05dc79..2efcda1 100644 --- a/src/cache-project-dot-gradle.ts +++ b/src/cache-project-dot-gradle.ts @@ -3,13 +3,8 @@ import fs from 'fs' import * as core from '@actions/core' import * as cache from '@actions/cache' -import { - generateCacheKey, - isCacheReadEnabled, - isCacheSaveEnabled -} from './cache-utils' +import {generateCacheKey} from './cache-utils' -const CACHE_NAME = 'project-dot-gradle' const PATHS_TO_CACHE = [ 'configuration-cache' // Only configuration-cache is stored at present ] @@ -17,10 +12,8 @@ const CACHE_KEY = 'PROJECT_CACHE_KEY' const CACHE_RESULT = 'PROJECT_CACHE_RESULT' export async function restore(rootDir: string): Promise { - if (!isCacheReadEnabled(CACHE_NAME)) return - if (projectDotGradleDirExists(rootDir)) { - core.debug( + core.info( 'Project .gradle directory already exists. Not restoring from cache.' ) return @@ -46,8 +39,6 @@ export async function restore(rootDir: string): Promise { } export async function save(rootDir: string): Promise { - if (!isCacheSaveEnabled(CACHE_NAME)) return - if (!projectDotGradleDirExists(rootDir)) { core.debug('No project .gradle dir to cache.') return diff --git a/src/caches.ts b/src/caches.ts index 4b33a5e..bc8dc64 100644 --- a/src/caches.ts +++ b/src/caches.ts @@ -1,26 +1,31 @@ import * as cacheGradleUserHome from './cache-gradle-user-home' import * as cacheProjectDotGradle from './cache-project-dot-gradle' import * as core from '@actions/core' +import {isCacheReadEnabled, isCacheSaveEnabled} from './cache-utils' const BUILD_ROOT_DIR = 'BUILD_ROOT_DIR' export async function restore(buildRootDirectory: string): Promise { - core.startGroup('Restore Gradle User Home from cache') - await cacheGradleUserHome.restore() - core.endGroup() + if (!isCacheReadEnabled('gradle')) { + core.debug('Cache read disabled') + return + } - core.startGroup('Restore project .gradle directory from cache') + core.startGroup('Restore Gradle state from cache') + await cacheGradleUserHome.restore() core.saveState(BUILD_ROOT_DIR, buildRootDirectory) await cacheProjectDotGradle.restore(buildRootDirectory) core.endGroup() } export async function save(): Promise { - core.startGroup('Cache Gradle User Home') - await cacheGradleUserHome.save() - core.endGroup() + if (!isCacheSaveEnabled('gradle')) { + core.debug('Cache save disabled') + return + } - core.startGroup('Cache project .gradle directory') + core.startGroup('Caching Gradle state') + await cacheGradleUserHome.save() const buildRootDirectory = core.getState(BUILD_ROOT_DIR) await cacheProjectDotGradle.save(buildRootDirectory) core.endGroup()