Unverified Commit b6395da6 authored by daz's avatar daz
Browse files

Cache validated checksums for later executions

The most common case for validation will be that the wrapper jars are unchanged
from a previous workflow run. In this case, we cache the validated wrapper
checksums to minimise the work required on a subsequent run.

Fixes #172
parent ce4c3a6c
Loading
Loading
Loading
Loading
+2 −3
Changes for sources/src/caching/gradle-home-extry-extractor.ts: 2 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -4,12 +4,11 @@ import * as core from '@actions/core'
import * as glob from '@actions/glob'
import * as semver from 'semver'

import {META_FILE_DIR} from './gradle-user-home-cache'
import {CacheEntryListener, CacheListener} from './cache-reporting'
import {cacheDebug, hashFileNames, isCacheDebuggingEnabled, restoreCache, saveCache, tryDelete} from './cache-utils'

import {BuildResult, loadBuildResults} from '../build-results'
import {CacheConfig} from '../configuration'
import {CacheConfig, ACTION_METADATA_DIR} from '../configuration'
import {getCacheKeyBase} from './cache-key'

const SKIP_RESTORE_VAR = 'GRADLE_BUILD_ACTION_SKIP_RESTORE'
@@ -298,7 +297,7 @@ abstract class AbstractEntryExtractor {
    }

    private getCacheMetadataFile(): string {
        const actionMetadataDirectory = path.resolve(this.gradleUserHome, META_FILE_DIR)
        const actionMetadataDirectory = path.resolve(this.gradleUserHome, ACTION_METADATA_DIR)
        fs.mkdirSync(actionMetadataDirectory, {recursive: true})

        return path.resolve(actionMetadataDirectory, `${this.extractorName}-entry-metadata.json`)
+3 −5
Changes for sources/src/caching/gradle-user-home-cache.ts: 3 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -7,14 +7,12 @@ import fs from 'fs'
import {generateCacheKey} from './cache-key'
import {CacheListener} from './cache-reporting'
import {saveCache, restoreCache, cacheDebug, isCacheDebuggingEnabled, tryDelete} from './cache-utils'
import {CacheConfig} from '../configuration'
import {CacheConfig, ACTION_METADATA_DIR} from '../configuration'
import {GradleHomeEntryExtractor, ConfigurationCacheEntryExtractor} from './gradle-home-extry-extractor'
import {getPredefinedToolchains, mergeToolchainContent, readResourceFileAsString} from './gradle-user-home-utils'

const RESTORED_CACHE_KEY_KEY = 'restored-cache-key'

export const META_FILE_DIR = '.setup-gradle'

export class GradleUserHomeCache {
    private readonly cacheName = 'home'
    private readonly cacheDescription = 'Gradle User Home'
@@ -172,7 +170,7 @@ export class GradleUserHomeCache {
     */
    protected getCachePath(): string[] {
        const rawPaths: string[] = this.cacheConfig.getCacheIncludes()
        rawPaths.push(META_FILE_DIR)
        rawPaths.push(ACTION_METADATA_DIR)
        const resolvedPaths = rawPaths.map(x => this.resolveCachePath(x))
        cacheDebug(`Using cache paths: ${resolvedPaths}`)
        return resolvedPaths
@@ -188,7 +186,7 @@ export class GradleUserHomeCache {

    private initializeGradleUserHome(): void {
        // Create a directory for storing action metadata
        const actionCacheDir = path.resolve(this.gradleUserHome, META_FILE_DIR)
        const actionCacheDir = path.resolve(this.gradleUserHome, ACTION_METADATA_DIR)
        fs.mkdirSync(actionCacheDir, {recursive: true})

        this.copyInitScripts()
+2
Changes for sources/src/configuration.ts: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -8,6 +8,8 @@ import path from 'path'

const ACTION_ID_VAR = 'GRADLE_ACTION_ID'

export const ACTION_METADATA_DIR = '.setup-gradle'

export class DependencyGraphConfig {
    getDependencyGraphOption(): DependencyGraphOption {
        const val = core.getInput('dependency-graph')
+2 −2
Changes for sources/src/setup-gradle.ts: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -46,13 +46,13 @@ export async function setup(
    core.saveState(USER_HOME, userHome)
    core.saveState(GRADLE_USER_HOME, gradleUserHome)

    await wrapperValidator.validateWrappers(wrapperValidationConfig, getWorkspaceDirectory())

    const cacheListener = new CacheListener()
    await caches.restore(userHome, gradleUserHome, cacheListener, cacheConfig)

    core.saveState(CACHE_LISTENER, cacheListener.stringify())

    await wrapperValidator.validateWrappers(wrapperValidationConfig, getWorkspaceDirectory(), gradleUserHome)

    await buildScan.setup(buildScanConfig)

    return true
+26
Changes for sources/src/wrapper-validation/cache.ts: 26 added lines, 0 removed lines.
Original line number Diff line number Diff line
import fs from 'fs'
import path from 'path'
import {ACTION_METADATA_DIR} from '../configuration'

export class ChecksumCache {
    private readonly cacheFile: string

    constructor(gradleUserHome: string) {
        this.cacheFile = path.resolve(gradleUserHome, ACTION_METADATA_DIR, 'valid-wrappers.json')
    }

    load(): string[] {
        // Load previously validated checksums saved in Gradle User Home
        if (fs.existsSync(this.cacheFile)) {
            return JSON.parse(fs.readFileSync(this.cacheFile, 'utf-8'))
        }
        return []
    }

    save(checksums: string[]): void {
        const uniqueChecksums = [...new Set(checksums)]
        // Save validated checksums to Gradle User Home
        fs.mkdirSync(path.dirname(this.cacheFile), {recursive: true})
        fs.writeFileSync(this.cacheFile, JSON.stringify(uniqueChecksums))
    }
}
Loading