From 12fc52a49ad5494b24af89e3bb6cea336e50b401 Mon Sep 17 00:00:00 2001 From: Daz DeBoer Date: Wed, 29 Dec 2021 13:36:24 -0700 Subject: [PATCH] Combine java-toolchain entry definitions - Remove configuration parameter for cache-entry-definitions. This allows richer modelling of extracted entries. - Include both *.zip and *.tar.gz in entry definition for java-toolchains. --- .../workflows/integTest-caching-config.yml | 24 ++------ .../integTest-caching-configuration-cache.yml | 3 +- .../integTest-caching-gradle-home.yml | 3 +- action.yml | 13 ----- src/cache-gradle-user-home.ts | 56 +++++++++++++++---- 5 files changed, 52 insertions(+), 47 deletions(-) diff --git a/.github/workflows/integTest-caching-config.yml b/.github/workflows/integTest-caching-config.yml index c38b629..61c7cff 100644 --- a/.github/workflows/integTest-caching-config.yml +++ b/.github/workflows/integTest-caching-config.yml @@ -22,21 +22,13 @@ jobs: - name: Setup Gradle uses: ./ with: - # Add "wrapper" to main cache entry and remove 'wrapper-zips' cache entry - # Exclude build-cache from main cache entry + # Add "enterprise" to main cache entry but omit "notifications" gradle-home-cache-includes: | caches - notifications - wrapper + enterprise + # Exclude build-cache from main cache entry gradle-home-cache-excludes: | caches/build-cache-1 - gradle-home-extracted-cache-entries: | - [ - ["generated-gradle-jars", "caches/*/generated-gradle-jars/*.jar"], - ["dependencies", "caches/modules-*/files-*/*/*/*/*/"], - ["instrumented-jars", "caches/jars-*/*/"], - ["kotlin-dsl", "caches/*/kotlin-dsl/*/*/"] - ] - name: Build using Gradle wrapper working-directory: __tests__/samples/groovy-dsl run: ./gradlew test @@ -57,17 +49,9 @@ jobs: # Use the same configuration when restoring state from cache gradle-home-cache-includes: | caches - notifications - wrapper + enterprise gradle-home-cache-excludes: | caches/build-cache-1 - gradle-home-extracted-cache-entries: | - [ - ["generated-gradle-jars", "caches/*/generated-gradle-jars/*.jar"], - ["dependencies", "caches/modules-*/files-*/*/*/*/*/"], - ["instrumented-jars", "caches/jars-*/*/"], - ["kotlin-dsl", "caches/*/kotlin-dsl/*/*/"] - ] cache-read-only: true - name: Execute Gradle build with --offline working-directory: __tests__/samples/groovy-dsl diff --git a/.github/workflows/integTest-caching-configuration-cache.yml b/.github/workflows/integTest-caching-configuration-cache.yml index a88768d..fd4ac6d 100644 --- a/.github/workflows/integTest-caching-configuration-cache.yml +++ b/.github/workflows/integTest-caching-configuration-cache.yml @@ -81,9 +81,10 @@ jobs: uses: actions/checkout@v2 - name: Setup Gradle with no cache extracted cache entries restored uses: ./ + env: + GRADLE_BUILD_ACTION_NO_EXTRACTED_ENTRIES: true with: cache-read-only: true - gradle-home-extracted-cache-entries: '[]' - name: Check execute Gradle build with configuration cache enabled (but not restored) working-directory: __tests__/samples/groovy-dsl run: ./gradlew test --configuration-cache diff --git a/.github/workflows/integTest-caching-gradle-home.yml b/.github/workflows/integTest-caching-gradle-home.yml index 86ebd57..f606afd 100644 --- a/.github/workflows/integTest-caching-gradle-home.yml +++ b/.github/workflows/integTest-caching-gradle-home.yml @@ -72,9 +72,10 @@ jobs: uses: actions/checkout@v2 - name: Setup Gradle with no extracted cache entries restored uses: ./ + env: + GRADLE_BUILD_ACTION_NO_EXTRACTED_ENTRIES: true with: cache-read-only: true - gradle-home-extracted-cache-entries: '[]' - name: Check executee Gradle build working-directory: __tests__/samples/groovy-dsl run: ./gradlew test diff --git a/action.yml b/action.yml index 6596af2..e7e0537 100644 --- a/action.yml +++ b/action.yml @@ -55,19 +55,6 @@ inputs: description: Used to uniquely identify the current job invocation. Defaults to the matrix values for this job; this should not be overridden by users (INTERNAL). required: false default: ${{ toJSON(matrix) }} - gradle-home-extracted-cache-entries: - description: Names and patterns of artifacts in Gradle User Home to cache separately. (EXPERIMENTAL - may be changed/removed without notice) - required: false - default: | - [ - ["generated-gradle-jars", "caches/*/generated-gradle-jars/*.jar"], - ["wrapper-zips", "wrapper/dists/*/*/*.zip"], - ["java-toolchains-win", "jdks/*.zip"], - ["java-toolchains-nix", "jdks/*.tar.gz"], - ["dependencies", "caches/modules-*/files-*/*/*/*/*"], - ["instrumented-jars", "caches/jars-*/*"], - ["kotlin-dsl", "caches/*/kotlin-dsl/*/*"] - ] outputs: build-scan-url: diff --git a/src/cache-gradle-user-home.ts b/src/cache-gradle-user-home.ts index 42b8991..b5a63b9 100644 --- a/src/cache-gradle-user-home.ts +++ b/src/cache-gradle-user-home.ts @@ -12,7 +12,7 @@ const META_FILE = 'cache-metadata.json' const INCLUDE_PATHS_PARAMETER = 'gradle-home-cache-includes' const EXCLUDE_PATHS_PARAMETER = 'gradle-home-cache-excludes' -const EXTRACTED_CACHE_ENTRIES_PARAMETER = 'gradle-home-extracted-cache-entries' +const NO_EXTRACTED_ENTRIES_VAR = 'GRADLE_BUILD_ACTION_NO_EXTRACTED_ENTRIES' /** * Represents the result of attempting to load or store an extracted cache entry. @@ -40,6 +40,21 @@ class ExtractedCacheEntryMetadata { entries: ExtractedCacheEntry[] = [] } +/** + * The specification for a type of extracted cache entry. + */ +class ExtractedCacheEntryDefinition { + artifactType: string + pattern: string + bundle: boolean + + constructor(artifactType: string, pattern: string, bundle: boolean) { + this.artifactType = artifactType + this.pattern = pattern + this.bundle = bundle + } +} + /** * Caches and restores the entire Gradle User Home directory, extracting entries containing common artifacts * for more efficient storage. @@ -63,7 +78,6 @@ export class GradleUserHomeCache extends AbstractCache { * Each extracted cache entry is restored in parallel, except when debugging is enabled. */ private async restoreExtractedCacheEntries(listener: CacheListener): Promise { - const extractedCacheEntryDefinitions = this.getExtractedCacheEntryDefinitions() const previouslyExtractedCacheEntries = this.loadExtractedCacheEntries() const processes: Promise[] = [] @@ -73,8 +87,8 @@ export class GradleUserHomeCache extends AbstractCache { const entryListener = listener.entry(cacheEntry.pattern) // Handle case where the extracted-cache-entry definitions have been changed - if (extractedCacheEntryDefinitions.get(artifactType) === undefined) { - core.info(`Found extracted cache entry for ${artifactType} but no such entry defined`) + if (process.env[NO_EXTRACTED_ENTRIES_VAR] === 'true') { + core.info(`Not restoring any extracted cache entries for ${artifactType}`) entryListener.markRequested('EXTRACTED_ENTRY_NOT_DEFINED') } else { processes.push( @@ -137,7 +151,6 @@ export class GradleUserHomeCache extends AbstractCache { /** * Saves any artifacts that are configured to be cached separately, based on the extracted cache entry definitions. - * These definitions are normally fixed, but can be overridden by the `gradle-home-extracted-cache-entries` parameter. * Each entry is extracted and saved in parallel, except when debugging is enabled. */ private async saveExtractedCacheEntries(listener: CacheListener): Promise { @@ -146,7 +159,11 @@ export class GradleUserHomeCache extends AbstractCache { const previouslyRestoredEntries = this.loadExtractedCacheEntries() const cacheActions: Promise[] = [] - for (const [artifactType, pattern] of cacheEntryDefinitions) { + // For each cache entry definition, determine if it has already been restored, and if not, extract it + for (const cacheEntryDefinition of cacheEntryDefinitions) { + const artifactType = cacheEntryDefinition.artifactType + const pattern = cacheEntryDefinition.pattern + // Find all matching files for this cache entry definition const globber = await glob.create(pattern, { implicitDescendants: false, @@ -159,7 +176,7 @@ export class GradleUserHomeCache extends AbstractCache { continue } - if (this.isBundlePattern(pattern)) { + if (cacheEntryDefinition.bundle) { // For an extracted "bundle", use the defined pattern and cache all matching files in a single entry. cacheActions.push( this.saveExtractedCacheEntry( @@ -300,12 +317,27 @@ export class GradleUserHomeCache extends AbstractCache { /** * Return the extracted cache entry definitions, which determine which artifacts will be cached * separately from the rest of the Gradle User Home cache entry. - * This is normally a fixed set, but can be overridden by the `gradle-home-extracted-cache-entries` parameter. */ - private getExtractedCacheEntryDefinitions(): Map { - const rawDefinitions = core.getInput(EXTRACTED_CACHE_ENTRIES_PARAMETER) - const parsedDefinitions = JSON.parse(rawDefinitions) - return new Map(Array.from(parsedDefinitions, ([key, value]) => [key, path.resolve(this.gradleUserHome, value)])) + private getExtractedCacheEntryDefinitions(): ExtractedCacheEntryDefinition[] { + const entryDefinition = ( + artifactType: string, + patterns: string[], + bundle: boolean + ): ExtractedCacheEntryDefinition => { + const resolvedPattern = patterns.map(x => this.resolveCachePath(x)).join('\n') + return new ExtractedCacheEntryDefinition(artifactType, resolvedPattern, bundle) + } + + const definitions = [ + entryDefinition('generated-gradle-jars', ['caches/*/generated-gradle-jars/*.jar'], false), + entryDefinition('wrapper-zips', ['wrapper/dists/*/*/*.zip'], false), + entryDefinition('java-toolchains', ['jdks/*.zip', 'jdks/*.tar.gz'], false), + entryDefinition('dependencies', ['caches/modules-*/files-*/*/*/*/*'], true), + entryDefinition('instrumented-jars', ['caches/jars-*/*'], true), + entryDefinition('kotlin-dsl', ['caches/*/kotlin-dsl/*/*'], true) + ] + this.debug(`Using extracted cache entry definitions: ${JSON.stringify(definitions, null, 2)}`) + return definitions } /**