Unverified Commit 498f0e40 authored by daz's avatar daz
Browse files

Update cache key names

- All cache keys are now structured as 'gradle-<cache-name>-<protocol-version>
  - This ensures that extracted entries are prefixed and versioned consistently
- Avoid using custom cache-key prefix for extracted entries. This should reduce the
  churn in integration tests that require some level of cache isolation.
parent 3335c161
Loading
Loading
Loading
Loading
+7 −5
Changes for sources/src/caching/cache-key.ts: 7 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@ import * as github from '@actions/github'
import {CacheConfig, getJobMatrix} from '../input-params'
import {hashStrings} from './cache-utils'

const CACHE_PROTOCOL_VERSION = 'v9-'
const CACHE_PROTOCOL_VERSION = 'v1'

const CACHE_KEY_PREFIX_VAR = 'GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX'
const CACHE_KEY_OS_VAR = 'GRADLE_BUILD_ACTION_CACHE_KEY_ENVIRONMENT'
@@ -29,8 +29,8 @@ export class CacheKey {
/**
 * Generates a cache key specific to the current job execution.
 * The key is constructed from the following inputs (with some user overrides):
 * - The cache key prefix: defaults to 'gradle-' but can be overridden by the user
 * - The cache protocol version
 * - The name of the cache
 * - The runner operating system
 * - The name of the workflow and Job being executed
 * - The matrix values for the Job being executed (job context)
@@ -43,7 +43,9 @@ export class CacheKey {
 * - Any previous key for this cache on the current OS
 */
export function generateCacheKey(cacheName: string, config: CacheConfig): CacheKey {
    const cacheKeyBase = `${getCacheKeyPrefix()}${CACHE_PROTOCOL_VERSION}${cacheName}`
    const prefix = process.env[CACHE_KEY_PREFIX_VAR] || ''

    const cacheKeyBase = `${prefix}${getCacheKeyBase(cacheName, CACHE_PROTOCOL_VERSION)}`

    // At the most general level, share caches for all executions on the same OS
    const cacheKeyForEnvironment = `${cacheKeyBase}|${getCacheKeyEnvironment()}`
@@ -64,9 +66,9 @@ export function generateCacheKey(cacheName: string, config: CacheConfig): CacheK
    return new CacheKey(cacheKey, [cacheKeyForJobContext, cacheKeyForJob, cacheKeyForEnvironment])
}

export function getCacheKeyPrefix(): string {
export function getCacheKeyBase(cacheName: string, cacheProtocolVersion: string): string {
    // Prefix can be used to force change all cache keys (defaults to cache protocol version)
    return process.env[CACHE_KEY_PREFIX_VAR] || ''
    return `gradle-${cacheName}-${cacheProtocolVersion}`
}

function getCacheKeyEnvironment(): string {
+4 −5
Changes for sources/src/caching/gradle-home-extry-extractor.ts: 4 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -10,9 +10,10 @@ import {cacheDebug, hashFileNames, isCacheDebuggingEnabled, restoreCache, saveCa

import {BuildResult, loadBuildResults} from '../build-results'
import {CacheConfig} from '../input-params'
import {getCacheKeyPrefix} from './cache-key'
import {getCacheKeyBase} from './cache-key'

const SKIP_RESTORE_VAR = 'GRADLE_BUILD_ACTION_SKIP_RESTORE'
const CACHE_PROTOCOL_VERSION = 'v1'

/**
 * Represents the result of attempting to load or store an extracted cache entry.
@@ -244,22 +245,20 @@ abstract class AbstractEntryExtractor {
    }

    protected createCacheKeyFromFileNames(artifactType: string, files: string[]): string {
        const cacheKeyPrefix = getCacheKeyPrefix()
        const relativeFiles = files.map(x => path.relative(this.gradleUserHome, x))
        const key = hashFileNames(relativeFiles)

        cacheDebug(`Generating cache key for ${artifactType} from file names: ${relativeFiles}`)

        return `${cacheKeyPrefix}${artifactType}-${key}`
        return `${getCacheKeyBase(artifactType, CACHE_PROTOCOL_VERSION)}-${key}`
    }

    protected async createCacheKeyFromFileContents(artifactType: string, pattern: string): Promise<string> {
        const cacheKeyPrefix = getCacheKeyPrefix()
        const key = await glob.hashFiles(pattern)

        cacheDebug(`Generating cache key for ${artifactType} from files matching: ${pattern}`)

        return `${cacheKeyPrefix}${artifactType}-${key}`
        return `${getCacheKeyBase(artifactType, CACHE_PROTOCOL_VERSION)}-${key}`
    }

    // Run actions sequentially if debugging is enabled
+1 −1
Changes for sources/src/caching/gradle-user-home-cache.ts: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@ const RESTORED_CACHE_KEY_KEY = 'restored-cache-key'
export const META_FILE_DIR = '.setup-gradle'

export class GradleUserHomeCache {
    private readonly cacheName = 'gradle'
    private readonly cacheName = 'home'
    private readonly cacheDescription = 'Gradle User Home'

    private readonly userHome: string