From a77cb2b0f88183c4b03dad0e1e70efeeb575319c Mon Sep 17 00:00:00 2001 From: daz Date: Thu, 18 Jul 2024 21:40:13 -0600 Subject: [PATCH] Add test for no cache-cleanup with config-cache hit --- ...integ-test-restore-configuration-cache.yml | 82 ++++++++++++++----- sources/src/build-results.ts | 4 + sources/src/caching/cache-reporting.ts | 3 + sources/src/caching/caches.ts | 12 ++- 4 files changed, 80 insertions(+), 21 deletions(-) diff --git a/.github/workflows/integ-test-restore-configuration-cache.yml b/.github/workflows/integ-test-restore-configuration-cache.yml index ab578e9..d681a27 100644 --- a/.github/workflows/integ-test-restore-configuration-cache.yml +++ b/.github/workflows/integ-test-restore-configuration-cache.yml @@ -43,6 +43,7 @@ jobs: uses: ./setup-gradle with: cache-read-only: false # For testing, allow writing cache entries on non-default branches + cache-write-only: true # Ensure we start with a clean cache entry cache-encryption-key: ${{ secrets.GRADLE_ENCRYPTION_KEY }} gradle-version: 8.6 - name: Groovy build with configuration-cache enabled @@ -52,6 +53,7 @@ jobs: verify-build-groovy: env: GRADLE_BUILD_ACTION_CACHE_KEY_JOB: restore-cc-groovy + GRADLE_BUILD_ACTION_CACHE_KEY_JOB_EXECUTION: ${{github.sha}}_1 needs: seed-build-groovy strategy: fail-fast: false @@ -64,6 +66,47 @@ jobs: - name: Initialize integ-test uses: ./.github/actions/init-integ-test + - name: Setup Java to ensure consistency + uses: actions/setup-java@v4 + with: + distribution: 'liberica' + java-version: 17 + - name: Setup Gradle + uses: ./setup-gradle + with: + cache-read-only: false + cache-cleanup: on-success + cache-encryption-key: ${{ secrets.GRADLE_ENCRYPTION_KEY }} + gradle-version: 8.6 + - name: Groovy build with configuration-cache enabled + id: execute + working-directory: .github/workflow-samples/groovy-dsl + run: gradle test --configuration-cache + - name: Verify configuration-cache hit + shell: bash + run: | + if [ -e ".github/workflow-samples/groovy-dsl/task-configured.txt" ]; then + echo "Configuration cache was not used - task was configured unexpectedly" + exit 1 + fi + + # Ensure that cache-cleanup doesn't remove all necessary files + verify-no-cache-cleanup-groovy: + env: + GRADLE_BUILD_ACTION_CACHE_KEY_JOB: restore-cc-groovy + GRADLE_BUILD_ACTION_CACHE_KEY_JOB_EXECUTION: ${{github.sha}}_2 + needs: verify-build-groovy + strategy: + fail-fast: false + matrix: + os: ${{fromJSON(inputs.runner-os)}} + runs-on: ${{ matrix.os }} + steps: + - name: Checkout sources + uses: actions/checkout@v4 + - name: Initialize integ-test + uses: ./.github/actions/init-integ-test + - name: Setup Java to ensure consistency uses: actions/setup-java@v4 with: @@ -79,19 +122,19 @@ jobs: id: execute working-directory: .github/workflow-samples/groovy-dsl run: gradle test --configuration-cache - - name: Check that configuration-cache was used - uses: actions/github-script@v7 - with: - script: | - const fs = require('fs') - if (fs.existsSync('.github/workflow-samples/groovy-dsl/task-configured.txt')) { - core.setFailed('Configuration cache was not used - task was configured unexpectedly') - } + - name: Verify configuration-cache hit + shell: bash + run: | + if [ -e ".github/workflow-samples/groovy-dsl/task-configured.txt" ]; then + echo "Configuration cache was not used - task was configured unexpectedly" + exit 1 + fi # Check that the build can run when no extracted cache entries are restored gradle-user-home-not-fully-restored: env: GRADLE_BUILD_ACTION_CACHE_KEY_JOB: restore-cc-groovy + GRADLE_BUILD_ACTION_CACHE_KEY_JOB_EXECUTION: ${{github.sha}}_x needs: seed-build-groovy strategy: fail-fast: false @@ -144,6 +187,7 @@ jobs: uses: ./setup-gradle with: cache-read-only: false # For testing, allow writing cache entries on non-default branches + cache-write-only: true # Ensure we start with a clean cache entry cache-encryption-key: ${{ secrets.GRADLE_ENCRYPTION_KEY }} gradle-version: 8.6 - name: Execute 'help' with configuration-cache enabled @@ -152,7 +196,8 @@ jobs: modify-build-kotlin: env: - GRADLE_BUILD_ACTION_CACHE_KEY_JOB: restore-cc-kotlin-modified + GRADLE_BUILD_ACTION_CACHE_KEY_JOB: restore-cc-kotlin + GRADLE_BUILD_ACTION_CACHE_KEY_JOB_EXECUTION: ${{github.sha}}_1 needs: seed-build-kotlin strategy: fail-fast: false @@ -183,7 +228,8 @@ jobs: # Test restore configuration-cache from the third build invocation verify-build-kotlin: env: - GRADLE_BUILD_ACTION_CACHE_KEY_JOB: restore-cc-kotlin-modified + GRADLE_BUILD_ACTION_CACHE_KEY_JOB: restore-cc-kotlin + GRADLE_BUILD_ACTION_CACHE_KEY_JOB_EXECUTION: ${{github.sha}}_2 needs: modify-build-kotlin strategy: fail-fast: false @@ -211,12 +257,10 @@ jobs: id: execute working-directory: .github/workflow-samples/kotlin-dsl run: gradle test --configuration-cache - - name: Check that configuration-cache was used - uses: actions/github-script@v7 - with: - script: | - const fs = require('fs') - if (fs.existsSync('.github/workflow-samples/kotlin-dsl/task-configured.txt')) { - core.setFailed('Configuration cache was not used - task was configured unexpectedly') - } - + - name: Verify configuration-cache hit + shell: bash + run: | + if [ -e ".github/workflow-samples/kotlin-dsl/task-configured.txt" ]; then + echo "Configuration cache was not used - task was configured unexpectedly" + exit 1 + fi diff --git a/sources/src/build-results.ts b/sources/src/build-results.ts index 9e3b1b4..82934cf 100644 --- a/sources/src/build-results.ts +++ b/sources/src/build-results.ts @@ -24,6 +24,10 @@ export class BuildResults { return this.results.some(result => result.buildFailed) } + anyConfigCacheHit(): boolean { + return this.results.some(result => result.configCacheHit) + } + uniqueGradleHomes(): string[] { const allHomes = this.results.map(buildResult => buildResult.gradleHomeDir) return Array.from(new Set(allHomes)) diff --git a/sources/src/caching/cache-reporting.ts b/sources/src/caching/cache-reporting.ts index 6dd0183..2ad07eb 100644 --- a/sources/src/caching/cache-reporting.ts +++ b/sources/src/caching/cache-reporting.ts @@ -19,6 +19,9 @@ export const DEFAULT_CLEANUP_ENABLED_REASON = `[Cache cleanup](https://github.co export const CLEANUP_DISABLED_DUE_TO_FAILURE = '[Cache cleanup was disabled due to build failure](https://github.com/gradle/actions/blob/v3/docs/setup-gradle.md#enabling-cache-cleanup). Use `cache-cleanup: always` to override this behavior.' +export const CLEANUP_DISABLED_DUE_TO_CONFIG_CACHE_HIT = + '[Cache cleanup was disabled due to configuration-cache reuse](https://github.com/gradle/actions/blob/v3/docs/setup-gradle.md#enabling-cache-cleanup). This is expected.' + /** * Collects information on what entries were saved and restored during the action. * This information is used to generate a summary of the cache usage. diff --git a/sources/src/caching/caches.ts b/sources/src/caching/caches.ts index 5602a94..9ea0e5f 100644 --- a/sources/src/caching/caches.ts +++ b/sources/src/caching/caches.ts @@ -1,5 +1,10 @@ import * as core from '@actions/core' -import {CacheListener, EXISTING_GRADLE_HOME, CLEANUP_DISABLED_DUE_TO_FAILURE} from './cache-reporting' +import { + CacheListener, + EXISTING_GRADLE_HOME, + CLEANUP_DISABLED_DUE_TO_FAILURE, + CLEANUP_DISABLED_DUE_TO_CONFIG_CACHE_HIT +} from './cache-reporting' import {GradleUserHomeCache} from './gradle-user-home-cache' import {CacheCleaner} from './cache-cleaner' import {DaemonController} from '../daemon-controller' @@ -90,7 +95,10 @@ export async function save( await daemonController.stopAllDaemons() if (cacheConfig.isCacheCleanupEnabled()) { - if (cacheConfig.shouldPerformCacheCleanup(buildResults.anyFailed())) { + if (buildResults.anyConfigCacheHit()) { + core.info('Not performing cache-cleanup due to config-cache reuse') + cacheListener.setCacheCleanupDisabled(CLEANUP_DISABLED_DUE_TO_CONFIG_CACHE_HIT) + } else if (cacheConfig.shouldPerformCacheCleanup(buildResults.anyFailed())) { cacheListener.setCacheCleanupEnabled() await performCacheCleanup(gradleUserHome) } else {