diff --git a/.github/workflows/integ-test-restore-gradle-home.yml b/.github/workflows/integ-test-restore-gradle-home.yml index 59d0296..a0161e9 100644 --- a/.github/workflows/integ-test-restore-gradle-home.yml +++ b/.github/workflows/integ-test-restore-gradle-home.yml @@ -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 diff --git a/action.yml b/action.yml index 12213c5..4862897 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/src/cache-utils.ts b/src/cache-utils.ts index 69df849..61cc663 100644 --- a/src/cache-utils.ts +++ b/src/cache-utils.ts @@ -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() } diff --git a/src/caches.ts b/src/caches.ts index 4de55db..4c78388 100644 --- a/src/caches.ts +++ b/src/caches.ts @@ -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() diff --git a/src/input-params.ts b/src/input-params.ts index 03e62c2..0bd7ab4 100644 --- a/src/input-params.ts +++ b/src/input-params.ts @@ -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') }