2020-06-13 13:44:30 +02:00
|
|
|
import * as fs from 'fs'
|
2020-06-14 12:28:17 +02:00
|
|
|
import * as os from 'os'
|
2020-06-13 13:44:30 +02:00
|
|
|
import * as path from 'path'
|
2020-09-13 14:17:37 +02:00
|
|
|
import * as httpm from '@actions/http-client'
|
2020-06-13 13:44:30 +02:00
|
|
|
import * as core from '@actions/core'
|
2021-07-08 12:22:48 -06:00
|
|
|
import * as cache from '@actions/cache'
|
2020-06-13 13:44:30 +02:00
|
|
|
import * as toolCache from '@actions/tool-cache'
|
2019-09-21 16:01:53 +02:00
|
|
|
|
2020-06-13 13:44:30 +02:00
|
|
|
import * as gradlew from './gradlew'
|
2023-06-03 15:58:54 -06:00
|
|
|
import * as params from './input-params'
|
2023-06-03 14:09:52 -06:00
|
|
|
import * as layout from './repository-layout'
|
2021-11-05 06:54:31 -06:00
|
|
|
import {handleCacheFailure, isCacheDisabled, isCacheReadOnly} from './cache-utils'
|
2019-09-23 12:12:03 +02:00
|
|
|
|
2020-06-13 13:44:30 +02:00
|
|
|
const gradleVersionsBaseUrl = 'https://services.gradle.org/versions'
|
2019-09-23 12:12:03 +02:00
|
|
|
|
2019-09-21 16:01:53 +02:00
|
|
|
/**
|
2023-06-03 14:09:52 -06:00
|
|
|
* Install any configured version of Gradle, adding the executable to the PATH.
|
|
|
|
* @return Installed Gradle executable or undefined if no version configured.
|
2019-09-21 16:01:53 +02:00
|
|
|
*/
|
2023-06-03 14:09:52 -06:00
|
|
|
export async function provisionGradle(): Promise<string | undefined> {
|
2023-06-03 15:58:54 -06:00
|
|
|
const gradleVersion = params.getGradleVersion()
|
2023-06-03 14:09:52 -06:00
|
|
|
if (gradleVersion !== '' && gradleVersion !== 'wrapper') {
|
|
|
|
return addToPath(path.resolve(await installGradle(gradleVersion)))
|
|
|
|
}
|
|
|
|
|
2023-06-03 15:58:54 -06:00
|
|
|
const gradleExecutable = params.getGradleExecutable()
|
2023-06-03 14:09:52 -06:00
|
|
|
if (gradleExecutable !== '') {
|
|
|
|
const workspaceDirectory = layout.workspaceDirectory()
|
|
|
|
return addToPath(path.resolve(workspaceDirectory, gradleExecutable))
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
async function addToPath(executable: string): Promise<string> {
|
|
|
|
core.addPath(path.dirname(executable))
|
|
|
|
return executable
|
|
|
|
}
|
|
|
|
|
|
|
|
async function installGradle(version: string): Promise<string> {
|
2020-06-13 13:54:27 +02:00
|
|
|
switch (version) {
|
2020-06-13 13:44:30 +02:00
|
|
|
case 'current':
|
|
|
|
return gradleCurrent()
|
|
|
|
case 'rc':
|
2021-10-29 07:34:44 -06:00
|
|
|
core.warning(`Specifying gradle-version 'rc' has been deprecated. Use 'release-candidate' instead.`)
|
2021-06-24 15:59:03 -07:00
|
|
|
return gradleReleaseCandidate()
|
|
|
|
case 'release-candidate':
|
2020-06-13 13:44:30 +02:00
|
|
|
return gradleReleaseCandidate()
|
|
|
|
case 'nightly':
|
|
|
|
return gradleNightly()
|
|
|
|
case 'release-nightly':
|
|
|
|
return gradleReleaseNightly()
|
2019-09-21 16:01:53 +02:00
|
|
|
default:
|
2020-06-13 13:54:27 +02:00
|
|
|
return gradle(version)
|
2019-09-21 16:01:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function gradleCurrent(): Promise<string> {
|
2021-10-29 07:34:44 -06:00
|
|
|
const versionInfo = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/current`)
|
2023-06-03 14:09:52 -06:00
|
|
|
return installGradleVersion(versionInfo)
|
2019-09-21 16:01:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function gradleReleaseCandidate(): Promise<string> {
|
2021-10-29 07:34:44 -06:00
|
|
|
const versionInfo = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/release-candidate`)
|
2020-09-13 13:30:54 +02:00
|
|
|
if (versionInfo && versionInfo.version && versionInfo.downloadUrl) {
|
2023-06-03 14:09:52 -06:00
|
|
|
return installGradleVersion(versionInfo)
|
2019-09-21 16:01:53 +02:00
|
|
|
}
|
2020-09-13 13:30:54 +02:00
|
|
|
core.info('No current release-candidate found, will fallback to current')
|
2020-06-13 13:44:30 +02:00
|
|
|
return gradleCurrent()
|
2019-09-21 16:01:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function gradleNightly(): Promise<string> {
|
2021-10-29 07:34:44 -06:00
|
|
|
const versionInfo = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/nightly`)
|
2023-06-03 14:09:52 -06:00
|
|
|
return installGradleVersion(versionInfo)
|
2019-09-21 16:01:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function gradleReleaseNightly(): Promise<string> {
|
2021-10-29 07:34:44 -06:00
|
|
|
const versionInfo = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/release-nightly`)
|
2023-06-03 14:09:52 -06:00
|
|
|
return installGradleVersion(versionInfo)
|
2019-09-21 16:01:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function gradle(version: string): Promise<string> {
|
2020-06-13 14:11:33 +02:00
|
|
|
const versionInfo = await findGradleVersionDeclaration(version)
|
|
|
|
if (!versionInfo) {
|
2020-06-13 13:44:30 +02:00
|
|
|
throw new Error(`Gradle version ${version} does not exists`)
|
2019-09-21 16:01:53 +02:00
|
|
|
}
|
2023-06-03 14:09:52 -06:00
|
|
|
return installGradleVersion(versionInfo)
|
2019-09-21 16:01:53 +02:00
|
|
|
}
|
|
|
|
|
2021-10-29 07:34:44 -06:00
|
|
|
async function gradleVersionDeclaration(url: string): Promise<GradleVersionInfo> {
|
2020-06-13 14:11:33 +02:00
|
|
|
return await httpGetGradleVersion(url)
|
2019-09-21 16:01:53 +02:00
|
|
|
}
|
|
|
|
|
2021-10-29 07:34:44 -06:00
|
|
|
async function findGradleVersionDeclaration(version: string): Promise<GradleVersionInfo | undefined> {
|
|
|
|
const gradleVersions = await httpGetGradleVersions(`${gradleVersionsBaseUrl}/all`)
|
2020-06-13 14:11:33 +02:00
|
|
|
return gradleVersions.find((entry: GradleVersionInfo) => {
|
2020-06-13 13:44:30 +02:00
|
|
|
return entry.version === version
|
|
|
|
})
|
2019-09-21 16:01:53 +02:00
|
|
|
}
|
|
|
|
|
2023-06-03 14:09:52 -06:00
|
|
|
async function installGradleVersion(versionInfo: GradleVersionInfo): Promise<string> {
|
2021-09-13 11:47:32 -06:00
|
|
|
return core.group(`Provision Gradle ${versionInfo.version}`, async () => {
|
|
|
|
return locateGradleAndDownloadIfRequired(versionInfo)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-29 07:34:44 -06:00
|
|
|
async function locateGradleAndDownloadIfRequired(versionInfo: GradleVersionInfo): Promise<string> {
|
2021-07-08 12:22:48 -06:00
|
|
|
const installsDir = path.join(os.homedir(), 'gradle-installations/installs')
|
|
|
|
const installDir = path.join(installsDir, `gradle-${versionInfo.version}`)
|
|
|
|
if (fs.existsSync(installDir)) {
|
|
|
|
core.info(`Gradle installation already exists at ${installDir}`)
|
|
|
|
return executableFrom(installDir)
|
2019-09-21 16:01:53 +02:00
|
|
|
}
|
|
|
|
|
2021-07-08 12:22:48 -06:00
|
|
|
const downloadPath = await downloadAndCacheGradleDistribution(versionInfo)
|
2021-07-05 13:17:31 -06:00
|
|
|
await toolCache.extractZip(downloadPath, installsDir)
|
2021-07-08 12:22:48 -06:00
|
|
|
core.info(`Extracted Gradle ${versionInfo.version} to ${installDir}`)
|
2019-09-21 16:01:53 +02:00
|
|
|
|
2020-06-13 13:44:30 +02:00
|
|
|
const executable = executableFrom(installDir)
|
|
|
|
fs.chmodSync(executable, '755')
|
|
|
|
core.info(`Provisioned Gradle executable ${executable}`)
|
2019-09-21 16:01:53 +02:00
|
|
|
|
2020-06-13 13:44:30 +02:00
|
|
|
return executable
|
2019-09-21 16:01:53 +02:00
|
|
|
}
|
|
|
|
|
2021-10-29 07:34:44 -06:00
|
|
|
async function downloadAndCacheGradleDistribution(versionInfo: GradleVersionInfo): Promise<string> {
|
|
|
|
const downloadPath = path.join(os.homedir(), `gradle-installations/downloads/gradle-${versionInfo.version}-bin.zip`)
|
2021-07-08 12:22:48 -06:00
|
|
|
|
2021-09-12 14:26:38 -06:00
|
|
|
if (isCacheDisabled()) {
|
2021-07-08 12:22:48 -06:00
|
|
|
await downloadGradleDistribution(versionInfo, downloadPath)
|
|
|
|
return downloadPath
|
|
|
|
}
|
|
|
|
|
|
|
|
const cacheKey = `gradle-${versionInfo.version}`
|
2021-11-05 06:54:31 -06:00
|
|
|
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`)
|
2021-08-24 12:57:17 -06:00
|
|
|
}
|
2021-11-05 06:54:31 -06:00
|
|
|
|
2021-10-29 07:34:44 -06:00
|
|
|
core.info(`Gradle distribution ${versionInfo.version} not found in cache. Will download.`)
|
2021-08-24 12:57:17 -06:00
|
|
|
await downloadGradleDistribution(versionInfo, downloadPath)
|
2021-07-08 12:22:48 -06:00
|
|
|
|
2021-09-12 14:26:38 -06:00
|
|
|
if (!isCacheReadOnly()) {
|
2021-07-08 12:22:48 -06:00
|
|
|
try {
|
|
|
|
await cache.saveCache([downloadPath], cacheKey)
|
|
|
|
} catch (error) {
|
2021-11-05 06:54:31 -06:00
|
|
|
handleCacheFailure(error, `Save Gradle distribution ${versionInfo.version} failed`)
|
2021-07-08 12:22:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return downloadPath
|
|
|
|
}
|
|
|
|
|
2021-10-29 07:34:44 -06:00
|
|
|
async function downloadGradleDistribution(versionInfo: GradleVersionInfo, downloadPath: string): Promise<void> {
|
2021-07-08 12:22:48 -06:00
|
|
|
await toolCache.downloadTool(versionInfo.downloadUrl, downloadPath)
|
2021-10-29 07:34:44 -06:00
|
|
|
core.info(`Downloaded ${versionInfo.downloadUrl} to ${downloadPath} (size ${fs.statSync(downloadPath).size})`)
|
2021-07-08 12:22:48 -06:00
|
|
|
}
|
|
|
|
|
2019-09-21 16:01:53 +02:00
|
|
|
function executableFrom(installDir: string): string {
|
2020-06-13 13:44:30 +02:00
|
|
|
return path.join(installDir, 'bin', `${gradlew.installScriptFilename()}`)
|
2019-09-21 16:01:53 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 14:11:33 +02:00
|
|
|
async function httpGetGradleVersion(url: string): Promise<GradleVersionInfo> {
|
|
|
|
return JSON.parse(await httpGetString(url))
|
|
|
|
}
|
|
|
|
|
2021-10-29 07:34:44 -06:00
|
|
|
async function httpGetGradleVersions(url: string): Promise<GradleVersionInfo[]> {
|
2020-06-13 14:11:33 +02:00
|
|
|
return JSON.parse(await httpGetString(url))
|
|
|
|
}
|
|
|
|
|
|
|
|
async function httpGetString(url: string): Promise<string> {
|
2021-08-07 15:17:27 -07:00
|
|
|
const httpClient = new httpm.HttpClient('gradle/gradle-build-action')
|
2021-07-05 13:17:31 -06:00
|
|
|
const response = await httpClient.get(url)
|
2020-06-13 14:11:33 +02:00
|
|
|
return response.readBody()
|
2019-09-23 12:12:03 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 14:11:33 +02:00
|
|
|
interface GradleVersionInfo {
|
|
|
|
version: string
|
|
|
|
downloadUrl: string
|
|
|
|
}
|