mirror of
https://github.com/gradle/gradle-build-action.git
synced 2025-04-06 13:24:13 +02:00
Use Gradle Wrapper if specified without Gradle version
Signed-off-by: Anders Fischer-Nielsen <andersfischern@me.com>
This commit is contained in:
parent
8d2472536e
commit
9b0ff91415
8 changed files with 5161 additions and 5102 deletions
5145
dist/main/index.js
vendored
5145
dist/main/index.js
vendored
File diff suppressed because it is too large
Load diff
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
5055
dist/post/index.js
vendored
5055
dist/post/index.js
vendored
File diff suppressed because it is too large
Load diff
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,10 +7,12 @@ import path from 'path'
|
|||
export class CacheCleaner {
|
||||
private readonly gradleUserHome: string
|
||||
private readonly tmpDir: string
|
||||
private readonly gradleVersion?: string
|
||||
|
||||
constructor(gradleUserHome: string, tmpDir: string) {
|
||||
constructor(gradleUserHome: string, tmpDir: string, gradleVersion?: string) {
|
||||
this.gradleUserHome = gradleUserHome
|
||||
this.tmpDir = tmpDir
|
||||
this.gradleVersion = gradleVersion
|
||||
}
|
||||
|
||||
async prepare(): Promise<void> {
|
||||
|
@ -42,9 +44,15 @@ export class CacheCleaner {
|
|||
)
|
||||
fs.writeFileSync(path.resolve(cleanupProjectDir, 'build.gradle'), 'task("noop") {}')
|
||||
|
||||
await exec.exec(`gradle -g ${this.gradleUserHome} --no-daemon --build-cache --no-scan --quiet noop`, [], {
|
||||
cwd: cleanupProjectDir
|
||||
})
|
||||
const gradleExecutable = this.gradleVersion === 'wrapper' ? './gradlew' : 'gradle'
|
||||
|
||||
await exec.exec(
|
||||
`${gradleExecutable} -g ${this.gradleUserHome} --no-daemon --build-cache --no-scan --quiet noop`,
|
||||
[],
|
||||
{
|
||||
cwd: cleanupProjectDir
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private async ageAllFiles(fileName = '*'): Promise<void> {
|
||||
|
|
|
@ -6,6 +6,7 @@ import {GradleStateCache} from './cache-base'
|
|||
import {CacheCleaner} from './cache-cleaner'
|
||||
|
||||
const CACHE_RESTORED_VAR = 'GRADLE_BUILD_ACTION_CACHE_RESTORED'
|
||||
const GRADLE_VERSION = 'GRADLE_VERSION'
|
||||
|
||||
export async function restore(gradleUserHome: string, cacheListener: CacheListener): Promise<void> {
|
||||
// Bypass restore cache on all but first action step in workflow.
|
||||
|
@ -48,7 +49,8 @@ export async function restore(gradleUserHome: string, cacheListener: CacheListen
|
|||
|
||||
if (isCacheCleanupEnabled() && !isCacheReadOnly()) {
|
||||
core.info('Preparing cache for cleanup.')
|
||||
const cacheCleaner = new CacheCleaner(gradleUserHome, process.env['RUNNER_TEMP']!)
|
||||
const gradleVersion = core.getState(GRADLE_VERSION)
|
||||
const cacheCleaner = new CacheCleaner(gradleUserHome, process.env['RUNNER_TEMP']!, gradleVersion)
|
||||
await cacheCleaner.prepare()
|
||||
}
|
||||
}
|
||||
|
@ -78,7 +80,8 @@ export async function save(
|
|||
|
||||
if (isCacheCleanupEnabled()) {
|
||||
core.info('Forcing cache cleanup.')
|
||||
const cacheCleaner = new CacheCleaner(gradleUserHome, process.env['RUNNER_TEMP']!)
|
||||
const gradleVersion = core.getState(GRADLE_VERSION)
|
||||
const cacheCleaner = new CacheCleaner(gradleUserHome, process.env['RUNNER_TEMP']!, gradleVersion)
|
||||
await cacheCleaner.forceCleanup()
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,8 @@ import * as setupGradle from './setup-gradle'
|
|||
import * as execution from './execution'
|
||||
import * as provision from './provision'
|
||||
|
||||
const GRADLE_VERSION = 'GRADLE_VERSION'
|
||||
|
||||
/**
|
||||
* The main entry point for the action, called by Github Actions for the step.
|
||||
*/
|
||||
|
@ -39,6 +41,10 @@ run()
|
|||
|
||||
async function provisionGradle(workspaceDirectory: string): Promise<string | undefined> {
|
||||
const gradleVersion = core.getInput('gradle-version')
|
||||
|
||||
// Save the Gradle version for use in the post-action step.
|
||||
core.saveState(GRADLE_VERSION, gradleVersion)
|
||||
|
||||
if (gradleVersion !== '' && gradleVersion !== 'wrapper') {
|
||||
return path.resolve(await provision.gradleVersion(gradleVersion))
|
||||
}
|
||||
|
|
|
@ -62,6 +62,36 @@ test('will cleanup unused gradle versions', async () => {
|
|||
expect(fs.existsSync(gradleCurrent)).toBe(true)
|
||||
})
|
||||
|
||||
test('will use Gradle Wrapper for cleanup if specified', async () => {
|
||||
const projectRoot = prepareTestProject()
|
||||
const gradleUserHome = path.resolve(projectRoot, 'HOME')
|
||||
const tmpDir = path.resolve(projectRoot, 'tmp')
|
||||
const cacheCleaner = new CacheCleaner(gradleUserHome, tmpDir, 'wrapper')
|
||||
|
||||
// Initialize HOME with 2 different Gradle versions
|
||||
await runGradleWrapperBuild(projectRoot, 'build')
|
||||
await runGradleBuild(projectRoot, 'build')
|
||||
|
||||
await cacheCleaner.prepare()
|
||||
|
||||
// Run with only one of these versions
|
||||
await runGradleBuild(projectRoot, 'build')
|
||||
|
||||
const gradle733 = path.resolve(gradleUserHome, "caches/7.3.3")
|
||||
const wrapper733 = path.resolve(gradleUserHome, "wrapper/dists/gradle-7.3.3-bin")
|
||||
const gradleCurrent = path.resolve(gradleUserHome, "caches/7.5.1")
|
||||
|
||||
expect(fs.existsSync(gradle733)).toBe(true)
|
||||
expect(fs.existsSync(wrapper733)).toBe(true)
|
||||
expect(fs.existsSync(gradleCurrent)).toBe(true)
|
||||
|
||||
await cacheCleaner.forceCleanup()
|
||||
|
||||
expect(fs.existsSync(gradle733)).toBe(false)
|
||||
expect(fs.existsSync(wrapper733)).toBe(false)
|
||||
expect(fs.existsSync(gradleCurrent)).toBe(true)
|
||||
})
|
||||
|
||||
async function runGradleBuild(projectRoot: string, args: string, version: string = '3.1'): Promise<void> {
|
||||
const status31 = await exec.exec(`gradle -g HOME --no-daemon --build-cache -Dcommons_math3_version="${version}" ${args}`, [], {
|
||||
cwd: projectRoot
|
||||
|
|
Loading…
Add table
Reference in a new issue