mirror of
https://github.com/gradle/gradle-build-action.git
synced 2024-12-25 14:16:18 +08:00
parent
e0d37eb073
commit
cc5cdb7fe0
@ -8,20 +8,21 @@ import * as exec from '@actions/exec'
|
|||||||
import {AbstractCache, hashStrings} from './cache-utils'
|
import {AbstractCache, hashStrings} from './cache-utils'
|
||||||
|
|
||||||
// Which paths under Gradle User Home should be cached
|
// Which paths under Gradle User Home should be cached
|
||||||
// TODO: This should adapt for the `GRADLE_USER_HOME` environment variable
|
const CACHE_PATH = ['caches', 'notifications']
|
||||||
// TODO: Allow the user to override / tweak this set
|
|
||||||
const CACHE_PATH = ['~/.gradle/caches', '~/.gradle/notifications']
|
|
||||||
|
|
||||||
const COMMON_ARTIFACT_CACHES = new Map([
|
const COMMON_ARTIFACT_CACHES = new Map([
|
||||||
['generated-gradle-jars', '~/.gradle/caches/*/generated-gradle-jars/*.jar'],
|
['generated-gradle-jars', 'caches/*/generated-gradle-jars/*.jar'],
|
||||||
['wrapper-zips', '~/.gradle/wrapper/dists/*/*/*.zip'],
|
['wrapper-zips', 'wrapper/dists/*/*/*.zip'],
|
||||||
['dependency-jars', '~/.gradle/caches/modules-*/files-*/**/*.jar'],
|
['dependency-jars', 'caches/modules-*/files-*/**/*.jar'],
|
||||||
['instrumented-jars', '~/.gradle/caches/jars-*/*/*.jar']
|
['instrumented-jars', 'caches/jars-*/*/*.jar']
|
||||||
])
|
])
|
||||||
|
|
||||||
export class GradleUserHomeCache extends AbstractCache {
|
export class GradleUserHomeCache extends AbstractCache {
|
||||||
constructor() {
|
private gradleUserHome: string
|
||||||
|
|
||||||
|
constructor(rootDir: string) {
|
||||||
super('gradle', 'Gradle User Home')
|
super('gradle', 'Gradle User Home')
|
||||||
|
this.gradleUserHome = this.determineGradleUserHome(rootDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
async afterRestore(): Promise<void> {
|
async afterRestore(): Promise<void> {
|
||||||
@ -32,7 +33,7 @@ export class GradleUserHomeCache extends AbstractCache {
|
|||||||
|
|
||||||
private async restoreCommonArtifacts(): Promise<void> {
|
private async restoreCommonArtifacts(): Promise<void> {
|
||||||
const processes: Promise<void>[] = []
|
const processes: Promise<void>[] = []
|
||||||
for (const [bundle, pattern] of COMMON_ARTIFACT_CACHES) {
|
for (const [bundle, pattern] of this.getCommonArtifactPaths()) {
|
||||||
const p = this.restoreCommonArtifactBundle(bundle, pattern)
|
const p = this.restoreCommonArtifactBundle(bundle, pattern)
|
||||||
// Run sequentially when debugging enabled
|
// Run sequentially when debugging enabled
|
||||||
if (this.cacheDebuggingEnabled) {
|
if (this.cacheDebuggingEnabled) {
|
||||||
@ -46,19 +47,19 @@ export class GradleUserHomeCache extends AbstractCache {
|
|||||||
|
|
||||||
private async restoreCommonArtifactBundle(
|
private async restoreCommonArtifactBundle(
|
||||||
bundle: string,
|
bundle: string,
|
||||||
pattern: string
|
artifactPath: string
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const cacheMetaFile = this.getCacheMetaFile(bundle)
|
const cacheMetaFile = this.getCacheMetaFile(bundle)
|
||||||
if (fs.existsSync(cacheMetaFile)) {
|
if (fs.existsSync(cacheMetaFile)) {
|
||||||
const cacheKey = fs.readFileSync(cacheMetaFile, 'utf-8').trim()
|
const cacheKey = fs.readFileSync(cacheMetaFile, 'utf-8').trim()
|
||||||
const restoreKey = await this.restoreCache([pattern], cacheKey)
|
const restoreKey = await this.restoreCache([artifactPath], cacheKey)
|
||||||
if (restoreKey) {
|
if (restoreKey) {
|
||||||
core.info(
|
core.info(
|
||||||
`Restored ${bundle} with key ${cacheKey} to ${pattern}`
|
`Restored ${bundle} with key ${cacheKey} to ${artifactPath}`
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
this.debug(
|
this.debug(
|
||||||
`Failed to restore ${bundle} with key ${cacheKey} to ${pattern}`
|
`Failed to restore ${bundle} with key ${cacheKey} to ${artifactPath}`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -70,7 +71,7 @@ export class GradleUserHomeCache extends AbstractCache {
|
|||||||
|
|
||||||
private getCacheMetaFile(name: string): string {
|
private getCacheMetaFile(name: string): string {
|
||||||
return path.resolve(
|
return path.resolve(
|
||||||
this.getGradleUserHome(),
|
this.gradleUserHome,
|
||||||
'caches',
|
'caches',
|
||||||
`.gradle-build-action.${name}.cache`
|
`.gradle-build-action.${name}.cache`
|
||||||
)
|
)
|
||||||
@ -80,15 +81,14 @@ export class GradleUserHomeCache extends AbstractCache {
|
|||||||
if (!this.cacheDebuggingEnabled) {
|
if (!this.cacheDebuggingEnabled) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const gradleUserHome = path.resolve(os.homedir(), '.gradle')
|
if (!fs.existsSync(this.gradleUserHome)) {
|
||||||
if (!fs.existsSync(gradleUserHome)) {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const result = await exec.getExecOutput(
|
const result = await exec.getExecOutput(
|
||||||
'du',
|
'du',
|
||||||
['-h', '-c', '-t', '5M'],
|
['-h', '-c', '-t', '5M'],
|
||||||
{
|
{
|
||||||
cwd: gradleUserHome,
|
cwd: this.gradleUserHome,
|
||||||
silent: true,
|
silent: true,
|
||||||
ignoreReturnCode: true
|
ignoreReturnCode: true
|
||||||
}
|
}
|
||||||
@ -116,7 +116,7 @@ export class GradleUserHomeCache extends AbstractCache {
|
|||||||
|
|
||||||
private async saveCommonArtifacts(): Promise<void> {
|
private async saveCommonArtifacts(): Promise<void> {
|
||||||
const processes: Promise<void>[] = []
|
const processes: Promise<void>[] = []
|
||||||
for (const [bundle, pattern] of COMMON_ARTIFACT_CACHES) {
|
for (const [bundle, pattern] of this.getCommonArtifactPaths()) {
|
||||||
const p = this.saveCommonArtifactBundle(bundle, pattern)
|
const p = this.saveCommonArtifactBundle(bundle, pattern)
|
||||||
// Run sequentially when debugging enabled
|
// Run sequentially when debugging enabled
|
||||||
if (this.cacheDebuggingEnabled) {
|
if (this.cacheDebuggingEnabled) {
|
||||||
@ -130,11 +130,11 @@ export class GradleUserHomeCache extends AbstractCache {
|
|||||||
|
|
||||||
private async saveCommonArtifactBundle(
|
private async saveCommonArtifactBundle(
|
||||||
bundle: string,
|
bundle: string,
|
||||||
pattern: string
|
artifactPath: string
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const cacheMetaFile = this.getCacheMetaFile(bundle)
|
const cacheMetaFile = this.getCacheMetaFile(bundle)
|
||||||
|
|
||||||
const globber = await glob.create(pattern)
|
const globber = await glob.create(artifactPath)
|
||||||
const commonArtifactFiles = await globber.glob()
|
const commonArtifactFiles = await globber.glob()
|
||||||
|
|
||||||
// Handle no matching files
|
// Handle no matching files
|
||||||
@ -160,7 +160,7 @@ export class GradleUserHomeCache extends AbstractCache {
|
|||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
core.info(`Caching ${bundle} with cache key: ${cacheKey}`)
|
core.info(`Caching ${bundle} with cache key: ${cacheKey}`)
|
||||||
await this.saveCache([pattern], cacheKey)
|
await this.saveCache([artifactPath], cacheKey)
|
||||||
|
|
||||||
this.debug(`Writing cache metafile: ${cacheMetaFile}`)
|
this.debug(`Writing cache metafile: ${cacheMetaFile}`)
|
||||||
fs.writeFileSync(cacheMetaFile, cacheKey)
|
fs.writeFileSync(cacheMetaFile, cacheKey)
|
||||||
@ -176,17 +176,31 @@ export class GradleUserHomeCache extends AbstractCache {
|
|||||||
return `${cacheKeyPrefix}${bundle}-${key}`
|
return `${cacheKeyPrefix}${bundle}-${key}`
|
||||||
}
|
}
|
||||||
|
|
||||||
protected getGradleUserHome(): string {
|
protected determineGradleUserHome(rootDir: string): string {
|
||||||
|
const customGradleUserHome = process.env['GRADLE_USER_HOME']
|
||||||
|
if (customGradleUserHome) {
|
||||||
|
return path.resolve(rootDir, customGradleUserHome)
|
||||||
|
}
|
||||||
|
|
||||||
return path.resolve(os.homedir(), '.gradle')
|
return path.resolve(os.homedir(), '.gradle')
|
||||||
}
|
}
|
||||||
|
|
||||||
protected cacheOutputExists(): boolean {
|
protected cacheOutputExists(): boolean {
|
||||||
// Need to check for 'caches' directory to avoid incorrect detection on MacOS agents
|
// Need to check for 'caches' directory to avoid incorrect detection on MacOS agents
|
||||||
const dir = path.resolve(this.getGradleUserHome(), 'caches')
|
const dir = path.resolve(this.gradleUserHome, 'caches')
|
||||||
return fs.existsSync(dir)
|
return fs.existsSync(dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
protected getCachePath(): string[] {
|
protected getCachePath(): string[] {
|
||||||
return CACHE_PATH
|
return CACHE_PATH.map(x => path.resolve(this.gradleUserHome, x))
|
||||||
|
}
|
||||||
|
|
||||||
|
private getCommonArtifactPaths(): Map<string, string> {
|
||||||
|
return new Map(
|
||||||
|
Array.from(COMMON_ARTIFACT_CACHES, ([key, value]) => [
|
||||||
|
key,
|
||||||
|
path.resolve(this.gradleUserHome, value)
|
||||||
|
])
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ export async function restore(buildRootDirectory: string): Promise<void> {
|
|||||||
await core.group('Restore Gradle state from cache', async () => {
|
await core.group('Restore Gradle state from cache', async () => {
|
||||||
core.saveState(BUILD_ROOT_DIR, buildRootDirectory)
|
core.saveState(BUILD_ROOT_DIR, buildRootDirectory)
|
||||||
return Promise.all([
|
return Promise.all([
|
||||||
new GradleUserHomeCache().restore(),
|
new GradleUserHomeCache(buildRootDirectory).restore(),
|
||||||
new ProjectDotGradleCache(buildRootDirectory).restore()
|
new ProjectDotGradleCache(buildRootDirectory).restore()
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
@ -29,7 +29,7 @@ export async function save(): Promise<void> {
|
|||||||
await core.group('Caching Gradle state', async () => {
|
await core.group('Caching Gradle state', async () => {
|
||||||
const buildRootDirectory = core.getState(BUILD_ROOT_DIR)
|
const buildRootDirectory = core.getState(BUILD_ROOT_DIR)
|
||||||
return Promise.all([
|
return Promise.all([
|
||||||
new GradleUserHomeCache().save(),
|
new GradleUserHomeCache(buildRootDirectory).save(),
|
||||||
new ProjectDotGradleCache(buildRootDirectory).save()
|
new ProjectDotGradleCache(buildRootDirectory).save()
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user