Allow cache to overwrite existing Gradle User Home

Fixes #480
This commit is contained in:
daz 2023-08-19 13:01:29 -06:00
parent 68e1dcdea4
commit 3d49588efc
No known key found for this signature in database
5 changed files with 66 additions and 7 deletions

View file

@ -99,3 +99,40 @@ jobs:
working-directory: .github/workflow-samples/groovy-dsl
run: ./gradlew test
# Test that a pre-existing gradle-user-home can be overwritten by the restored cache
pre-existing-gradle-home:
needs: seed-build
strategy:
matrix:
os: ${{fromJSON(inputs.runner-os)}}
runs-on: ${{ matrix.os }}
steps:
- name: Checkout sources
uses: actions/checkout@v3
- name: Download distribution if required
uses: ./.github/actions/download-dist
- name: Pre-create Gradle User Home
shell: bash
run: |
mkdir -p ~/.gradle/caches
touch ~/.gradle/gradle.properties
touch ~/.gradle/caches/dummy.txt
- name: Setup Gradle
uses: ./
with:
cache-read-only: true
cache-overwrite-existing: true
- name: Check that pre-existing content still exists
shell: bash
run: |
if [ ! -e ~/.gradle/caches/dummy.txt ]; then
echo "::error ::Should find dummy.txt after cache restore"
exit 1
fi
if [ ! -e ~/.gradle/gradle.properties ]; then
echo "::error ::Should find gradle.properties after cache restore"
exit 1
fi
- name: Execute Gradle build with --offline
working-directory: .github/workflow-samples/groovy-dsl
run: ./gradlew test --offline

View file

@ -35,6 +35,11 @@ inputs:
required: false
default: false
cache-overwrite-existing:
description: When 'true', a pre-existing Gradle User Home will not prevent the cache from being restored.
required: false
default: false
gradle-home-cache-includes:
description: Paths within Gradle User Home to cache.
required: false

View file

@ -37,6 +37,10 @@ export function isCacheWriteOnly(): boolean {
return params.isCacheWriteOnly()
}
export function isCacheOverwriteExisting(): boolean {
return params.isCacheOverwriteExisting()
}
export function isCacheDebuggingEnabled(): boolean {
return params.isCacheDebuggingEnabled()
}

View file

@ -1,5 +1,11 @@
import * as core from '@actions/core'
import {isCacheCleanupEnabled, isCacheDisabled, isCacheReadOnly, isCacheWriteOnly} from './cache-utils'
import {
isCacheCleanupEnabled,
isCacheDisabled,
isCacheReadOnly,
isCacheWriteOnly,
isCacheOverwriteExisting
} from './cache-utils'
import {CacheListener} from './cache-reporting'
import {DaemonController} from './daemon-controller'
import {GradleStateCache} from './cache-base'
@ -26,12 +32,15 @@ export async function restore(gradleUserHome: string, cacheListener: CacheListen
}
if (gradleStateCache.cacheOutputExists()) {
core.info('Gradle User Home already exists: will not restore from cache.')
// Initialize pre-existing Gradle User Home.
gradleStateCache.init()
cacheListener.cacheDisabled = true
cacheListener.cacheDisabledReason = 'disabled due to pre-existing Gradle User Home'
return
if (!isCacheOverwriteExisting()) {
core.info('Gradle User Home already exists: will not restore from cache.')
// Initialize pre-existing Gradle User Home.
gradleStateCache.init()
cacheListener.cacheDisabled = true
cacheListener.cacheDisabledReason = 'disabled due to pre-existing Gradle User Home'
return
}
core.info('Gradle User Home already exists: will overwrite with cached contents.')
}
gradleStateCache.init()

View file

@ -13,6 +13,10 @@ export function isCacheWriteOnly(): boolean {
return getBooleanInput('cache-write-only')
}
export function isCacheOverwriteExisting(): boolean {
return getBooleanInput('cache-overwrite-existing')
}
export function isCacheStrictMatch(): boolean {
return getBooleanInput('gradle-home-cache-strict-match')
}