Merge pull request #140 from gradle/cache-disable

Fix Build Scan capture when cache is disabled

With the change to use init-scripts to inject Build Scan capture, this functionality broke when caching was disabled and Gradle User Home wasn't initialized.

This PR fixes a couple of issues and ensures that the Gradle User Home is initialized even when it is not restored from the GitHub Actions cache.

Fixes #138 
Fixes #139
This commit is contained in:
Daz DeBoer 2022-01-19 11:31:29 -07:00 committed by GitHub
commit a8da4e5ca3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 100 additions and 43 deletions

View File

@ -4,7 +4,7 @@ on:
workflow_dispatch: workflow_dispatch:
env: env:
GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}- GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}:${{github.run_attempt}}-
jobs: jobs:

View File

@ -7,7 +7,7 @@ on:
workflow_dispatch: workflow_dispatch:
env: env:
GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}- GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}:${{github.run_attempt}}-
jobs: jobs:
action-inputs: action-inputs:

View File

@ -7,7 +7,7 @@ on:
workflow_dispatch: workflow_dispatch:
env: env:
GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}- GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}:${{github.run_attempt}}-
GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED: true GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED: true
jobs: jobs:
@ -46,7 +46,7 @@ jobs:
- name: Setup Gradle - name: Setup Gradle
uses: ./ uses: ./
with: with:
# Use the same configuration when restoring state from cache # Use the same configuration as used in the seed build
gradle-home-cache-includes: | gradle-home-cache-includes: |
caches caches
enterprise enterprise
@ -57,4 +57,47 @@ jobs:
working-directory: __tests__/samples/groovy-dsl working-directory: __tests__/samples/groovy-dsl
run: ./gradlew test --offline run: ./gradlew test --offline
# Test that build scans are captured when caching is explicitly disabled
cache-disabled:
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-disabled: true
- name: Run Gradle build
id: gradle
working-directory: __tests__/samples/no-wrapper${{ matrix.build-root-suffix }}
run: gradle help "-DgradleVersionCheck=${{matrix.gradle}}"
- name: Check build scan url is captured
if: ${{ !steps.gradle.outputs.build-scan-url }}
uses: actions/github-script@v3
with:
script: |
core.setFailed('No build scan detected')
# Test that build scans are captured when caching is disabled because Gradle User Home already exists
cache-disabled-pre-existing-gradle-home:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Create dummy Gradle User Home
run: mkdir -p ~/.gradle/caches
- name: Setup Gradle
uses: ./
- name: Run Gradle build
id: gradle
working-directory: __tests__/samples/no-wrapper${{ matrix.build-root-suffix }}
run: gradle help "-DgradleVersionCheck=${{matrix.gradle}}"
- name: Check build scan url is captured
if: ${{ !steps.gradle.outputs.build-scan-url }}
uses: actions/github-script@v3
with:
script: |
core.setFailed('No build scan detected')

View File

@ -14,7 +14,7 @@ jobs:
# These builds should start fresh without cache hits, due to the seed injected into the cache key above. # These builds should start fresh without cache hits, due to the seed injected into the cache key above.
seed-build-groovy: seed-build-groovy:
env: env:
GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}-groovy- GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}:${{github.run_attempt}}-groovy-
strategy: strategy:
matrix: matrix:
os: [ubuntu-latest, windows-latest] os: [ubuntu-latest, windows-latest]
@ -30,7 +30,7 @@ jobs:
configuration-cache-groovy: configuration-cache-groovy:
env: env:
GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}-groovy- GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}:${{github.run_attempt}}-groovy-
needs: seed-build-groovy needs: seed-build-groovy
strategy: strategy:
matrix: matrix:
@ -52,7 +52,7 @@ jobs:
# Check that the build can run when no extracted cache entries are restored # Check that the build can run when no extracted cache entries are restored
no-extracted-cache-entries-restored: no-extracted-cache-entries-restored:
env: env:
GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}-groovy- GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}:${{github.run_attempt}}-groovy-
needs: seed-build-groovy needs: seed-build-groovy
strategy: strategy:
matrix: matrix:
@ -73,7 +73,7 @@ jobs:
seed-build-kotlin: seed-build-kotlin:
env: env:
GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}-kotlin- GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}:${{github.run_attempt}}-kotlin-
strategy: strategy:
matrix: matrix:
os: [ubuntu-latest, windows-latest] os: [ubuntu-latest, windows-latest]
@ -83,13 +83,13 @@ jobs:
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Setup Gradle - name: Setup Gradle
uses: ./ uses: ./
- name: Kotlin build with configuration-cache enabled - name: Execute 'help' with configuration-cache enabled
working-directory: __tests__/samples/kotlin-dsl working-directory: __tests__/samples/kotlin-dsl
run: ./gradlew help --configuration-cache run: ./gradlew help --configuration-cache
modify-build-kotlin: modify-build-kotlin:
env: env:
GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}-kotlin- GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}:${{github.run_attempt}}-kotlin-
needs: seed-build-kotlin needs: seed-build-kotlin
strategy: strategy:
matrix: matrix:
@ -100,14 +100,14 @@ jobs:
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Setup Gradle - name: Setup Gradle
uses: ./ uses: ./
- name: Kotlin build with configuration-cache enabled - name: Execute 'test' with configuration-cache enabled
working-directory: __tests__/samples/kotlin-dsl working-directory: __tests__/samples/kotlin-dsl
run: ./gradlew test --configuration-cache run: ./gradlew test --configuration-cache
# Test restore configuration-cache from the third build invocation # Test restore configuration-cache from the third build invocation
configuration-cache-kotlin: configuration-cache-kotlin:
env: env:
GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}-kotlin- GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}:${{github.run_attempt}}-kotlin-
needs: modify-build-kotlin needs: modify-build-kotlin
strategy: strategy:
matrix: matrix:
@ -120,7 +120,7 @@ jobs:
uses: ./ uses: ./
with: with:
cache-read-only: true cache-read-only: true
- name: Execute Gradle build and verify cached configuration - name: Execute 'test' again and verify cached configuration
env: env:
VERIFY_CACHED_CONFIGURATION: true VERIFY_CACHED_CONFIGURATION: true
working-directory: __tests__/samples/kotlin-dsl working-directory: __tests__/samples/kotlin-dsl

View File

@ -7,7 +7,7 @@ on:
workflow_dispatch: workflow_dispatch:
env: env:
GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}- GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}:${{github.run_attempt}}-
jobs: jobs:
seed-build: seed-build:

View File

@ -7,7 +7,7 @@ on:
workflow_dispatch: workflow_dispatch:
env: env:
GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}- GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}:${{github.run_attempt}}-
jobs: jobs:
seed-build: seed-build:

View File

@ -7,7 +7,7 @@ on:
workflow_dispatch: workflow_dispatch:
env: env:
GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}- GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}:${{github.run_attempt}}-
jobs: jobs:
seed-build: seed-build:

View File

@ -7,7 +7,7 @@ on:
workflow_dispatch: workflow_dispatch:
env: env:
GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}- GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}:${{github.run_attempt}}-
jobs: jobs:
# Tests for executing with different Gradle versions. # Tests for executing with different Gradle versions.

View File

@ -7,7 +7,7 @@ on:
workflow_dispatch: workflow_dispatch:
env: env:
GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}- GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}:${{github.run_attempt}}-
GRADLE_USER_HOME: ${{github.workspace}}/custom/gradle/home GRADLE_USER_HOME: ${{github.workspace}}/custom/gradle/home
jobs: jobs:

View File

@ -7,7 +7,7 @@ on:
workflow_dispatch: workflow_dispatch:
env: env:
GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}- GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}:${{github.run_attempt}}-
jobs: jobs:
# Tests for executing with different Gradle versions. # Tests for executing with different Gradle versions.

View File

@ -7,7 +7,7 @@ on:
workflow_dispatch: workflow_dispatch:
env: env:
GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}- GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}:${{github.run_attempt}}-
GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED: true GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED: true
jobs: jobs:

View File

@ -7,7 +7,7 @@ on:
workflow_dispatch: workflow_dispatch:
env: env:
GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}- GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}:${{github.run_attempt}}-
GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED: true GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED: true
jobs: jobs:

2
dist/main/index.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

2
dist/post/index.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -99,6 +99,17 @@ export class GradleStateCache {
this.initializeGradleUserHome(this.gradleUserHome, initScriptsDir) this.initializeGradleUserHome(this.gradleUserHome, initScriptsDir)
} }
cacheOutputExists(): boolean {
const paths = this.getCachePath()
for (const p of paths) {
if (fs.existsSync(p)) {
cacheDebug(`Cache output exists at ${p}`)
return true
}
}
return false
}
/** /**
* Restores the cache entry, finding the closest match to the currently running job. * Restores the cache entry, finding the closest match to the currently running job.
*/ */

View File

@ -8,11 +8,29 @@ const GRADLE_USER_HOME = 'GRADLE_USER_HOME'
const CACHE_LISTENER = 'CACHE_LISTENER' const CACHE_LISTENER = 'CACHE_LISTENER'
export async function restore(gradleUserHome: string): Promise<void> { export async function restore(gradleUserHome: string): Promise<void> {
if (!shouldRestoreCaches()) { // Bypass restore cache on all but first action step in workflow.
if (process.env[CACHE_RESTORED_VAR]) {
core.info('Cache only restored on first action step.')
return
}
core.exportVariable(CACHE_RESTORED_VAR, true)
const gradleStateCache = new GradleStateCache(gradleUserHome)
if (isCacheDisabled()) {
core.info('Cache is disabled: will not restore state from previous builds.')
// Initialize the Gradle User Home even when caching is disabled.
gradleStateCache.init()
return
}
if (gradleStateCache.cacheOutputExists()) {
core.info('Gradle User Home already exists: will not restore from cache.')
// Initialize pre-existing Gradle User Home.
gradleStateCache.init()
return return
} }
const gradleStateCache = new GradleStateCache(gradleUserHome)
gradleStateCache.init() gradleStateCache.init()
await core.group('Restore Gradle state from cache', async () => { await core.group('Restore Gradle state from cache', async () => {
@ -24,8 +42,6 @@ export async function restore(gradleUserHome: string): Promise<void> {
core.saveState(CACHE_LISTENER, cacheListener.stringify()) core.saveState(CACHE_LISTENER, cacheListener.stringify())
}) })
// Export var that is detected in all later restore steps
core.exportVariable(CACHE_RESTORED_VAR, true)
// Export state that is detected in corresponding post-action step // Export state that is detected in corresponding post-action step
core.saveState(CACHE_RESTORED_VAR, true) core.saveState(CACHE_RESTORED_VAR, true)
} }
@ -51,19 +67,6 @@ export async function save(): Promise<void> {
logCachingReport(cacheListener) logCachingReport(cacheListener)
} }
function shouldRestoreCaches(): boolean {
if (isCacheDisabled()) {
core.info('Cache is disabled: will not restore state from previous builds.')
return false
}
if (process.env[CACHE_RESTORED_VAR]) {
core.info('Cache only restored on first action step.')
return false
}
return true
}
function shouldSaveCaches(): boolean { function shouldSaveCaches(): boolean {
if (isCacheDisabled()) { if (isCacheDisabled()) {
core.info('Cache is disabled: will not save state for later builds.') core.info('Cache is disabled: will not save state for later builds.')
@ -71,7 +74,7 @@ function shouldSaveCaches(): boolean {
} }
if (!core.getState(CACHE_RESTORED_VAR)) { if (!core.getState(CACHE_RESTORED_VAR)) {
core.info('Cache will only be saved in final post-action step.') core.info('Cache will not be saved: not restored in main action step.')
return false return false
} }