mirror of
				https://github.com/gradle/gradle-build-action.git
				synced 2025-11-04 09:58:56 +08:00 
			
		
		
		
	Cache wrapper installation
This commit is contained in:
		
							
								
								
									
										52
									
								
								src/cache.ts
									
									
									
									
									
								
							
							
						
						
									
										52
									
								
								src/cache.ts
									
									
									
									
									
								
							@@ -1,9 +1,11 @@
 | 
			
		||||
import * as core from '@actions/core'
 | 
			
		||||
import * as cache from '@actions/cache'
 | 
			
		||||
import * as path from 'path'
 | 
			
		||||
import * as fs from 'fs'
 | 
			
		||||
 | 
			
		||||
const WRAPPER_CACHE_KEY = 'WRAPPER_CACHE_KEY'
 | 
			
		||||
const WRAPPER_CACHE_PATH = 'WRAPPER_CACHE_PATH'
 | 
			
		||||
const WRAPPER_CACHE_RESULT = 'WRAPPER_CACHE_RESULT'
 | 
			
		||||
 | 
			
		||||
export async function restoreCachedWrapperDist(
 | 
			
		||||
    executableDirectory: string
 | 
			
		||||
@@ -14,21 +16,63 @@ export async function restoreCachedWrapperDist(
 | 
			
		||||
            'gradle/wrapper/gradle-wrapper.properties'
 | 
			
		||||
        )
 | 
			
		||||
    )
 | 
			
		||||
    if (!wrapperSlug) return
 | 
			
		||||
 | 
			
		||||
    const wrapperCacheKey = `wrapper-${wrapperSlug}`
 | 
			
		||||
    const wrapperCachePath = path.join(
 | 
			
		||||
        process.env.HOME!,
 | 
			
		||||
        `.gradle/wrapper/dists/gradle-${wrapperSlug}`
 | 
			
		||||
    )
 | 
			
		||||
    core.info(`${WRAPPER_CACHE_KEY} = ${wrapperCacheKey}`)
 | 
			
		||||
    core.info(`${WRAPPER_CACHE_PATH} = ${wrapperCachePath}`)
 | 
			
		||||
 | 
			
		||||
    core.saveState(WRAPPER_CACHE_KEY, wrapperCacheKey)
 | 
			
		||||
    core.saveState(WRAPPER_CACHE_PATH, wrapperCachePath)
 | 
			
		||||
 | 
			
		||||
    const restoredKey = await cache.restoreCache(
 | 
			
		||||
        [wrapperCachePath],
 | 
			
		||||
        wrapperCacheKey
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    if (!restoredKey) {
 | 
			
		||||
        core.info(
 | 
			
		||||
            'Wrapper installation cache not found, expect a Gradle distribution download.'
 | 
			
		||||
        )
 | 
			
		||||
        return
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    core.saveState(WRAPPER_CACHE_RESULT, restoredKey)
 | 
			
		||||
    core.info(`Wrapper installation restored from cache key: ${restoredKey}`)
 | 
			
		||||
    return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export async function cacheWrapperDist(): Promise<void> {
 | 
			
		||||
    core.info(`${WRAPPER_CACHE_KEY} = ${core.getState(WRAPPER_CACHE_KEY)}`)
 | 
			
		||||
    core.info(`${WRAPPER_CACHE_PATH} = ${core.getState(WRAPPER_CACHE_PATH)}`)
 | 
			
		||||
    const cacheKey = core.getState(WRAPPER_CACHE_KEY)
 | 
			
		||||
    const cachePath = core.getState(WRAPPER_CACHE_PATH)
 | 
			
		||||
    const cacheResult = core.getState(WRAPPER_CACHE_RESULT)
 | 
			
		||||
 | 
			
		||||
    if (!cachePath) {
 | 
			
		||||
        core.debug('No wrapper installation to cache.')
 | 
			
		||||
        return
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (cacheResult && cacheKey === cacheResult) {
 | 
			
		||||
        core.info(
 | 
			
		||||
            `Wrapper installation cache hit occurred on the cache key ${cacheKey}, 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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -56,9 +56,11 @@ async function resolveGradleExecutable(baseDirectory: string): Promise<string> {
 | 
			
		||||
 | 
			
		||||
function resolveBuildRootDirectory(baseDirectory: string): string {
 | 
			
		||||
    const buildRootDirectory = inputOrNull('build-root-directory')
 | 
			
		||||
    return buildRootDirectory === null
 | 
			
		||||
        ? path.resolve(baseDirectory)
 | 
			
		||||
        : path.resolve(baseDirectory, buildRootDirectory)
 | 
			
		||||
    const resolvedBuildRootDirectory =
 | 
			
		||||
        buildRootDirectory === null
 | 
			
		||||
            ? path.resolve(baseDirectory)
 | 
			
		||||
            : path.resolve(baseDirectory, buildRootDirectory)
 | 
			
		||||
    return resolvedBuildRootDirectory
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function parseCommandLineArguments(): string[] {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,9 +1,7 @@
 | 
			
		||||
import * as core from '@actions/core'
 | 
			
		||||
import * as cache from './cache'
 | 
			
		||||
 | 
			
		||||
// Invoked by GitHub Actions
 | 
			
		||||
export async function run(): Promise<void> {
 | 
			
		||||
    core.info('POST Gradle Command Action')
 | 
			
		||||
    await cache.cacheWrapperDist()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user