Unverified Commit 08d5b40c authored by Daz DeBoer's avatar Daz DeBoer
Browse files

Add experimental support for 'cache-write-only'

There may be cases where it a "fresh" cache entry would be beneficial,
for example if the Gradle User Home cache entry grows over time.

This setting would run the build as if no prior cache entry exists.
parent 0a5ede19
Loading
Loading
Loading
Loading
+39
Changes for .github/workflows/integTest-caching-config.yml: 39 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -101,3 +101,42 @@ jobs:
      with:
        script: |
          core.setFailed('No build scan detected')

  # Test seed the cache with cache-write-only and verify with cache-read-only
  seed-build-write-only:
    env:
      GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}:${{github.run_attempt}}-write-only-
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest]
    runs-on: ${{ matrix.os }}
    steps:
    - name: Checkout sources
      uses: actions/checkout@v2
    - name: Setup Gradle
      uses: ./
      with:
        cache-write-only: true
    - name: Build using Gradle wrapper
      working-directory: __tests__/samples/groovy-dsl
      run: ./gradlew test

  verify-write-only-build:
    env:
      GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}:${{github.run_attempt}}-write-only-
    needs: seed-build-write-only
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest]
    runs-on: ${{ matrix.os }}
    steps:
    - name: Checkout sources
      uses: actions/checkout@v2
    - name: Setup Gradle
      uses: ./
      with:
        cache-read-only: true
    - name: Execute Gradle build with --offline
      working-directory: __tests__/samples/groovy-dsl
      run: ./gradlew test --offline
+4
Changes for action.yml: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -50,6 +50,10 @@ inputs:
  # The following action properties allow fine-grained tweaking of the action caching behaviour.
  # These properties are experimental and not (yet) designed for production use, and may change without notice in a subsequent release of `gradle-build-action`.
  # Use at your own risk!
  cache-write-only:
    description: When 'true', entries will not be restored from the cache but will be saved at the end of the Job. This allows a 'clean' cache entry to be written.
    required: false
    default: false
  gradle-home-cache-strict-match:
    description: When 'true', the action will not attempt to restore the Gradle User Home entries from other Jobs.
    required: false
+8 −13
Changes for src/cache-base.ts: 8 added lines, 13 removed lines.
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ import {
import {ConfigurationCacheEntryExtractor, GradleHomeEntryExtractor} from './cache-extract-entries'

const CACHE_PROTOCOL_VERSION = 'v6-'
const RESTORED_CACHE_KEY_KEY = 'restored-cache-key'

export const META_FILE_DIR = '.gradle-build-action'
export const PROJECT_ROOTS_FILE = 'project-roots.txt'
@@ -81,8 +82,6 @@ function generateCacheKey(cacheName: string): CacheKey {
export class GradleStateCache {
    private cacheName: string
    private cacheDescription: string
    private cacheKeyStateKey: string
    private cacheResultStateKey: string

    protected readonly gradleUserHome: string

@@ -90,8 +89,6 @@ export class GradleStateCache {
        this.gradleUserHome = gradleUserHome
        this.cacheName = 'gradle'
        this.cacheDescription = 'Gradle User Home'
        this.cacheKeyStateKey = `CACHE_KEY_gradle`
        this.cacheResultStateKey = `CACHE_RESULT_gradle`
    }

    init(): void {
@@ -122,7 +119,6 @@ export class GradleStateCache {
        const entryListener = listener.entry(this.cacheDescription)

        const cacheKey = generateCacheKey(this.cacheName)
        core.saveState(this.cacheKeyStateKey, cacheKey.key)

        cacheDebug(
            `Requesting ${this.cacheDescription} with
@@ -136,7 +132,7 @@ export class GradleStateCache {
            return
        }

        core.saveState(this.cacheResultStateKey, cacheResult.key)
        core.saveState(RESTORED_CACHE_KEY_KEY, cacheResult.key)

        core.info(`Restored ${this.cacheDescription} from cache key: ${cacheResult.key}`)

@@ -165,12 +161,11 @@ export class GradleStateCache {
     * it is saved with the exact key.
     */
    async save(listener: CacheListener): Promise<void> {
        // Retrieve the state set in the previous 'restore' step.
        const cacheKeyFromRestore = core.getState(this.cacheKeyStateKey)
        const cacheResultFromRestore = core.getState(this.cacheResultStateKey)
        const cacheKey = generateCacheKey(this.cacheName).key
        const restoredCacheKey = core.getState(RESTORED_CACHE_KEY_KEY)

        if (cacheResultFromRestore && cacheKeyFromRestore === cacheResultFromRestore) {
            core.info(`Cache hit occurred on the cache key ${cacheKeyFromRestore}, not saving cache.`)
        if (restoredCacheKey && cacheKey === restoredCacheKey) {
            core.info(`Cache hit occurred on the cache key ${cacheKey}, not saving cache.`)
            return
        }

@@ -181,10 +176,10 @@ export class GradleStateCache {
            return
        }

        core.info(`Caching ${this.cacheDescription} with cache key: ${cacheKeyFromRestore}`)
        core.info(`Caching ${this.cacheDescription} with cache key: ${cacheKey}`)
        const cachePath = this.getCachePath()
        const entryListener = listener.entry(this.cacheDescription)
        await saveCache(cachePath, cacheKeyFromRestore, entryListener)
        await saveCache(cachePath, cacheKey, entryListener)

        return
    }
+3
Changes for src/cache-reporting.ts: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -28,6 +28,9 @@ export class CacheListener {
    }

    static rehydrate(stringRep: string): CacheListener {
        if (stringRep === '') {
            return new CacheListener()
        }
        const rehydrated: CacheListener = Object.assign(new CacheListener(), JSON.parse(stringRep))
        const entries = rehydrated.cacheEntries
        for (let index = 0; index < entries.length; index++) {
+5
Changes for src/cache-utils.ts: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ import {CacheEntryListener} from './cache-reporting'
const JOB_CONTEXT_PARAMETER = 'workflow-job-context'
const CACHE_DISABLED_PARAMETER = 'cache-disabled'
const CACHE_READONLY_PARAMETER = 'cache-read-only'
const CACHE_WRITEONLY_PARAMETER = 'cache-write-only'
const CACHE_DEBUG_VAR = 'GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED'
const CACHE_PREFIX_VAR = 'GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX'

@@ -20,6 +21,10 @@ export function isCacheReadOnly(): boolean {
    return core.getBooleanInput(CACHE_READONLY_PARAMETER)
}

export function isCacheWriteOnly(): boolean {
    return core.getBooleanInput(CACHE_WRITEONLY_PARAMETER)
}

export function isCacheDebuggingEnabled(): boolean {
    return process.env[CACHE_DEBUG_VAR] ? true : false
}
Loading