gradle-build-action/src/provision.ts

188 lines
6.8 KiB
TypeScript
Raw Normal View History

2020-06-13 11:44:30 +00:00
import * as fs from 'fs'
2020-06-14 10:28:17 +00:00
import * as os from 'os'
2020-06-13 11:44:30 +00:00
import * as path from 'path'
import * as httpm from '@actions/http-client'
2020-06-13 11:44:30 +00:00
import * as core from '@actions/core'
import * as cache from '@actions/cache'
2020-06-13 11:44:30 +00:00
import * as toolCache from '@actions/tool-cache'
2019-09-21 14:01:53 +00:00
2020-06-13 11:44:30 +00:00
import * as gradlew from './gradlew'
import * as params from './input-params'
import * as layout from './repository-layout'
import {handleCacheFailure, isCacheDisabled, isCacheReadOnly} from './cache-utils'
2019-09-23 10:12:03 +00:00
2020-06-13 11:44:30 +00:00
const gradleVersionsBaseUrl = 'https://services.gradle.org/versions'
2019-09-23 10:12:03 +00:00
2019-09-21 14:01:53 +00: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 14:01:53 +00:00
*/
export async function provisionGradle(): Promise<string | undefined> {
const gradleVersion = params.getGradleVersion()
if (gradleVersion !== '' && gradleVersion !== 'wrapper') {
return addToPath(path.resolve(await installGradle(gradleVersion)))
}
const gradleExecutable = params.getGradleExecutable()
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> {
const versionInfo = await resolveGradleVersion(version)
core.setOutput('gradle-version', versionInfo.version)
return installGradleVersion(versionInfo)
}
async function resolveGradleVersion(version: string): Promise<GradleVersionInfo> {
2020-06-13 11:54:27 +00:00
switch (version) {
2020-06-13 11:44:30 +00:00
case 'current':
return gradleCurrent()
case 'rc':
core.warning(`Specifying gradle-version 'rc' has been deprecated. Use 'release-candidate' instead.`)
return gradleReleaseCandidate()
case 'release-candidate':
2020-06-13 11:44:30 +00:00
return gradleReleaseCandidate()
case 'nightly':
return gradleNightly()
case 'release-nightly':
return gradleReleaseNightly()
2019-09-21 14:01:53 +00:00
default:
2020-06-13 11:54:27 +00:00
return gradle(version)
2019-09-21 14:01:53 +00:00
}
}
async function gradleCurrent(): Promise<GradleVersionInfo> {
return await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/current`)
2019-09-21 14:01:53 +00:00
}
async function gradleReleaseCandidate(): Promise<GradleVersionInfo> {
const versionInfo = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/release-candidate`)
if (versionInfo && versionInfo.version && versionInfo.downloadUrl) {
return versionInfo
2019-09-21 14:01:53 +00:00
}
core.info('No current release-candidate found, will fallback to current')
2020-06-13 11:44:30 +00:00
return gradleCurrent()
2019-09-21 14:01:53 +00:00
}
async function gradleNightly(): Promise<GradleVersionInfo> {
return await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/nightly`)
2019-09-21 14:01:53 +00:00
}
async function gradleReleaseNightly(): Promise<GradleVersionInfo> {
return await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/release-nightly`)
2019-09-21 14:01:53 +00:00
}
async function gradle(version: string): Promise<GradleVersionInfo> {
2020-06-13 12:11:33 +00:00
const versionInfo = await findGradleVersionDeclaration(version)
if (!versionInfo) {
2020-06-13 11:44:30 +00:00
throw new Error(`Gradle version ${version} does not exists`)
2019-09-21 14:01:53 +00:00
}
return versionInfo
2019-09-21 14:01:53 +00:00
}
async function gradleVersionDeclaration(url: string): Promise<GradleVersionInfo> {
2020-06-13 12:11:33 +00:00
return await httpGetGradleVersion(url)
2019-09-21 14:01:53 +00:00
}
async function findGradleVersionDeclaration(version: string): Promise<GradleVersionInfo | undefined> {
const gradleVersions = await httpGetGradleVersions(`${gradleVersionsBaseUrl}/all`)
2020-06-13 12:11:33 +00:00
return gradleVersions.find((entry: GradleVersionInfo) => {
2020-06-13 11:44:30 +00:00
return entry.version === version
})
2019-09-21 14:01:53 +00:00
}
async function installGradleVersion(versionInfo: GradleVersionInfo): Promise<string> {
return core.group(`Provision Gradle ${versionInfo.version}`, async () => {
return locateGradleAndDownloadIfRequired(versionInfo)
})
}
async function locateGradleAndDownloadIfRequired(versionInfo: GradleVersionInfo): Promise<string> {
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 14:01:53 +00:00
}
const downloadPath = await downloadAndCacheGradleDistribution(versionInfo)
await toolCache.extractZip(downloadPath, installsDir)
core.info(`Extracted Gradle ${versionInfo.version} to ${installDir}`)
2019-09-21 14:01:53 +00:00
2020-06-13 11:44:30 +00:00
const executable = executableFrom(installDir)
fs.chmodSync(executable, '755')
core.info(`Provisioned Gradle executable ${executable}`)
2019-09-21 14:01:53 +00:00
2020-06-13 11:44:30 +00:00
return executable
2019-09-21 14:01:53 +00:00
}
async function downloadAndCacheGradleDistribution(versionInfo: GradleVersionInfo): Promise<string> {
const downloadPath = path.join(os.homedir(), `gradle-installations/downloads/gradle-${versionInfo.version}-bin.zip`)
if (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 (!isCacheReadOnly()) {
try {
await cache.saveCache([downloadPath], cacheKey)
} catch (error) {
handleCacheFailure(error, `Save Gradle distribution ${versionInfo.version} failed`)
}
}
return downloadPath
}
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})`)
}
2019-09-21 14:01:53 +00:00
function executableFrom(installDir: string): string {
2020-06-13 11:44:30 +00:00
return path.join(installDir, 'bin', `${gradlew.installScriptFilename()}`)
2019-09-21 14:01:53 +00:00
}
2020-06-13 12:11:33 +00:00
async function httpGetGradleVersion(url: string): Promise<GradleVersionInfo> {
return JSON.parse(await httpGetString(url))
}
async function httpGetGradleVersions(url: string): Promise<GradleVersionInfo[]> {
2020-06-13 12:11:33 +00:00
return JSON.parse(await httpGetString(url))
}
async function httpGetString(url: string): Promise<string> {
const httpClient = new httpm.HttpClient('gradle/gradle-build-action')
const response = await httpClient.get(url)
2020-06-13 12:11:33 +00:00
return response.readBody()
2019-09-23 10:12:03 +00:00
}
2020-06-13 12:11:33 +00:00
interface GradleVersionInfo {
version: string
downloadUrl: string
}