Commit 4c7d97cc authored by Paul Merlin's avatar Paul Merlin
Browse files

Add caching of Gradle build configuration cache

parent 806543fb
Loading
Loading
Loading
Loading
+9
Changes for action.yml: 9 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -32,6 +32,15 @@ inputs:
  dependencies-cache-exact:
    description: Whether to restore only if exact match, default to 'false'
    required: false
  configuration-cache-enabled:
    description: Whether caching build configuration is enabled or not, default to 'false'
    required: false
  configuration-cache-key:
    description: Globs of files to hash in the build root directory, separated by new lines, use best-effort if unset
    required: false
  configuration-cache-exact:
    description: Whether to restore only if exact match, default to 'false'
    required: false

outputs:
  build-scan-url:
+1 −1

File changed.

Preview size limit exceeded, changes collapsed.

+1 −1

File changed.

Preview size limit exceeded, changes collapsed.

+96
Changes for src/cache-configuration.ts: 96 added lines, 0 removed lines.
Original line number Diff line number Diff line
import path from 'path'
import fs from 'fs'

import * as core from '@actions/core'
import * as cache from '@actions/cache'

import * as github from './github-utils'
import * as crypto from './crypto-utils'

import {inputCacheKeyGlobs, tryDeleteFiles} from './cache-dependencies'

const CONFIGURATION_CACHE_PATH = 'CONFIGURATION_CACHE_PATH'
const CONFIGURATION_CACHE_KEY = 'CONFIGURATION_CACHE_KEY'
const CONFIGURATION_CACHE_RESULT = 'CONFIGURATION_CACHE_RESULT'

export async function restoreCachedConfiguration(
    rootDir: string
): Promise<void> {
    if (isConfigurationCacheDisabled()) return

    const cachePath = path.resolve(rootDir, '.gradle/configuration-cache')
    core.saveState(CONFIGURATION_CACHE_PATH, cachePath)

    const inputCacheExact = github.inputBoolean('configuration-cache-exact')
    const cacheKeyGlobs = inputCacheKeyGlobs('configuration-cache-key')

    const hash = await crypto.hashFiles(rootDir, cacheKeyGlobs)
    const cacheKeyPrefix = 'configuration-'
    const cacheKey = `${cacheKeyPrefix}${hash}`
    core.saveState(CONFIGURATION_CACHE_KEY, cacheKey)

    const cacheResult = await cache.restoreCache(
        [cachePath],
        cacheKey,
        inputCacheExact ? [] : [cacheKeyPrefix]
    )

    if (!cacheResult) {
        core.info(
            'Configuration cache not found, expect task graph calculation.'
        )
        return
    }

    core.saveState(CONFIGURATION_CACHE_RESULT, cacheResult)
    core.info(`Configuration restored from cache key: ${cacheResult}`)
    return
}

export async function cacheConfiguration(): Promise<void> {
    if (isConfigurationCacheDisabled()) return

    const cachePath = core.getState(CONFIGURATION_CACHE_PATH)
    const cacheKey = core.getState(CONFIGURATION_CACHE_KEY)
    const cacheResult = core.getState(CONFIGURATION_CACHE_RESULT)

    if (!cachePath || !fs.existsSync(cachePath)) {
        core.debug('No configuration to cache.')
        return
    }

    if (cacheResult && cacheKey === cacheResult) {
        core.info(
            `Configuration cache hit occurred on the cache key ${cacheKey}, not saving cache.`
        )
        return
    }

    const locksDeleted = tryDeleteFiles([
        path.resolve(cachePath, 'configuration-cache.lock')
    ])
    if (!locksDeleted) {
        core.warning(
            'Unable to delete configuration lock files, try using --no-daemon or stopping the daemon last if you have several Gradle steps, not saving cache.'
        )
        return
    }

    try {
        await cache.saveCache([cachePath], cacheKey)
    } catch (error) {
        if (error.name === cache.ValidationError.name) {
            throw error
        } else if (error.name === cache.ReserveCacheError.name) {
            core.info(error.message)
        } else {
            core.info(`[warning] ${error.message}`)
        }
    }

    return
}

function isConfigurationCacheDisabled(): boolean {
    return !github.inputBoolean('configuration-cache-enabled', false)
}
+14 −11
Changes for src/cache-dependencies.ts: 14 added lines, 11 removed lines.
Original line number Diff line number Diff line
@@ -21,16 +21,7 @@ export async function restoreCachedDependencies(
    core.saveState(DEPENDENCIES_CACHE_PATH, cachePath)

    const inputCacheExact = github.inputBoolean('dependencies-cache-exact')

    const inputCacheKeyGlobs = github.inputArrayOrNull('dependencies-cache-key')
    const cacheKeyGlobs = inputCacheKeyGlobs
        ? inputCacheKeyGlobs
        : [
              '**/*.gradle',
              '**/*.gradle.kts',
              '**/gradle.properties',
              'gradle/**'
          ]
    const cacheKeyGlobs = inputCacheKeyGlobs('dependencies-cache-key')

    const hash = await crypto.hashFiles(rootDir, cacheKeyGlobs)
    const cacheKeyPrefix = 'dependencies-'
@@ -97,7 +88,7 @@ export async function cacheDependencies(): Promise<void> {
    return
}

function tryDeleteFiles(filePaths: string[]): boolean {
export function tryDeleteFiles(filePaths: string[]): boolean {
    let failure = false
    for (const filePath of filePaths) {
        if (fs.existsSync(filePath)) {
@@ -114,3 +105,15 @@ function tryDeleteFiles(filePaths: string[]): boolean {
function isDependenciesCacheDisabled(): boolean {
    return !github.inputBoolean('dependencies-cache-enabled', false)
}

export function inputCacheKeyGlobs(input: string): string[] {
    const inputValue = github.inputArrayOrNull(input)
    return inputValue
        ? inputValue
        : [
              '**/*.gradle',
              '**/*.gradle.kts',
              '**/gradle.properties',
              'gradle/**'
          ]
}
Loading