Merge pull request #83 from gradle/dd/v2

Adapt paths to differing Gradle User Home
This commit is contained in:
Daz DeBoer 2021-09-27 22:08:40 -06:00 committed by GitHub
commit 5576baa56b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 163 additions and 83 deletions

View file

@ -1,5 +1,4 @@
# make sure the build works and doesn't produce spurious changes
name: dev
name: Verify generated outputs
on:
pull_request:

View file

@ -1,10 +1,6 @@
# Run builds under certain failure conditions to allow the output to be manually inspected.
# These build invocations are informational only, and are expected to fail
name: failure-cases
name: Execute failure cases
on:
pull_request:
push:
workflow_dispatch:
env:

View file

@ -1,5 +1,4 @@
# Verify the functionality works as expected
name: integration-testing
name: Test caching
on:
pull_request:
@ -31,42 +30,6 @@ jobs:
build-root-directory: __tests__/samples/basic
arguments: test --configuration-cache
# Tests for executing with different Gradle versions.
# Each build verifies that it is executed with the expected Gradle version.
gradle-execution:
needs: seed-build
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
include:
- os: windows-latest
script-suffix: '.bat'
runs-on: ${{ matrix.os }}
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Test use defined Gradle version
uses: ./
with:
gradle-version: 6.9
build-root-directory: __tests__/samples/no-wrapper
arguments: help -DgradleVersionCheck=6.9
cache-read-only: true
- name: Test use Gradle version alias
uses: ./
with:
gradle-version: release-candidate
build-root-directory: __tests__/samples/no-wrapper
arguments: help -DgradleVersionCheck=7.2
cache-read-only: true
- name: Test use defined Gradle executable
uses: ./
with:
gradle-executable: __tests__/samples/basic/gradlew${{ matrix.script-suffix }}
build-root-directory: __tests__/samples/no-wrapper
arguments: help -DgradleVersionCheck=7.1.1
cache-read-only: true
# Test that the gradle-user-home cache will cache dependencies, by running build with --offline
dependencies-cache:
needs: seed-build

View file

@ -0,0 +1,42 @@
name: Test Gradle execution
on:
pull_request:
push:
workflow_dispatch:
env:
CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}-
jobs:
# Tests for executing with different Gradle versions.
# Each build verifies that it is executed with the expected Gradle version.
gradle-execution:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
include:
- os: windows-latest
script-suffix: '.bat'
runs-on: ${{ matrix.os }}
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Test use defined Gradle version
uses: ./
with:
gradle-version: 6.9
build-root-directory: __tests__/samples/no-wrapper
arguments: help -DgradleVersionCheck=6.9
- name: Test use Gradle version alias
uses: ./
with:
gradle-version: release-candidate
build-root-directory: __tests__/samples/no-wrapper
arguments: help -DgradleVersionCheck=7.2
- name: Test use defined Gradle executable
uses: ./
with:
gradle-executable: __tests__/samples/basic/gradlew${{ matrix.script-suffix }}
build-root-directory: __tests__/samples/no-wrapper
arguments: help -DgradleVersionCheck=7.1.1

View file

@ -0,0 +1,52 @@
name: Test caching with a custom GRADLE_USER_HOME
on:
pull_request:
push:
workflow_dispatch:
env:
CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}-
GRADLE_USER_HOME: custom/gradle/home
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:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Build using Gradle wrapper
uses: ./
with:
build-root-directory: __tests__/samples/basic
arguments: test
# Test that the gradle-user-home cache will cache dependencies, by running build with --offline
dependencies-cache:
needs: seed-build
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Execute Gradle build with --offline
uses: ./
with:
build-root-directory: __tests__/samples/basic
arguments: test --offline
cache-read-only: true
# Test that the gradle-user-home cache will cache and restore local build-cache
build-cache:
needs: seed-build
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Execute Gradle build and verify tasks from cache
uses: ./
with:
build-root-directory: __tests__/samples/basic
arguments: test -DverifyCachedBuild=true
cache-read-only: true

View file

@ -1,5 +1,4 @@
# Make sure the action works on a clean machine without building
name: integration-testing-kotlin-dsl
name: Test caching with Kotlin DSL
on:
pull_request:

View file

@ -11,5 +11,11 @@ describe('cacheUtils-utils', () => {
const hash = cacheUtils.hashStrings(['foo', 'bar', 'baz'])
expect(hash).toBe('6df23dc03f9b54cc38a0fc1483df6e21')
})
it('normalized filenames', async () => {
const fileNames = ['/foo/bar/baz.zip', '../boo.html']
const posixHash = cacheUtils.hashFileNames(fileNames)
const windowsHash = cacheUtils.hashFileNames(fileNames)
expect(posixHash).toBe(windowsHash)
})
})
})

2
dist/main/index.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

2
dist/post/index.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -5,23 +5,24 @@ import * as core from '@actions/core'
import * as glob from '@actions/glob'
import * as exec from '@actions/exec'
import {AbstractCache, hashStrings} from './cache-utils'
import {AbstractCache, hashFileNames} from './cache-utils'
// Which paths under Gradle User Home should be cached
// TODO: This should adapt for the `GRADLE_USER_HOME` environment variable
// TODO: Allow the user to override / tweak this set
const CACHE_PATH = ['~/.gradle/caches', '~/.gradle/notifications']
const CACHE_PATH = ['caches', 'notifications']
const COMMON_ARTIFACT_CACHES = new Map([
['generated-gradle-jars', '~/.gradle/caches/*/generated-gradle-jars/*.jar'],
['wrapper-zips', '~/.gradle/wrapper/dists/*/*/*.zip'],
['dependency-jars', '~/.gradle/caches/modules-*/files-*/**/*.jar'],
['instrumented-jars', '~/.gradle/caches/jars-*/*/*.jar']
['generated-gradle-jars', 'caches/*/generated-gradle-jars/*.jar'],
['wrapper-zips', 'wrapper/dists/*/*/*.zip'],
['dependency-jars', 'caches/modules-*/files-*/**/*.jar'],
['instrumented-jars', 'caches/jars-*/*/*.jar']
])
export class GradleUserHomeCache extends AbstractCache {
constructor() {
private gradleUserHome: string
constructor(rootDir: string) {
super('gradle', 'Gradle User Home')
this.gradleUserHome = this.determineGradleUserHome(rootDir)
}
async afterRestore(): Promise<void> {
@ -32,7 +33,7 @@ export class GradleUserHomeCache extends AbstractCache {
private async restoreCommonArtifacts(): 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)
// Run sequentially when debugging enabled
if (this.cacheDebuggingEnabled) {
@ -46,19 +47,19 @@ export class GradleUserHomeCache extends AbstractCache {
private async restoreCommonArtifactBundle(
bundle: string,
pattern: string
artifactPath: string
): Promise<void> {
const cacheMetaFile = this.getCacheMetaFile(bundle)
if (fs.existsSync(cacheMetaFile)) {
const cacheKey = fs.readFileSync(cacheMetaFile, 'utf-8').trim()
const restoreKey = await this.restoreCache([pattern], cacheKey)
const restoreKey = await this.restoreCache([artifactPath], cacheKey)
if (restoreKey) {
core.info(
`Restored ${bundle} with key ${cacheKey} to ${pattern}`
`Restored ${bundle} with key ${cacheKey} to ${artifactPath}`
)
} else {
this.debug(
`Failed to restore ${bundle} with key ${cacheKey} to ${pattern}`
`Failed to restore ${bundle} with key ${cacheKey} to ${artifactPath}`
)
}
} else {
@ -70,7 +71,7 @@ export class GradleUserHomeCache extends AbstractCache {
private getCacheMetaFile(name: string): string {
return path.resolve(
this.getGradleUserHome(),
this.gradleUserHome,
'caches',
`.gradle-build-action.${name}.cache`
)
@ -80,15 +81,14 @@ export class GradleUserHomeCache extends AbstractCache {
if (!this.cacheDebuggingEnabled) {
return
}
const gradleUserHome = path.resolve(os.homedir(), '.gradle')
if (!fs.existsSync(gradleUserHome)) {
if (!fs.existsSync(this.gradleUserHome)) {
return
}
const result = await exec.getExecOutput(
'du',
['-h', '-c', '-t', '5M'],
{
cwd: gradleUserHome,
cwd: this.gradleUserHome,
silent: true,
ignoreReturnCode: true
}
@ -116,7 +116,7 @@ export class GradleUserHomeCache extends AbstractCache {
private async saveCommonArtifacts(): 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)
// Run sequentially when debugging enabled
if (this.cacheDebuggingEnabled) {
@ -130,11 +130,11 @@ export class GradleUserHomeCache extends AbstractCache {
private async saveCommonArtifactBundle(
bundle: string,
pattern: string
artifactPath: string
): Promise<void> {
const cacheMetaFile = this.getCacheMetaFile(bundle)
const globber = await glob.create(pattern)
const globber = await glob.create(artifactPath)
const commonArtifactFiles = await globber.glob()
// Handle no matching files
@ -149,10 +149,7 @@ export class GradleUserHomeCache extends AbstractCache {
const previouslyRestoredKey = fs.existsSync(cacheMetaFile)
? fs.readFileSync(cacheMetaFile, 'utf-8').trim()
: ''
const cacheKey = this.createCacheKey(
bundle,
hashStrings(commonArtifactFiles)
)
const cacheKey = this.createCacheKey(bundle, commonArtifactFiles)
if (previouslyRestoredKey === cacheKey) {
this.debug(
@ -160,7 +157,7 @@ export class GradleUserHomeCache extends AbstractCache {
)
} else {
core.info(`Caching ${bundle} with cache key: ${cacheKey}`)
await this.saveCache([pattern], cacheKey)
await this.saveCache([artifactPath], cacheKey)
this.debug(`Writing cache metafile: ${cacheMetaFile}`)
fs.writeFileSync(cacheMetaFile, cacheKey)
@ -171,22 +168,41 @@ export class GradleUserHomeCache extends AbstractCache {
}
}
protected createCacheKey(bundle: string, key: string): string {
protected createCacheKey(bundle: string, files: string[]): string {
const cacheKeyPrefix = process.env['CACHE_KEY_PREFIX'] || ''
const relativeFiles = files.map(x =>
path.relative(this.gradleUserHome, x)
)
const key = hashFileNames(relativeFiles)
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')
}
protected cacheOutputExists(): boolean {
// 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)
}
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)
])
)
}
}

View file

@ -2,6 +2,7 @@ import * as core from '@actions/core'
import * as cache from '@actions/cache'
import * as github from '@actions/github'
import * as crypto from 'crypto'
import * as path from 'path'
export function isCacheDisabled(): boolean {
return core.getBooleanInput('cache-disabled')
@ -53,6 +54,12 @@ export function hashStrings(values: string[]): string {
return hash.digest('hex')
}
export function hashFileNames(fileNames: string[]): string {
return hashStrings(
fileNames.map(x => x.replace(new RegExp(`\\${path.sep}`, 'g'), '/'))
)
}
class CacheKey {
key: string
restoreKeys: string[]

View file

@ -14,7 +14,7 @@ export async function restore(buildRootDirectory: string): Promise<void> {
await core.group('Restore Gradle state from cache', async () => {
core.saveState(BUILD_ROOT_DIR, buildRootDirectory)
return Promise.all([
new GradleUserHomeCache().restore(),
new GradleUserHomeCache(buildRootDirectory).restore(),
new ProjectDotGradleCache(buildRootDirectory).restore()
])
})
@ -29,7 +29,7 @@ export async function save(): Promise<void> {
await core.group('Caching Gradle state', async () => {
const buildRootDirectory = core.getState(BUILD_ROOT_DIR)
return Promise.all([
new GradleUserHomeCache().save(),
new GradleUserHomeCache(buildRootDirectory).save(),
new ProjectDotGradleCache(buildRootDirectory).save()
])
})