mirror of
https://github.com/gradle/actions.git
synced 2026-08-19 12:22:51 +00:00
With this change, the caching functionality of `setup-gradle` and `dependency-submission` is now provided by `gradle-actions-caching`, a closed-source library distributed under our [Terms of Use](https://gradle.com/legal/terms-of-use/). The rest of the action implementation remains open source. Using `setup-gradle` or `dependency-submission` with caching enabled involves loading and using the `gradle-actions-caching` component, requiring acceptance of the [Terms of Use](https://gradle.com/legal/terms-of-use/). There are no functional changes to caching provided by these actions: all workflows will continue to function as before. The non-caching aspects of action implementation remain open source. By running these actions with caching disabled they can be used without ever loading `gradle-actions-caching` or accepting the license terms. Supporting the caching infrastructure in this project requires a substantial engineering investment by Gradle Technologies, which we can sustain thanks to Develocity, our commercial offering. Caching technologies are a core part of the Develocity offering, and the caching in `setup-gradle` fits squarely in that space. This licensing change lets us continue to build advanced capabilities that go beyond what we would offer as open source. Proper production-ready Configuration Cache support will be the first capability. Improving build performance for self-hosted runners will follow. We may introduce functionality restrictions in future updates. However, caching functionality will remain free for public repositories. We have a long-standing commitment to open source, as maintainers of Gradle Build Tool, and by [sponsoring the open source community](https://gradle.com/oss-sponsored-by-develocity/) with free Develocity licenses. Public repositories are primarily used by open source projects, and we remain committed to supporting them. - Implementation of caching logic to save and restore Gradle User Home content has been removed, replaced by the `gradle-actions-caching` component. - The `@actions/caching` library is still used to cache Gradle distributions that are downloaded and provisioned by `setup-gradle`. This PR updates to the latest version of `@actions/caching`, and removes the patch that is no longer required. - License notices are now displayed in documentation, logs and the generated Job Summary.
213 lines
7.7 KiB
TypeScript
213 lines
7.7 KiB
TypeScript
import * as fs from 'fs'
|
|
import * as os from 'os'
|
|
import * as path from 'path'
|
|
import * as httpm from '@actions/http-client'
|
|
import * as core from '@actions/core'
|
|
import * as cache from '@actions/cache'
|
|
import * as toolCache from '@actions/tool-cache'
|
|
|
|
import {determineGradleVersion, findGradleExecutableOnPath} from './gradle'
|
|
import * as gradlew from './gradlew'
|
|
import {CacheConfig} from '../configuration'
|
|
|
|
const gradleVersionsBaseUrl = 'https://services.gradle.org/versions'
|
|
|
|
/**
|
|
* Install any configured version of Gradle, adding the executable to the PATH.
|
|
* @return Installed Gradle executable or undefined if no version configured.
|
|
*/
|
|
export async function provisionGradle(gradleVersion: string): Promise<string | undefined> {
|
|
if (gradleVersion !== '' && gradleVersion !== 'wrapper') {
|
|
return addToPath(await installGradle(gradleVersion))
|
|
}
|
|
|
|
return undefined
|
|
}
|
|
|
|
async function addToPath(executable: string): Promise<string> {
|
|
core.addPath(path.dirname(executable))
|
|
return executable
|
|
}
|
|
|
|
async function installGradle(version: string): Promise<string> {
|
|
const versionInfo = await resolveGradleVersion(version)
|
|
core.setOutput('gradle-version', versionInfo.version)
|
|
return installGradleVersion(versionInfo)
|
|
}
|
|
|
|
async function resolveGradleVersion(version: string): Promise<GradleVersionInfo> {
|
|
switch (version) {
|
|
case 'current':
|
|
return gradleCurrent()
|
|
case 'rc':
|
|
core.warning(`Specifying gradle-version 'rc' has been deprecated. Use 'release-candidate' instead.`)
|
|
return gradleReleaseCandidate()
|
|
case 'release-candidate':
|
|
return gradleReleaseCandidate()
|
|
case 'nightly':
|
|
return gradleNightly()
|
|
case 'release-nightly':
|
|
return gradleReleaseNightly()
|
|
default:
|
|
return gradleRelease(version)
|
|
}
|
|
}
|
|
|
|
async function gradleCurrent(): Promise<GradleVersionInfo> {
|
|
return await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/current`)
|
|
}
|
|
|
|
async function gradleReleaseCandidate(): Promise<GradleVersionInfo> {
|
|
const versionInfo = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/release-candidate`)
|
|
if (versionInfo && versionInfo.version && versionInfo.downloadUrl) {
|
|
return versionInfo
|
|
}
|
|
core.info('No current release-candidate found, will fallback to current')
|
|
return gradleCurrent()
|
|
}
|
|
|
|
async function gradleNightly(): Promise<GradleVersionInfo> {
|
|
return await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/nightly`)
|
|
}
|
|
|
|
async function gradleReleaseNightly(): Promise<GradleVersionInfo> {
|
|
return await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/release-nightly`)
|
|
}
|
|
|
|
async function gradleRelease(version: string): Promise<GradleVersionInfo> {
|
|
const versionInfo = await findGradleVersionDeclaration(version)
|
|
if (!versionInfo) {
|
|
throw new Error(`Gradle version ${version} does not exist`)
|
|
}
|
|
return versionInfo
|
|
}
|
|
|
|
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`)
|
|
return gradleVersions.find((entry: GradleVersionInfo) => {
|
|
return entry.version === version
|
|
})
|
|
}
|
|
|
|
async function installGradleVersion(versionInfo: GradleVersionInfo): Promise<string> {
|
|
return core.group(`Provision Gradle ${versionInfo.version}`, async () => {
|
|
const gradleOnPath = await findGradleExecutableOnPath()
|
|
if (gradleOnPath) {
|
|
const gradleOnPathVersion = await determineGradleVersion(gradleOnPath)
|
|
if (gradleOnPathVersion === versionInfo.version) {
|
|
core.info(`Gradle version ${versionInfo.version} is already available on PATH. Not installing.`)
|
|
return gradleOnPath
|
|
}
|
|
}
|
|
|
|
return locateGradleAndDownloadIfRequired(versionInfo)
|
|
})
|
|
}
|
|
|
|
async function locateGradleAndDownloadIfRequired(versionInfo: GradleVersionInfo): Promise<string> {
|
|
const installsDir = path.join(getProvisionDir(), 'installs')
|
|
const installDir = path.join(installsDir, `gradle-${versionInfo.version}`)
|
|
if (fs.existsSync(installDir)) {
|
|
core.info(`Gradle installation already exists at ${installDir}`)
|
|
return executableFrom(installDir)
|
|
}
|
|
|
|
const downloadPath = await downloadAndCacheGradleDistribution(versionInfo)
|
|
await toolCache.extractZip(downloadPath, installsDir)
|
|
core.info(`Extracted Gradle ${versionInfo.version} to ${installDir}`)
|
|
|
|
const executable = executableFrom(installDir)
|
|
fs.chmodSync(executable, '755')
|
|
core.info(`Provisioned Gradle executable ${executable}`)
|
|
|
|
return executable
|
|
}
|
|
|
|
async function downloadAndCacheGradleDistribution(versionInfo: GradleVersionInfo): Promise<string> {
|
|
const downloadPath = path.join(getProvisionDir(), `downloads/gradle-${versionInfo.version}-bin.zip`)
|
|
|
|
// TODO: Convert this to a class and inject config
|
|
const cacheConfig = new CacheConfig()
|
|
if (cacheConfig.isCacheDisabled()) {
|
|
await downloadGradleDistribution(versionInfo, downloadPath)
|
|
return downloadPath
|
|
}
|
|
|
|
const cacheKey = `gradle-${versionInfo.version}`
|
|
try {
|
|
const restoreKey = await cache.restoreCache([downloadPath], cacheKey)
|
|
if (restoreKey) {
|
|
core.info(`Restored Gradle distribution ${cacheKey} from cache to ${downloadPath}`)
|
|
return downloadPath
|
|
}
|
|
} catch (error) {
|
|
handleCacheFailure(error, `Restore Gradle distribution ${versionInfo.version} failed`)
|
|
}
|
|
|
|
core.info(`Gradle distribution ${versionInfo.version} not found in cache. Will download.`)
|
|
await downloadGradleDistribution(versionInfo, downloadPath)
|
|
|
|
if (!cacheConfig.isCacheReadOnly()) {
|
|
try {
|
|
await cache.saveCache([downloadPath], cacheKey)
|
|
} catch (error) {
|
|
handleCacheFailure(error, `Save Gradle distribution ${versionInfo.version} failed`)
|
|
}
|
|
}
|
|
return downloadPath
|
|
}
|
|
|
|
function getProvisionDir(): string {
|
|
const tmpDir = process.env['RUNNER_TEMP'] ?? os.tmpdir()
|
|
return path.join(tmpDir, `.gradle-actions/gradle-installations`)
|
|
}
|
|
|
|
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})`)
|
|
}
|
|
|
|
function executableFrom(installDir: string): string {
|
|
return path.join(installDir, 'bin', `${gradlew.installScriptFilename()}`)
|
|
}
|
|
|
|
async function httpGetGradleVersion(url: string): Promise<GradleVersionInfo> {
|
|
return JSON.parse(await httpGetString(url))
|
|
}
|
|
|
|
async function httpGetGradleVersions(url: string): Promise<GradleVersionInfo[]> {
|
|
return JSON.parse(await httpGetString(url))
|
|
}
|
|
|
|
async function httpGetString(url: string): Promise<string> {
|
|
const httpClient = new httpm.HttpClient('gradle/actions')
|
|
const response = await httpClient.get(url)
|
|
return response.readBody()
|
|
}
|
|
|
|
interface GradleVersionInfo {
|
|
version: string
|
|
downloadUrl: string
|
|
}
|
|
|
|
function handleCacheFailure(error: unknown, message: string): void {
|
|
if (error instanceof cache.ValidationError) {
|
|
// Fail on cache validation errors
|
|
throw error
|
|
}
|
|
if (error instanceof cache.ReserveCacheError) {
|
|
// Reserve cache errors are expected if the artifact has been previously cached
|
|
core.info(`${message}: ${error}`)
|
|
} else {
|
|
// Warn on all other errors
|
|
core.warning(`${message}: ${error}`)
|
|
if (error instanceof Error && error.stack) {
|
|
core.info(error.stack)
|
|
}
|
|
}
|
|
}
|