Unverified Commit 6fca6b39 authored by Daz DeBoer's avatar Daz DeBoer
Browse files

Add support for read-only caching in v2

parent d9cc0aec
Loading
Loading
Loading
Loading
+3 −3
Changes for action.yml: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -17,15 +17,15 @@ inputs:
    description: Gradle command line arguments, see gradle --help
    required: false
  distributions-cache-enabled:
    description: Whether caching downloaded Gradle distributions is enabled or not, default to 'true'
    description: Whether caching downloaded Gradle distributions is enabled or not. Valid values are 'true' (default), 'false', and 'read-only'.
    required: false
    default: true
  gradle-user-home-cache-enabled:
    description: Whether caching of state in Gradle User Home is enabled, default to 'true'
    description: Whether caching of state in Gradle User Home is enabled. Valid values are 'true' (default), 'false', and 'read-only'.
    required: false
    default: true
  project-dot-gradle-cache-enabled:
    description: Whether caching of state in project .gradle dir is enabled, default to 'false'
    description: Whether caching of state in project .gradle dir is enabled. Valid values are 'true' (default), 'false', and 'read-only'.
    required: false
    default: true

+8 −7
Changes for src/cache-gradle-user-home.ts: 8 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -5,8 +5,13 @@ import os from 'os'
import * as core from '@actions/core'
import * as cache from '@actions/cache'
import * as github from '@actions/github'
import {truncateArgs} from './cache-utils'
import {
    isCacheReadEnabled,
    isCacheSaveEnabled,
    truncateArgs
} from './cache-utils'

const CACHE_NAME = 'gradle-user-home'
const CACHE_PATH = [
    '~/.gradle/caches/*', // All directories in 'caches'
    '~/.gradle/notifications/*', // Prevent the re-rendering of first-use message for version
@@ -16,7 +21,7 @@ const CACHE_KEY = 'GUH_CACHE_KEY'
const CACHE_RESULT = 'GUH_CACHE_RESULT'

export async function restore(): Promise<void> {
    if (!isGradleUserHomeCacheEnabled()) return
    if (!isCacheReadEnabled(CACHE_NAME)) return

    if (gradleUserHomeExists()) {
        core.debug('Gradle User Home already exists. Not restoring from cache.')
@@ -50,7 +55,7 @@ export async function restore(): Promise<void> {
}

export async function save(): Promise<void> {
    if (!isGradleUserHomeCacheEnabled()) return
    if (!isCacheSaveEnabled(CACHE_NAME)) return

    if (!gradleUserHomeExists()) {
        core.debug('No Gradle User Home to cache.')
@@ -90,10 +95,6 @@ export async function save(): Promise<void> {
    return
}

export function isGradleUserHomeCacheEnabled(): boolean {
    return core.getBooleanInput('gradle-user-home-cache-enabled')
}

function gradleUserHomeExists(): boolean {
    // Need to check for 'caches' directory to avoid incorrect detection on MacOS agents
    const dir = path.resolve(os.homedir(), '.gradle/caches')
+8 −7
Changes for src/cache-project-dot-gradle.ts: 8 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -4,8 +4,13 @@ import fs from 'fs'
import * as core from '@actions/core'
import * as cache from '@actions/cache'
import * as github from '@actions/github'
import {truncateArgs} from './cache-utils'
import {
    isCacheReadEnabled,
    isCacheSaveEnabled,
    truncateArgs
} from './cache-utils'

const CACHE_NAME = 'project-dot-gradle'
const PATHS_TO_CACHE = [
    'configuration-cache' // Only configuration-cache is stored at present
]
@@ -13,7 +18,7 @@ const CACHE_KEY = 'PROJECT_CACHE_KEY'
const CACHE_RESULT = 'PROJECT_CACHE_RESULT'

export async function restore(rootDir: string): Promise<void> {
    if (!isProjectDotGradleCacheEnabled()) return
    if (!isCacheReadEnabled(CACHE_NAME)) return

    if (projectDotGradleDirExists(rootDir)) {
        core.debug(
@@ -48,7 +53,7 @@ export async function restore(rootDir: string): Promise<void> {
}

export async function save(rootDir: string): Promise<void> {
    if (!isProjectDotGradleCacheEnabled()) return
    if (!isCacheSaveEnabled(CACHE_NAME)) return

    if (!projectDotGradleDirExists(rootDir)) {
        core.debug('No project .gradle dir to cache.')
@@ -88,10 +93,6 @@ export async function save(rootDir: string): Promise<void> {
    return
}

function isProjectDotGradleCacheEnabled(): boolean {
    return core.getBooleanInput('project-dot-gradle-cache-enabled')
}

function getCachePath(rootDir: string): string[] {
    const dir = getProjectDotGradleDir(rootDir)
    return PATHS_TO_CACHE.map(x => path.resolve(dir, x))
+25
Changes for src/cache-utils.ts: 25 added lines, 0 removed lines.
Original line number Diff line number Diff line
import * as core from '@actions/core'

export function truncateArgs(args: string): string {
    return args.trim().replace(/\s+/g, ' ').substr(0, 400)
}

export function isCacheReadEnabled(cacheName: string): boolean {
    const configValue = getCacheEnabledValue(cacheName)
    return configValue === 'true' || configValue === 'read-only'
}

export function isCacheSaveEnabled(cacheName: string): boolean {
    const configValue = getCacheEnabledValue(cacheName)
    return configValue === 'true'
}

function getCacheEnabledValue(cacheName: string): string {
    const configValue = core
        .getInput(`${cacheName}-cache-enabled`)
        .toLowerCase()

    if (['true', 'false', 'read-only'].includes(configValue)) {
        return configValue
    }
    throw new Error(
        `Invalid cache-enabled parameter '${configValue}'. Valid values are ['true', 'false', 'read-only']`
    )
}
−8
Changes for src/post.ts: 0 added lines, 8 removed lines.
Original line number Diff line number Diff line
import * as core from '@actions/core'

import * as caches from './caches'

// Invoked by GitHub Actions
export async function run(): Promise<void> {
    if (isCacheReadOnly()) return

    await caches.save()
}

function isCacheReadOnly(): boolean {
    return core.getBooleanInput('cache-read-only')
}

run()
Loading