Ensure minimum Gradle version for cache-cleanup

Instead of always installing the latest version of Gradle for cache-cleanup,
check for a version on PATH and use it if version >= 8.9
This commit is contained in:
daz 2024-08-31 10:22:37 -06:00
parent 05bf82e720
commit a6284191d3
No known key found for this signature in database
5 changed files with 125 additions and 26 deletions

View File

@ -55,11 +55,12 @@ export class CacheCleaner {
)
fs.writeFileSync(path.resolve(cleanupProjectDir, 'build.gradle'), 'task("noop") {}')
const executable = await provisioner.provisionGradle('current')
// Gradle >= 8.9 required for cache cleanup
const executable = await provisioner.provisionGradleAtLeast('8.9')
await core.group('Executing Gradle to clean up caches', async () => {
core.info(`Cleaning up caches last used before ${cleanTimestamp}`)
await this.executeCleanupBuild(executable!, cleanupProjectDir)
await this.executeCleanupBuild(executable, cleanupProjectDir)
})
}

View File

@ -9,7 +9,7 @@ import {cacheDebug, hashFileNames, isCacheDebuggingEnabled, restoreCache, saveCa
import {BuildResult, loadBuildResults} from '../build-results'
import {CacheConfig, ACTION_METADATA_DIR} from '../configuration'
import {getCacheKeyBase} from './cache-key'
import { versionIsAtLeast } from '../execution/gradle'
import {versionIsAtLeast} from '../execution/gradle'
const SKIP_RESTORE_VAR = 'GRADLE_BUILD_ACTION_SKIP_RESTORE'
const CACHE_PROTOCOL_VERSION = 'v1'

View File

@ -1,8 +1,8 @@
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import which from 'which'
import * as semver from 'semver'
import * as provisioner from './provision'
import * as gradlew from './gradlew'
@ -48,3 +48,27 @@ export function versionIsAtLeast(actualVersion: string, requiredVersion: string)
return semver.gte(actualSemver, comparisonSemver)
}
}
export async function findGradleVersionOnPath(): Promise<GradleExecutable | undefined> {
const gradleExecutable = await which('gradle', {nothrow: true})
if (gradleExecutable) {
const output = await exec.getExecOutput(gradleExecutable, ['-v'], {silent: true})
const version = parseGradleVersionFromOutput(output.stdout)
return version ? new GradleExecutable(version, gradleExecutable) : undefined
}
return undefined
}
export function parseGradleVersionFromOutput(output: string): string | undefined {
const regex = /Gradle (\d+\.\d+(\.\d+)?(-.*)?)/
const versionString = output.match(regex)?.[1]
return versionString
}
class GradleExecutable {
constructor(
readonly version: string,
readonly executable: string
) {}
}

View File

@ -1,13 +1,12 @@
import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
import which from 'which'
import * as httpm from '@actions/http-client'
import * as core from '@actions/core'
import * as cache from '@actions/cache'
import * as exec from '@actions/exec'
import * as toolCache from '@actions/tool-cache'
import {findGradleVersionOnPath, versionIsAtLeast} from './gradle'
import * as gradlew from './gradlew'
import {handleCacheFailure} from '../caching/cache-utils'
import {CacheConfig} from '../configuration'
@ -26,6 +25,16 @@ export async function provisionGradle(gradleVersion: string): Promise<string | u
return undefined
}
/**
* Ensure that the Gradle version on PATH is no older than the specified version.
* If the version on PATH is older, install the specified version and add it to the PATH.
* @return Installed Gradle executable or undefined if no version configured.
*/
export async function provisionGradleAtLeast(gradleVersion: string): Promise<string> {
const installedVersion = await installGradleVersionAtLeast(await gradleRelease(gradleVersion))
return addToPath(installedVersion)
}
async function addToPath(executable: string): Promise<string> {
core.addPath(path.dirname(executable))
return executable
@ -51,7 +60,7 @@ async function resolveGradleVersion(version: string): Promise<GradleVersionInfo>
case 'release-nightly':
return gradleReleaseNightly()
default:
return gradle(version)
return gradleRelease(version)
}
}
@ -76,7 +85,7 @@ async function gradleReleaseNightly(): Promise<GradleVersionInfo> {
return await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/release-nightly`)
}
async function gradle(version: string): Promise<GradleVersionInfo> {
async function gradleRelease(version: string): Promise<GradleVersionInfo> {
const versionInfo = await findGradleVersionDeclaration(version)
if (!versionInfo) {
throw new Error(`Gradle version ${version} does not exists`)
@ -97,10 +106,24 @@ async function findGradleVersionDeclaration(version: string): Promise<GradleVers
async function installGradleVersion(versionInfo: GradleVersionInfo): Promise<string> {
return core.group(`Provision Gradle ${versionInfo.version}`, async () => {
const preInstalledGradle = await findGradleVersionOnPath(versionInfo)
if (preInstalledGradle !== undefined) {
const gradleOnPath = await findGradleVersionOnPath()
if (gradleOnPath?.version === versionInfo.version) {
core.info(`Gradle version ${versionInfo.version} is already available on PATH. Not installing.`)
return preInstalledGradle
return gradleOnPath.executable
}
return locateGradleAndDownloadIfRequired(versionInfo)
})
}
async function installGradleVersionAtLeast(versionInfo: GradleVersionInfo): Promise<string> {
return core.group(`Provision Gradle >= ${versionInfo.version}`, async () => {
const gradleOnPath = await findGradleVersionOnPath()
if (gradleOnPath && versionIsAtLeast(gradleOnPath.version, versionInfo.version)) {
core.info(
`Gradle version ${gradleOnPath.version} is available on PATH and >= ${versionInfo.version}. Not installing.`
)
return gradleOnPath.executable
}
return locateGradleAndDownloadIfRequired(versionInfo)
@ -192,15 +215,3 @@ interface GradleVersionInfo {
version: string
downloadUrl: string
}
async function findGradleVersionOnPath(versionInfo: GradleVersionInfo): Promise<string | undefined> {
const gradleExecutable = await which('gradle', {nothrow: true})
if (gradleExecutable) {
const output = await exec.getExecOutput(gradleExecutable, ['-v'], {silent: true})
if (output.stdout.includes(`\nGradle ${versionInfo.version}\n`)) {
return gradleExecutable
}
}
return undefined
}

View File

@ -1,7 +1,8 @@
import { versionIsAtLeast } from '../../src/execution/gradle'
import { describe } from 'node:test'
import { versionIsAtLeast, parseGradleVersionFromOutput } from '../../src/execution/gradle'
describe('gradle-version', () => {
describe('can compare to', () => {
describe('gradle', () => {
describe('can compare version with', () => {
it('same version', async () => {
expect(versionIsAtLeast('6.7.1', '6.7.1')).toBe(true)
expect(versionIsAtLeast('7.0', '7.0')).toBe(true)
@ -38,4 +39,66 @@ describe('gradle-version', () => {
expect(versionIsAtLeast('9.1-branch-provider_api_migration_public_api_changes-20240826121451+0000', '9.1')).toBe(false)
})
})
describe('can parse version from output', () => {
it('major version', async () => {
const output = `
------------------------------------------------------------
Gradle 8.9
------------------------------------------------------------
`
const version = await parseGradleVersionFromOutput(output)!
expect(version).toBe('8.9')
})
it('patch version', async () => {
const output = `
------------------------------------------------------------
Gradle 8.9.1
------------------------------------------------------------
`
const version = await parseGradleVersionFromOutput(output)!
expect(version).toBe('8.9.1')
})
it('rc version', async () => {
const output = `
------------------------------------------------------------
Gradle 8.9-rc-1
------------------------------------------------------------
`
const version = await parseGradleVersionFromOutput(output)!
expect(version).toBe('8.9-rc-1')
})
it('milestone version', async () => {
const output = `
------------------------------------------------------------
Gradle 8.0-milestone-6
------------------------------------------------------------
`
const version = await parseGradleVersionFromOutput(output)!
expect(version).toBe('8.0-milestone-6')
})
it('snapshot version', async () => {
const output = `
------------------------------------------------------------
Gradle 8.10.2-20240828012138+0000
------------------------------------------------------------
`
const version = await parseGradleVersionFromOutput(output)!
expect(version).toBe('8.10.2-20240828012138+0000')
})
it('branch version', async () => {
const output = `
------------------------------------------------------------
Gradle 9.0-branch-provider_api_migration_public_api_changes-20240830060514+0000
------------------------------------------------------------
`
const version = await parseGradleVersionFromOutput(output)!
expect(version).toBe('9.0-branch-provider_api_migration_public_api_changes-20240830060514+0000')
})
})
})