mirror of
https://github.com/gradle/gradle-build-action
synced 2024-11-27 20:12:22 +00:00
Merge pull request #109 from gradle/dd/rc21
Fix issues for 2.0-rc.2 - Support multi-line strings for cache-tuning parameters #106 - Include all downloaded files in `dependencies` bundle #100 - Only restore configuration-cache if Gradle User Home is fully restored #107
This commit is contained in:
commit
613f4ec588
18 changed files with 263 additions and 260 deletions
|
@ -11,7 +11,7 @@
|
|||
"eslint-comments/no-use": "off",
|
||||
"import/no-namespace": "off",
|
||||
"no-unused-vars": "off",
|
||||
"@typescript-eslint/no-unused-vars": "error",
|
||||
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
|
||||
"@typescript-eslint/explicit-member-accessibility": ["error", {"accessibility": "no-public"}],
|
||||
"@typescript-eslint/no-require-imports": "error",
|
||||
"@typescript-eslint/array-type": "error",
|
||||
|
|
16
.github/workflows/integTest-caching-config.yml
vendored
16
.github/workflows/integTest-caching-config.yml
vendored
|
@ -28,13 +28,15 @@ jobs:
|
|||
# Add "wrapper" to main cache entry and remove 'wrapper-zips' bundle
|
||||
# Exclude build-cache from main cache entry
|
||||
gradle-home-cache-includes: |
|
||||
["caches", "notifications", "wrapper"]
|
||||
caches
|
||||
notifications
|
||||
wrapper
|
||||
gradle-home-cache-excludes: |
|
||||
["caches/build-cache-1"]
|
||||
caches/build-cache-1
|
||||
gradle-home-cache-artifact-bundles: |
|
||||
[
|
||||
["generated-gradle-jars", "caches/*/generated-gradle-jars/*.jar"],
|
||||
["dependency-jars", "caches/modules-*/files-*/**/*.jar"],
|
||||
["dependencies", "caches/modules-*/files-*/*/*/*/*/"],
|
||||
["instrumented-jars", "caches/jars-*/*/"],
|
||||
["kotlin-dsl", "caches/*/kotlin-dsl/*/*/"]
|
||||
]
|
||||
|
@ -57,13 +59,15 @@ jobs:
|
|||
cache-read-only: true
|
||||
# Need the same configuration when restoring state from cache
|
||||
gradle-home-cache-includes: |
|
||||
["caches", "notifications", "wrapper"]
|
||||
caches
|
||||
notifications
|
||||
wrapper
|
||||
gradle-home-cache-excludes: |
|
||||
["caches/build-cache-1"]
|
||||
caches/build-cache-1
|
||||
gradle-home-cache-artifact-bundles: |
|
||||
[
|
||||
["generated-gradle-jars", "caches/*/generated-gradle-jars/*.jar"],
|
||||
["dependency-jars", "caches/modules-*/files-*/**/*.jar"],
|
||||
["dependencies", "caches/modules-*/files-*/*/*/*/*/"],
|
||||
["instrumented-jars", "caches/jars-*/*/"],
|
||||
["kotlin-dsl", "caches/*/kotlin-dsl/*/*/"]
|
||||
]
|
||||
|
|
65
.github/workflows/integTest-caching-configuration-cache.yml
vendored
Normal file
65
.github/workflows/integTest-caching-configuration-cache.yml
vendored
Normal file
|
@ -0,0 +1,65 @@
|
|||
name: Test save/restore configuration-cache state
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
push:
|
||||
workflow_dispatch:
|
||||
|
||||
env:
|
||||
GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}-
|
||||
GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED: true
|
||||
|
||||
jobs:
|
||||
# Run initial Gradle builds to push initial cache entries
|
||||
# These builds should start fresh without cache hits, due to the seed injected into the cache key above.
|
||||
seed-build:
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest, windows-latest]
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v2
|
||||
- name: Build with configuration-cache enabled
|
||||
uses: ./
|
||||
with:
|
||||
build-root-directory: __tests__/samples/groovy-dsl
|
||||
arguments: test --configuration-cache
|
||||
|
||||
# Test that the project-dot-gradle cache will cache and restore configuration-cache
|
||||
configuration-cache:
|
||||
needs: seed-build
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest, windows-latest]
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v2
|
||||
- name: Execute Gradle build and verify cached configuration
|
||||
uses: ./
|
||||
env:
|
||||
VERIFY_CACHED_CONFIGURATION: true
|
||||
with:
|
||||
build-root-directory: __tests__/samples/groovy-dsl
|
||||
arguments: test --configuration-cache
|
||||
cache-read-only: true
|
||||
|
||||
# Check that the build can run when no bundles are restored
|
||||
no-bundles-restored:
|
||||
needs: seed-build
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest, windows-latest]
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v2
|
||||
- name: Execute Gradle build with no cache artifact bundles restored
|
||||
uses: ./
|
||||
with:
|
||||
build-root-directory: __tests__/samples/groovy-dsl
|
||||
arguments: test --configuration-cache
|
||||
cache-read-only: true
|
||||
gradle-home-cache-artifact-bundles: '[]'
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
name: Test caching
|
||||
name: Test save/restore Gradle Home directory
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
|
@ -24,11 +24,6 @@ jobs:
|
|||
with:
|
||||
build-root-directory: __tests__/samples/groovy-dsl
|
||||
arguments: test
|
||||
- name: Build with configuration-cache enabled
|
||||
uses: ./
|
||||
with:
|
||||
build-root-directory: __tests__/samples/groovy-dsl
|
||||
arguments: test --configuration-cache
|
||||
|
||||
# Test that the gradle-user-home cache will cache dependencies, by running build with --offline
|
||||
dependencies-cache:
|
||||
|
@ -64,25 +59,6 @@ jobs:
|
|||
arguments: test -DverifyCachedBuild=true
|
||||
cache-read-only: true
|
||||
|
||||
# Test that the project-dot-gradle cache will cache and restore configuration-cache
|
||||
configuration-cache:
|
||||
needs: seed-build
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest, windows-latest]
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v2
|
||||
- name: Execute Gradle build and verify cached configuration
|
||||
uses: ./
|
||||
env:
|
||||
VERIFY_CACHED_CONFIGURATION: true
|
||||
with:
|
||||
build-root-directory: __tests__/samples/groovy-dsl
|
||||
arguments: test --configuration-cache
|
||||
cache-read-only: true
|
||||
|
||||
# Check that the build can run when no bundles are restored
|
||||
no-bundles-restored:
|
||||
needs: seed-build
|
|
@ -1,5 +1,5 @@
|
|||
{
|
||||
"printWidth": 80,
|
||||
"printWidth": 120,
|
||||
"tabWidth": 4,
|
||||
"useTabs": false,
|
||||
"semi": false,
|
||||
|
|
|
@ -191,10 +191,12 @@ The contents to be cached can be fine tuned by including and excluding certain p
|
|||
```yaml
|
||||
# Cache downloaded JDKs in addition to the default directories.
|
||||
gradle-home-cache-includes: |
|
||||
["caches", "notifications", "jdks"]
|
||||
caches
|
||||
notifications
|
||||
jdks
|
||||
# Exclude the local build-cache from the directories cached.
|
||||
gradle-home-cache-excludes: |
|
||||
["caches/build-cache-1"]
|
||||
caches/build-cache-1
|
||||
```
|
||||
|
||||
You can specify any number of fixed paths or patterns to include or exclude.
|
||||
|
|
|
@ -36,13 +36,13 @@ inputs:
|
|||
description: Paths within Gradle User Home to cache.
|
||||
required: false
|
||||
default: |
|
||||
["caches", "notifications"]
|
||||
caches
|
||||
notifications
|
||||
|
||||
gradle-home-cache-excludes:
|
||||
description: Paths within Gradle User Home to exclude from cache.
|
||||
required: false
|
||||
default: |
|
||||
[]
|
||||
|
||||
# e.g. Use the following setting to prevent the local build cache from being saved/restored
|
||||
# gradle-home-cache-excludes: |
|
||||
# ["caches/build-cache-1"]
|
||||
|
@ -62,7 +62,7 @@ inputs:
|
|||
[
|
||||
["generated-gradle-jars", "caches/*/generated-gradle-jars/*.jar"],
|
||||
["wrapper-zips", "wrapper/dists/*/*/*.zip"],
|
||||
["dependency-jars", "caches/modules-*/files-*/**/*.jar"],
|
||||
["dependencies", "caches/modules-*/files-*/*/*/*/*/"],
|
||||
["instrumented-jars", "caches/jars-*/*/"],
|
||||
["kotlin-dsl", "caches/*/kotlin-dsl/*/*/"]
|
||||
]
|
||||
|
|
2
dist/main/index.js
vendored
2
dist/main/index.js
vendored
File diff suppressed because one or more lines are too long
2
dist/main/index.js.map
vendored
2
dist/main/index.js.map
vendored
File diff suppressed because one or more lines are too long
2
dist/post/index.js
vendored
2
dist/post/index.js
vendored
File diff suppressed because one or more lines are too long
2
dist/post/index.js.map
vendored
2
dist/post/index.js.map
vendored
File diff suppressed because one or more lines are too long
|
@ -7,6 +7,8 @@ import * as exec from '@actions/exec'
|
|||
|
||||
import {
|
||||
AbstractCache,
|
||||
CacheEntryReport,
|
||||
CachingReport,
|
||||
getCacheKeyPrefix,
|
||||
hashFileNames,
|
||||
tryDelete
|
||||
|
@ -26,21 +28,38 @@ export class GradleUserHomeCache extends AbstractCache {
|
|||
this.gradleUserHome = this.determineGradleUserHome(rootDir)
|
||||
}
|
||||
|
||||
async afterRestore(): Promise<void> {
|
||||
async afterRestore(report: CachingReport): Promise<void> {
|
||||
await this.reportGradleUserHomeSize('as restored from cache')
|
||||
await this.restoreArtifactBundles()
|
||||
await this.restoreArtifactBundles(report)
|
||||
await this.reportGradleUserHomeSize('after restoring common artifacts')
|
||||
}
|
||||
|
||||
private async restoreArtifactBundles(): Promise<void> {
|
||||
private async restoreArtifactBundles(report: CachingReport): Promise<void> {
|
||||
const processes: Promise<void>[] = []
|
||||
for (const [bundle, pattern] of this.getArtifactBundles()) {
|
||||
const p = this.restoreArtifactBundle(bundle, pattern)
|
||||
// Run sequentially when debugging enabled
|
||||
if (this.cacheDebuggingEnabled) {
|
||||
await p
|
||||
|
||||
const bundleMetaFiles = await this.getBundleMetaFiles()
|
||||
const bundlePatterns = this.getArtifactBundles()
|
||||
|
||||
// Iterate over all bundle meta files and try to restore
|
||||
for (const bundleMetaFile of bundleMetaFiles) {
|
||||
const bundle = path.basename(bundleMetaFile, '.cache')
|
||||
const bundleEntryReport = report.addEntryReport(bundle)
|
||||
const bundlePattern = bundlePatterns.get(bundle)
|
||||
|
||||
// Handle case where the 'artifactBundlePatterns' have been changed
|
||||
if (bundlePattern === undefined) {
|
||||
core.info(`Found bundle metafile for ${bundle} but no such bundle defined`)
|
||||
bundleEntryReport.markRequested('BUNDLE_NOT_CONFIGURED')
|
||||
tryDelete(bundleMetaFile)
|
||||
return
|
||||
} else {
|
||||
const p = this.restoreArtifactBundle(bundle, bundlePattern, bundleMetaFile, bundleEntryReport)
|
||||
// Run sequentially when debugging enabled
|
||||
if (this.cacheDebuggingEnabled) {
|
||||
await p
|
||||
}
|
||||
processes.push(p)
|
||||
}
|
||||
processes.push(p)
|
||||
}
|
||||
|
||||
await Promise.all(processes)
|
||||
|
@ -48,25 +67,20 @@ export class GradleUserHomeCache extends AbstractCache {
|
|||
|
||||
private async restoreArtifactBundle(
|
||||
bundle: string,
|
||||
artifactPath: string
|
||||
bundlePattern: string,
|
||||
bundleMetaFile: string,
|
||||
report: CacheEntryReport
|
||||
): Promise<void> {
|
||||
const bundleMetaFile = this.getBundleMetaFile(bundle)
|
||||
if (fs.existsSync(bundleMetaFile)) {
|
||||
const cacheKey = fs.readFileSync(bundleMetaFile, 'utf-8').trim()
|
||||
const restoreKey = await this.restoreCache([artifactPath], cacheKey)
|
||||
if (restoreKey) {
|
||||
core.info(
|
||||
`Restored ${bundle} with key ${cacheKey} to ${artifactPath}`
|
||||
)
|
||||
} else {
|
||||
this.debug(
|
||||
`Did not restore ${bundle} with key ${cacheKey} to ${artifactPath}`
|
||||
)
|
||||
}
|
||||
const cacheKey = fs.readFileSync(bundleMetaFile, 'utf-8').trim()
|
||||
report.markRequested(cacheKey)
|
||||
|
||||
const restoredKey = await this.restoreCache([bundlePattern], cacheKey)
|
||||
if (restoredKey) {
|
||||
core.info(`Restored ${bundle} with key ${cacheKey} to ${bundlePattern}`)
|
||||
report.markRestored(restoredKey)
|
||||
} else {
|
||||
this.debug(
|
||||
`No metafile found to restore ${bundle}: ${bundleMetaFile}`
|
||||
)
|
||||
core.info(`Did not restore ${bundle} with key ${cacheKey} to ${bundlePattern}`)
|
||||
tryDelete(bundleMetaFile)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -74,6 +88,13 @@ export class GradleUserHomeCache extends AbstractCache {
|
|||
return path.resolve(this.gradleUserHome, META_FILE_DIR, `${name}.cache`)
|
||||
}
|
||||
|
||||
private async getBundleMetaFiles(): Promise<string[]> {
|
||||
const metaFiles = path.resolve(this.gradleUserHome, META_FILE_DIR, '*.cache')
|
||||
const globber = await glob.create(metaFiles)
|
||||
const bundleFiles = await globber.glob()
|
||||
return bundleFiles
|
||||
}
|
||||
|
||||
async beforeSave(): Promise<void> {
|
||||
await this.reportGradleUserHomeSize('before saving common artifacts')
|
||||
this.removeExcludedPaths()
|
||||
|
@ -84,12 +105,8 @@ export class GradleUserHomeCache extends AbstractCache {
|
|||
}
|
||||
|
||||
private removeExcludedPaths(): void {
|
||||
const rawPaths: string[] = JSON.parse(
|
||||
core.getInput(EXCLUDE_PATHS_PARAMETER)
|
||||
)
|
||||
const resolvedPaths = rawPaths.map(x =>
|
||||
path.resolve(this.gradleUserHome, x)
|
||||
)
|
||||
const rawPaths: string[] = core.getMultilineInput(EXCLUDE_PATHS_PARAMETER)
|
||||
const resolvedPaths = rawPaths.map(x => path.resolve(this.gradleUserHome, x))
|
||||
|
||||
for (const p of resolvedPaths) {
|
||||
this.debug(`Deleting excluded path: ${p}`)
|
||||
|
@ -111,10 +128,7 @@ export class GradleUserHomeCache extends AbstractCache {
|
|||
await Promise.all(processes)
|
||||
}
|
||||
|
||||
private async saveArtifactBundle(
|
||||
bundle: string,
|
||||
artifactPath: string
|
||||
): Promise<void> {
|
||||
private async saveArtifactBundle(bundle: string, artifactPath: string): Promise<void> {
|
||||
const bundleMetaFile = this.getBundleMetaFile(bundle)
|
||||
|
||||
const globber = await glob.create(artifactPath, {
|
||||
|
@ -138,9 +152,7 @@ export class GradleUserHomeCache extends AbstractCache {
|
|||
const cacheKey = this.createCacheKey(bundle, bundleFiles)
|
||||
|
||||
if (previouslyRestoredKey === cacheKey) {
|
||||
this.debug(
|
||||
`No change to previously restored ${bundle}. Not caching.`
|
||||
)
|
||||
this.debug(`No change to previously restored ${bundle}. Not caching.`)
|
||||
} else {
|
||||
core.info(`Caching ${bundle} with cache key: ${cacheKey}`)
|
||||
await this.saveCache([artifactPath], cacheKey)
|
||||
|
@ -154,14 +166,10 @@ export class GradleUserHomeCache extends AbstractCache {
|
|||
|
||||
protected createCacheKey(bundle: string, files: string[]): string {
|
||||
const cacheKeyPrefix = getCacheKeyPrefix()
|
||||
const relativeFiles = files.map(x =>
|
||||
path.relative(this.gradleUserHome, x)
|
||||
)
|
||||
const relativeFiles = files.map(x => path.relative(this.gradleUserHome, x))
|
||||
const key = hashFileNames(relativeFiles)
|
||||
|
||||
this.debug(
|
||||
`Generating cache key for ${bundle} from files: ${relativeFiles}`
|
||||
)
|
||||
this.debug(`Generating cache key for ${bundle} from files: ${relativeFiles}`)
|
||||
|
||||
return `${cacheKeyPrefix}${bundle}-${key}`
|
||||
}
|
||||
|
@ -193,9 +201,7 @@ export class GradleUserHomeCache extends AbstractCache {
|
|||
}
|
||||
|
||||
protected getCachePath(): string[] {
|
||||
const rawPaths: string[] = JSON.parse(
|
||||
core.getInput(INCLUDE_PATHS_PARAMETER)
|
||||
)
|
||||
const rawPaths: string[] = core.getMultilineInput(INCLUDE_PATHS_PARAMETER)
|
||||
rawPaths.push(META_FILE_DIR)
|
||||
const resolvedPaths = rawPaths.map(x => this.resolveCachePath(x))
|
||||
this.debug(`Using cache paths: ${resolvedPaths}`)
|
||||
|
@ -211,19 +217,10 @@ export class GradleUserHomeCache extends AbstractCache {
|
|||
}
|
||||
|
||||
private getArtifactBundles(): Map<string, string> {
|
||||
const artifactBundleDefinition = core.getInput(
|
||||
ARTIFACT_BUNDLES_PARAMETER
|
||||
)
|
||||
this.debug(
|
||||
`Using artifact bundle definition: ${artifactBundleDefinition}`
|
||||
)
|
||||
const artifactBundleDefinition = core.getInput(ARTIFACT_BUNDLES_PARAMETER)
|
||||
this.debug(`Using artifact bundle definition: ${artifactBundleDefinition}`)
|
||||
const artifactBundles = JSON.parse(artifactBundleDefinition)
|
||||
return new Map(
|
||||
Array.from(artifactBundles, ([key, value]) => [
|
||||
key,
|
||||
path.resolve(this.gradleUserHome, value)
|
||||
])
|
||||
)
|
||||
return new Map(Array.from(artifactBundles, ([key, value]) => [key, path.resolve(this.gradleUserHome, value)]))
|
||||
}
|
||||
|
||||
private async reportGradleUserHomeSize(label: string): Promise<void> {
|
||||
|
@ -233,15 +230,11 @@ export class GradleUserHomeCache extends AbstractCache {
|
|||
if (!fs.existsSync(this.gradleUserHome)) {
|
||||
return
|
||||
}
|
||||
const result = await exec.getExecOutput(
|
||||
'du',
|
||||
['-h', '-c', '-t', '5M'],
|
||||
{
|
||||
cwd: this.gradleUserHome,
|
||||
silent: true,
|
||||
ignoreReturnCode: true
|
||||
}
|
||||
)
|
||||
const result = await exec.getExecOutput('du', ['-h', '-c', '-t', '5M'], {
|
||||
cwd: this.gradleUserHome,
|
||||
silent: true,
|
||||
ignoreReturnCode: true
|
||||
})
|
||||
|
||||
core.info(`Gradle User Home (directories >5M): ${label}`)
|
||||
|
||||
|
|
|
@ -5,6 +5,8 @@ import * as crypto from 'crypto'
|
|||
import * as path from 'path'
|
||||
import * as fs from 'fs'
|
||||
|
||||
const CACHE_PROTOCOL_VERSION = 'v4-'
|
||||
|
||||
const CACHE_DISABLED_PARAMETER = 'cache-disabled'
|
||||
const CACHE_READONLY_PARAMETER = 'cache-read-only'
|
||||
const JOB_CONTEXT_PARAMETER = 'workflow-job-context'
|
||||
|
@ -25,7 +27,7 @@ export function isCacheDebuggingEnabled(): boolean {
|
|||
|
||||
export function getCacheKeyPrefix(): string {
|
||||
// Prefix can be used to force change all cache keys (defaults to cache protocol version)
|
||||
return process.env[CACHE_PREFIX_VAR] || 'v3-'
|
||||
return process.env[CACHE_PREFIX_VAR] || CACHE_PROTOCOL_VERSION
|
||||
}
|
||||
|
||||
function generateCacheKey(cacheName: string): CacheKey {
|
||||
|
@ -44,11 +46,7 @@ function generateCacheKey(cacheName: string): CacheKey {
|
|||
// Exact match on Git SHA
|
||||
const cacheKey = `${cacheKeyForJobContext}-${github.context.sha}`
|
||||
|
||||
return new CacheKey(cacheKey, [
|
||||
cacheKeyForJobContext,
|
||||
cacheKeyForJob,
|
||||
cacheKeyForOs
|
||||
])
|
||||
return new CacheKey(cacheKey, [cacheKeyForJobContext, cacheKeyForJob, cacheKeyForOs])
|
||||
}
|
||||
|
||||
function determineJobContext(): string {
|
||||
|
@ -66,9 +64,7 @@ export function hashStrings(values: string[]): string {
|
|||
}
|
||||
|
||||
export function hashFileNames(fileNames: string[]): string {
|
||||
return hashStrings(
|
||||
fileNames.map(x => x.replace(new RegExp(`\\${path.sep}`, 'g'), '/'))
|
||||
)
|
||||
return hashStrings(fileNames.map(x => x.replace(new RegExp(`\\${path.sep}`, 'g'), '/')))
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -109,6 +105,48 @@ class CacheKey {
|
|||
}
|
||||
}
|
||||
|
||||
export class CachingReport {
|
||||
cacheEntryReports: CacheEntryReport[] = []
|
||||
|
||||
get fullyRestored(): boolean {
|
||||
return this.cacheEntryReports.every(x => !x.wasRequestedButNotRestored())
|
||||
}
|
||||
|
||||
addEntryReport(name: string): CacheEntryReport {
|
||||
const report = new CacheEntryReport(name)
|
||||
this.cacheEntryReports.push(report)
|
||||
return report
|
||||
}
|
||||
}
|
||||
|
||||
export class CacheEntryReport {
|
||||
entryName: string
|
||||
requestedKey: string | undefined
|
||||
requestedRestoreKeys: string[] | undefined
|
||||
restoredKey: string | undefined
|
||||
restoredSize: number | undefined
|
||||
|
||||
savedKey: string | undefined
|
||||
savedSize: number | undefined
|
||||
|
||||
constructor(entryName: string) {
|
||||
this.entryName = entryName
|
||||
}
|
||||
|
||||
wasRequestedButNotRestored(): boolean {
|
||||
return this.requestedKey !== undefined && this.restoredKey === undefined
|
||||
}
|
||||
|
||||
markRequested(key: string, restoreKeys: string[] = []): void {
|
||||
this.requestedKey = key
|
||||
this.requestedRestoreKeys = restoreKeys
|
||||
}
|
||||
|
||||
markRestored(key: string): void {
|
||||
this.restoredKey = key
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class AbstractCache {
|
||||
private cacheName: string
|
||||
private cacheDescription: string
|
||||
|
@ -125,17 +163,15 @@ export abstract class AbstractCache {
|
|||
this.cacheDebuggingEnabled = isCacheDebuggingEnabled()
|
||||
}
|
||||
|
||||
async restore(): Promise<void> {
|
||||
async restore(report: CachingReport): Promise<void> {
|
||||
if (this.cacheOutputExists()) {
|
||||
core.info(
|
||||
`${this.cacheDescription} already exists. Not restoring from cache.`
|
||||
)
|
||||
core.info(`${this.cacheDescription} already exists. Not restoring from cache.`)
|
||||
return
|
||||
}
|
||||
|
||||
const cacheKey = generateCacheKey(this.cacheName)
|
||||
|
||||
core.saveState(this.cacheKeyStateKey, cacheKey.key)
|
||||
const cacheKey = this.prepareCacheKey()
|
||||
const entryReport = report.addEntryReport(this.cacheName)
|
||||
entryReport.markRequested(cacheKey.key, cacheKey.restoreKeys)
|
||||
|
||||
this.debug(
|
||||
`Requesting ${this.cacheDescription} with
|
||||
|
@ -143,34 +179,29 @@ export abstract class AbstractCache {
|
|||
restoreKeys:[${cacheKey.restoreKeys}]`
|
||||
)
|
||||
|
||||
const cacheResult = await this.restoreCache(
|
||||
this.getCachePath(),
|
||||
cacheKey.key,
|
||||
cacheKey.restoreKeys
|
||||
)
|
||||
const cacheResult = await this.restoreCache(this.getCachePath(), cacheKey.key, cacheKey.restoreKeys)
|
||||
|
||||
if (!cacheResult) {
|
||||
core.info(
|
||||
`${this.cacheDescription} cache not found. Will start with empty.`
|
||||
)
|
||||
core.info(`${this.cacheDescription} cache not found. Will start with empty.`)
|
||||
return
|
||||
}
|
||||
|
||||
core.saveState(this.cacheResultStateKey, cacheResult)
|
||||
|
||||
core.info(
|
||||
`Restored ${this.cacheDescription} from cache key: ${cacheResult}`
|
||||
)
|
||||
entryReport.markRestored(cacheResult)
|
||||
core.info(`Restored ${this.cacheDescription} from cache key: ${cacheResult}`)
|
||||
|
||||
try {
|
||||
await this.afterRestore()
|
||||
await this.afterRestore(report)
|
||||
} catch (error) {
|
||||
core.warning(
|
||||
`Restore ${this.cacheDescription} failed in 'afterRestore': ${error}`
|
||||
)
|
||||
core.warning(`Restore ${this.cacheDescription} failed in 'afterRestore': ${error}`)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
prepareCacheKey(): CacheKey {
|
||||
const cacheKey = generateCacheKey(this.cacheName)
|
||||
|
||||
core.saveState(this.cacheKeyStateKey, cacheKey.key)
|
||||
return cacheKey
|
||||
}
|
||||
|
||||
protected async restoreCache(
|
||||
|
@ -179,11 +210,7 @@ export abstract class AbstractCache {
|
|||
cacheRestoreKeys: string[] = []
|
||||
): Promise<string | undefined> {
|
||||
try {
|
||||
return await cache.restoreCache(
|
||||
cachePath,
|
||||
cacheKey,
|
||||
cacheRestoreKeys
|
||||
)
|
||||
return await cache.restoreCache(cachePath, cacheKey, cacheRestoreKeys)
|
||||
} catch (error) {
|
||||
if (error instanceof cache.ValidationError) {
|
||||
// Validation errors should fail the build action
|
||||
|
@ -195,7 +222,7 @@ export abstract class AbstractCache {
|
|||
}
|
||||
}
|
||||
|
||||
protected async afterRestore(): Promise<void> {}
|
||||
protected async afterRestore(_report: CachingReport): Promise<void> {}
|
||||
|
||||
async save(): Promise<void> {
|
||||
if (!this.cacheOutputExists()) {
|
||||
|
@ -207,31 +234,23 @@ export abstract class AbstractCache {
|
|||
const cacheResult = core.getState(this.cacheResultStateKey)
|
||||
|
||||
if (!cacheKey) {
|
||||
this.debug(
|
||||
`${this.cacheDescription} existed prior to cache restore. Not saving.`
|
||||
)
|
||||
this.debug(`${this.cacheDescription} existed prior to cache restore. Not saving.`)
|
||||
return
|
||||
}
|
||||
|
||||
if (cacheResult && cacheKey === cacheResult) {
|
||||
core.info(
|
||||
`Cache hit occurred on the cache key ${cacheKey}, not saving cache.`
|
||||
)
|
||||
core.info(`Cache hit occurred on the cache key ${cacheKey}, not saving cache.`)
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
await this.beforeSave()
|
||||
} catch (error) {
|
||||
core.warning(
|
||||
`Save ${this.cacheDescription} failed in 'beforeSave': ${error}`
|
||||
)
|
||||
core.warning(`Save ${this.cacheDescription} failed in 'beforeSave': ${error}`)
|
||||
return
|
||||
}
|
||||
|
||||
core.info(
|
||||
`Caching ${this.cacheDescription} with cache key: ${cacheKey}`
|
||||
)
|
||||
core.info(`Caching ${this.cacheDescription} with cache key: ${cacheKey}`)
|
||||
const cachePath = this.getCachePath()
|
||||
await this.saveCache(cachePath, cacheKey)
|
||||
|
||||
|
@ -240,10 +259,7 @@ export abstract class AbstractCache {
|
|||
|
||||
protected async beforeSave(): Promise<void> {}
|
||||
|
||||
protected async saveCache(
|
||||
cachePath: string[],
|
||||
cacheKey: string
|
||||
): Promise<void> {
|
||||
protected async saveCache(cachePath: string[], cacheKey: string): Promise<void> {
|
||||
try {
|
||||
await cache.saveCache(cachePath, cacheKey)
|
||||
} catch (error) {
|
||||
|
|
|
@ -1,32 +1,37 @@
|
|||
import {GradleUserHomeCache} from './cache-gradle-user-home'
|
||||
import {ProjectDotGradleCache} from './cache-project-dot-gradle'
|
||||
import * as core from '@actions/core'
|
||||
import {isCacheDisabled, isCacheReadOnly} from './cache-utils'
|
||||
import {CachingReport, isCacheDisabled, isCacheReadOnly} from './cache-utils'
|
||||
|
||||
const BUILD_ROOT_DIR = 'BUILD_ROOT_DIR'
|
||||
|
||||
export async function restore(buildRootDirectory: string): Promise<void> {
|
||||
if (isCacheDisabled()) {
|
||||
core.info(
|
||||
'Cache is disabled: will not restore state from previous builds.'
|
||||
)
|
||||
core.info('Cache is disabled: will not restore state from previous builds.')
|
||||
return
|
||||
}
|
||||
|
||||
await core.group('Restore Gradle state from cache', async () => {
|
||||
core.saveState(BUILD_ROOT_DIR, buildRootDirectory)
|
||||
return Promise.all([
|
||||
new GradleUserHomeCache(buildRootDirectory).restore(),
|
||||
new ProjectDotGradleCache(buildRootDirectory).restore()
|
||||
])
|
||||
|
||||
const cachingReport = new CachingReport()
|
||||
|
||||
await new GradleUserHomeCache(buildRootDirectory).restore(cachingReport)
|
||||
|
||||
const projectDotGradleCache = new ProjectDotGradleCache(buildRootDirectory)
|
||||
if (cachingReport.fullyRestored) {
|
||||
// Only restore the configuration-cache if the Gradle Home is fully restored
|
||||
await projectDotGradleCache.restore(cachingReport)
|
||||
} else {
|
||||
// Otherwise, prepare the cache key for later save()
|
||||
projectDotGradleCache.prepareCacheKey()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export async function save(): Promise<void> {
|
||||
if (isCacheReadOnly()) {
|
||||
core.info(
|
||||
'Cache is read-only: will not save state for use in subsequent builds.'
|
||||
)
|
||||
core.info('Cache is read-only: will not save state for use in subsequent builds.')
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -3,11 +3,7 @@ import fs from 'fs'
|
|||
import path from 'path'
|
||||
import {writeInitScript} from './build-scan-capture'
|
||||
|
||||
export async function execute(
|
||||
executable: string,
|
||||
root: string,
|
||||
args: string[]
|
||||
): Promise<BuildResult> {
|
||||
export async function execute(executable: string, root: string, args: string[]): Promise<BuildResult> {
|
||||
let buildScanUrl: string | undefined
|
||||
|
||||
// TODO: instead of running with no-daemon, run `--stop` in post action.
|
||||
|
|
|
@ -17,10 +17,7 @@ export function locateGradleWrapperScript(buildRootDirectory: string): string {
|
|||
}
|
||||
|
||||
function validateGradleWrapper(buildRootDirectory: string): void {
|
||||
const wrapperProperties = path.resolve(
|
||||
buildRootDirectory,
|
||||
'gradle/wrapper/gradle-wrapper.properties'
|
||||
)
|
||||
const wrapperProperties = path.resolve(buildRootDirectory, 'gradle/wrapper/gradle-wrapper.properties')
|
||||
if (!fs.existsSync(wrapperProperties)) {
|
||||
throw new Error(
|
||||
`Cannot locate a Gradle wrapper properties file at '${wrapperProperties}'. Specify 'gradle-version' or 'gradle-executable' for projects without Gradle wrapper configured.`
|
||||
|
|
18
src/main.ts
18
src/main.ts
|
@ -18,10 +18,7 @@ export async function run(): Promise<void> {
|
|||
const args: string[] = parseCommandLineArguments()
|
||||
|
||||
const result = await execution.execute(
|
||||
await resolveGradleExecutable(
|
||||
workspaceDirectory,
|
||||
buildRootDirectory
|
||||
),
|
||||
await resolveGradleExecutable(workspaceDirectory, buildRootDirectory),
|
||||
buildRootDirectory,
|
||||
args
|
||||
)
|
||||
|
@ -34,9 +31,7 @@ export async function run(): Promise<void> {
|
|||
if (result.buildScanUrl) {
|
||||
core.setFailed(`Gradle build failed: ${result.buildScanUrl}`)
|
||||
} else {
|
||||
core.setFailed(
|
||||
`Gradle build failed: process exited with status ${result.status}`
|
||||
)
|
||||
core.setFailed(`Gradle build failed: process exited with status ${result.status}`)
|
||||
}
|
||||
} else {
|
||||
if (result.buildScanUrl) {
|
||||
|
@ -53,10 +48,7 @@ export async function run(): Promise<void> {
|
|||
|
||||
run()
|
||||
|
||||
async function resolveGradleExecutable(
|
||||
workspaceDirectory: string,
|
||||
buildRootDirectory: string
|
||||
): Promise<string> {
|
||||
async function resolveGradleExecutable(workspaceDirectory: string, buildRootDirectory: string): Promise<string> {
|
||||
const gradleVersion = core.getInput('gradle-version')
|
||||
if (gradleVersion !== '' && gradleVersion !== 'wrapper') {
|
||||
return path.resolve(await provision.gradleVersion(gradleVersion))
|
||||
|
@ -73,9 +65,7 @@ async function resolveGradleExecutable(
|
|||
function resolveBuildRootDirectory(baseDirectory: string): string {
|
||||
const buildRootDirectory = core.getInput('build-root-directory')
|
||||
const resolvedBuildRootDirectory =
|
||||
buildRootDirectory === ''
|
||||
? path.resolve(baseDirectory)
|
||||
: path.resolve(baseDirectory, buildRootDirectory)
|
||||
buildRootDirectory === '' ? path.resolve(baseDirectory) : path.resolve(baseDirectory, buildRootDirectory)
|
||||
return resolvedBuildRootDirectory
|
||||
}
|
||||
|
||||
|
|
|
@ -19,9 +19,7 @@ export async function gradleVersion(version: string): Promise<string> {
|
|||
case 'current':
|
||||
return gradleCurrent()
|
||||
case 'rc':
|
||||
core.warning(
|
||||
`Specifying gradle-version 'rc' has been deprecated. Use 'release-candidate' instead.`
|
||||
)
|
||||
core.warning(`Specifying gradle-version 'rc' has been deprecated. Use 'release-candidate' instead.`)
|
||||
return gradleReleaseCandidate()
|
||||
case 'release-candidate':
|
||||
return gradleReleaseCandidate()
|
||||
|
@ -35,16 +33,12 @@ export async function gradleVersion(version: string): Promise<string> {
|
|||
}
|
||||
|
||||
async function gradleCurrent(): Promise<string> {
|
||||
const versionInfo = await gradleVersionDeclaration(
|
||||
`${gradleVersionsBaseUrl}/current`
|
||||
)
|
||||
const versionInfo = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/current`)
|
||||
return provisionGradle(versionInfo)
|
||||
}
|
||||
|
||||
async function gradleReleaseCandidate(): Promise<string> {
|
||||
const versionInfo = await gradleVersionDeclaration(
|
||||
`${gradleVersionsBaseUrl}/release-candidate`
|
||||
)
|
||||
const versionInfo = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/release-candidate`)
|
||||
if (versionInfo && versionInfo.version && versionInfo.downloadUrl) {
|
||||
return provisionGradle(versionInfo)
|
||||
}
|
||||
|
@ -53,16 +47,12 @@ async function gradleReleaseCandidate(): Promise<string> {
|
|||
}
|
||||
|
||||
async function gradleNightly(): Promise<string> {
|
||||
const versionInfo = await gradleVersionDeclaration(
|
||||
`${gradleVersionsBaseUrl}/nightly`
|
||||
)
|
||||
const versionInfo = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/nightly`)
|
||||
return provisionGradle(versionInfo)
|
||||
}
|
||||
|
||||
async function gradleReleaseNightly(): Promise<string> {
|
||||
const versionInfo = await gradleVersionDeclaration(
|
||||
`${gradleVersionsBaseUrl}/release-nightly`
|
||||
)
|
||||
const versionInfo = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/release-nightly`)
|
||||
return provisionGradle(versionInfo)
|
||||
}
|
||||
|
||||
|
@ -74,34 +64,24 @@ async function gradle(version: string): Promise<string> {
|
|||
return provisionGradle(versionInfo)
|
||||
}
|
||||
|
||||
async function gradleVersionDeclaration(
|
||||
url: string
|
||||
): Promise<GradleVersionInfo> {
|
||||
async function gradleVersionDeclaration(url: string): Promise<GradleVersionInfo> {
|
||||
return await httpGetGradleVersion(url)
|
||||
}
|
||||
|
||||
async function findGradleVersionDeclaration(
|
||||
version: string
|
||||
): Promise<GradleVersionInfo | undefined> {
|
||||
const gradleVersions = await httpGetGradleVersions(
|
||||
`${gradleVersionsBaseUrl}/all`
|
||||
)
|
||||
async function findGradleVersionDeclaration(version: string): Promise<GradleVersionInfo | undefined> {
|
||||
const gradleVersions = await httpGetGradleVersions(`${gradleVersionsBaseUrl}/all`)
|
||||
return gradleVersions.find((entry: GradleVersionInfo) => {
|
||||
return entry.version === version
|
||||
})
|
||||
}
|
||||
|
||||
async function provisionGradle(
|
||||
versionInfo: GradleVersionInfo
|
||||
): Promise<string> {
|
||||
async function provisionGradle(versionInfo: GradleVersionInfo): Promise<string> {
|
||||
return core.group(`Provision Gradle ${versionInfo.version}`, async () => {
|
||||
return locateGradleAndDownloadIfRequired(versionInfo)
|
||||
})
|
||||
}
|
||||
|
||||
async function locateGradleAndDownloadIfRequired(
|
||||
versionInfo: GradleVersionInfo
|
||||
): Promise<string> {
|
||||
async function locateGradleAndDownloadIfRequired(versionInfo: GradleVersionInfo): Promise<string> {
|
||||
const installsDir = path.join(os.homedir(), 'gradle-installations/installs')
|
||||
const installDir = path.join(installsDir, `gradle-${versionInfo.version}`)
|
||||
if (fs.existsSync(installDir)) {
|
||||
|
@ -120,13 +100,8 @@ async function locateGradleAndDownloadIfRequired(
|
|||
return executable
|
||||
}
|
||||
|
||||
async function downloadAndCacheGradleDistribution(
|
||||
versionInfo: GradleVersionInfo
|
||||
): Promise<string> {
|
||||
const downloadPath = path.join(
|
||||
os.homedir(),
|
||||
`gradle-installations/downloads/gradle-${versionInfo.version}-bin.zip`
|
||||
)
|
||||
async function downloadAndCacheGradleDistribution(versionInfo: GradleVersionInfo): Promise<string> {
|
||||
const downloadPath = path.join(os.homedir(), `gradle-installations/downloads/gradle-${versionInfo.version}-bin.zip`)
|
||||
|
||||
if (isCacheDisabled()) {
|
||||
await downloadGradleDistribution(versionInfo, downloadPath)
|
||||
|
@ -136,14 +111,10 @@ async function downloadAndCacheGradleDistribution(
|
|||
const cacheKey = `gradle-${versionInfo.version}`
|
||||
const restoreKey = await cache.restoreCache([downloadPath], cacheKey)
|
||||
if (restoreKey) {
|
||||
core.info(
|
||||
`Restored Gradle distribution ${cacheKey} from cache to ${downloadPath}`
|
||||
)
|
||||
core.info(`Restored Gradle distribution ${cacheKey} from cache to ${downloadPath}`)
|
||||
return downloadPath
|
||||
}
|
||||
core.info(
|
||||
`Gradle distribution ${versionInfo.version} not found in cache. Will download.`
|
||||
)
|
||||
core.info(`Gradle distribution ${versionInfo.version} not found in cache. Will download.`)
|
||||
await downloadGradleDistribution(versionInfo, downloadPath)
|
||||
|
||||
if (!isCacheReadOnly()) {
|
||||
|
@ -151,10 +122,7 @@ async function downloadAndCacheGradleDistribution(
|
|||
await cache.saveCache([downloadPath], cacheKey)
|
||||
} catch (error) {
|
||||
// Fail on validation errors or non-errors (the latter to keep Typescript happy)
|
||||
if (
|
||||
error instanceof cache.ValidationError ||
|
||||
!(error instanceof Error)
|
||||
) {
|
||||
if (error instanceof cache.ValidationError || !(error instanceof Error)) {
|
||||
throw error
|
||||
}
|
||||
core.warning(error.message)
|
||||
|
@ -163,16 +131,9 @@ async function downloadAndCacheGradleDistribution(
|
|||
return downloadPath
|
||||
}
|
||||
|
||||
async function downloadGradleDistribution(
|
||||
versionInfo: GradleVersionInfo,
|
||||
downloadPath: string
|
||||
): Promise<void> {
|
||||
async function downloadGradleDistribution(versionInfo: GradleVersionInfo, downloadPath: string): Promise<void> {
|
||||
await toolCache.downloadTool(versionInfo.downloadUrl, downloadPath)
|
||||
core.info(
|
||||
`Downloaded ${versionInfo.downloadUrl} to ${downloadPath} (size ${
|
||||
fs.statSync(downloadPath).size
|
||||
})`
|
||||
)
|
||||
core.info(`Downloaded ${versionInfo.downloadUrl} to ${downloadPath} (size ${fs.statSync(downloadPath).size})`)
|
||||
}
|
||||
|
||||
function executableFrom(installDir: string): string {
|
||||
|
@ -183,9 +144,7 @@ async function httpGetGradleVersion(url: string): Promise<GradleVersionInfo> {
|
|||
return JSON.parse(await httpGetString(url))
|
||||
}
|
||||
|
||||
async function httpGetGradleVersions(
|
||||
url: string
|
||||
): Promise<GradleVersionInfo[]> {
|
||||
async function httpGetGradleVersions(url: string): Promise<GradleVersionInfo[]> {
|
||||
return JSON.parse(await httpGetString(url))
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue