mirror of
https://github.com/gradle/gradle-build-action.git
synced 2025-04-06 13:24:13 +02:00
Honour the wrapper-cache-enabled flag for downloaded distributions
This commit is contained in:
parent
c511d263a6
commit
4edc9657af
2 changed files with 52 additions and 28 deletions
2
dist/main/index.js
vendored
2
dist/main/index.js
vendored
File diff suppressed because one or more lines are too long
|
@ -7,6 +7,7 @@ import * as cache from '@actions/cache'
|
||||||
import * as toolCache from '@actions/tool-cache'
|
import * as toolCache from '@actions/tool-cache'
|
||||||
|
|
||||||
import * as gradlew from './gradlew'
|
import * as gradlew from './gradlew'
|
||||||
|
import * as github from './github-utils'
|
||||||
|
|
||||||
const gradleVersionsBaseUrl = 'https://services.gradle.org/versions'
|
const gradleVersionsBaseUrl = 'https://services.gradle.org/versions'
|
||||||
|
|
||||||
|
@ -32,7 +33,7 @@ async function gradleCurrent(): Promise<string> {
|
||||||
const versionInfo = await gradleVersionDeclaration(
|
const versionInfo = await gradleVersionDeclaration(
|
||||||
`${gradleVersionsBaseUrl}/current`
|
`${gradleVersionsBaseUrl}/current`
|
||||||
)
|
)
|
||||||
return provisionGradle(versionInfo.version, versionInfo.downloadUrl)
|
return provisionGradle(versionInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function gradleReleaseCandidate(): Promise<string> {
|
async function gradleReleaseCandidate(): Promise<string> {
|
||||||
|
@ -40,7 +41,7 @@ async function gradleReleaseCandidate(): Promise<string> {
|
||||||
`${gradleVersionsBaseUrl}/release-candidate`
|
`${gradleVersionsBaseUrl}/release-candidate`
|
||||||
)
|
)
|
||||||
if (versionInfo && versionInfo.version && versionInfo.downloadUrl) {
|
if (versionInfo && versionInfo.version && versionInfo.downloadUrl) {
|
||||||
return provisionGradle(versionInfo.version, versionInfo.downloadUrl)
|
return provisionGradle(versionInfo)
|
||||||
}
|
}
|
||||||
core.info('No current release-candidate found, will fallback to current')
|
core.info('No current release-candidate found, will fallback to current')
|
||||||
return gradleCurrent()
|
return gradleCurrent()
|
||||||
|
@ -50,14 +51,14 @@ async function gradleNightly(): Promise<string> {
|
||||||
const versionInfo = await gradleVersionDeclaration(
|
const versionInfo = await gradleVersionDeclaration(
|
||||||
`${gradleVersionsBaseUrl}/nightly`
|
`${gradleVersionsBaseUrl}/nightly`
|
||||||
)
|
)
|
||||||
return provisionGradle(versionInfo.version, versionInfo.downloadUrl)
|
return provisionGradle(versionInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function gradleReleaseNightly(): Promise<string> {
|
async function gradleReleaseNightly(): Promise<string> {
|
||||||
const versionInfo = await gradleVersionDeclaration(
|
const versionInfo = await gradleVersionDeclaration(
|
||||||
`${gradleVersionsBaseUrl}/release-nightly`
|
`${gradleVersionsBaseUrl}/release-nightly`
|
||||||
)
|
)
|
||||||
return provisionGradle(versionInfo.version, versionInfo.downloadUrl)
|
return provisionGradle(versionInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function gradle(version: string): Promise<string> {
|
async function gradle(version: string): Promise<string> {
|
||||||
|
@ -65,7 +66,7 @@ async function gradle(version: string): Promise<string> {
|
||||||
if (!versionInfo) {
|
if (!versionInfo) {
|
||||||
throw new Error(`Gradle version ${version} does not exists`)
|
throw new Error(`Gradle version ${version} does not exists`)
|
||||||
}
|
}
|
||||||
return provisionGradle(versionInfo.version, versionInfo.downloadUrl)
|
return provisionGradle(versionInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function gradleVersionDeclaration(
|
async function gradleVersionDeclaration(
|
||||||
|
@ -85,28 +86,45 @@ async function findGradleVersionDeclaration(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function provisionGradle(version: string, url: string): Promise<string> {
|
async function provisionGradle(
|
||||||
const tmpdir = path.join(os.homedir(), 'gradle-installations')
|
versionInfo: GradleVersionInfo
|
||||||
|
): Promise<string> {
|
||||||
|
const downloadPath = await downloadAndCacheGradleDistribution(versionInfo)
|
||||||
|
|
||||||
|
const installsDir = path.join(os.homedir(), 'gradle-installations/installs')
|
||||||
|
await toolCache.extractZip(downloadPath, installsDir)
|
||||||
|
const installDir = path.join(installsDir, `gradle-${versionInfo.version}`)
|
||||||
|
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(
|
const downloadPath = path.join(
|
||||||
tmpdir,
|
os.homedir(),
|
||||||
`downloads/gradle-${version}-bin.zip`
|
`gradle-installations/downloads/gradle-${versionInfo.version}-bin.zip`
|
||||||
)
|
)
|
||||||
|
|
||||||
const cacheKey = `gradle-${version}`
|
if (isDistributionCacheDisabled()) {
|
||||||
|
await downloadGradleDistribution(versionInfo, downloadPath)
|
||||||
|
return downloadPath
|
||||||
|
}
|
||||||
|
|
||||||
|
const cacheKey = `gradle-${versionInfo.version}`
|
||||||
const restoreKey = await cache.restoreCache([downloadPath], cacheKey)
|
const restoreKey = await cache.restoreCache([downloadPath], cacheKey)
|
||||||
if (restoreKey) {
|
if (restoreKey) {
|
||||||
core.info(`Restored Gradle distribution ${cacheKey} from cache`)
|
core.info(`Restored Gradle distribution ${cacheKey} from cache`)
|
||||||
} else {
|
} else {
|
||||||
core.info(
|
core.info(
|
||||||
`Gradle distribution ${cacheKey} not found in cache. Will download and cache.`
|
`Gradle distribution ${versionInfo.version} not found in cache. Will download.`
|
||||||
)
|
|
||||||
await toolCache.downloadTool(url, downloadPath)
|
|
||||||
core.info(
|
|
||||||
`Downloaded ${url} to ${downloadPath} (size ${
|
|
||||||
fs.statSync(downloadPath).size
|
|
||||||
})`
|
|
||||||
)
|
)
|
||||||
|
await downloadGradleDistribution(versionInfo, downloadPath)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await cache.saveCache([downloadPath], cacheKey)
|
await cache.saveCache([downloadPath], cacheKey)
|
||||||
|
@ -120,17 +138,19 @@ async function provisionGradle(version: string, url: string): Promise<string> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return downloadPath
|
||||||
|
}
|
||||||
|
|
||||||
const installsDir = path.join(tmpdir, 'installs')
|
async function downloadGradleDistribution(
|
||||||
await toolCache.extractZip(downloadPath, installsDir)
|
versionInfo: GradleVersionInfo,
|
||||||
const installDir = path.join(installsDir, `gradle-${version}`)
|
downloadPath: string
|
||||||
core.info(`Extracted Gradle ${version} to ${installDir}`)
|
): Promise<void> {
|
||||||
|
await toolCache.downloadTool(versionInfo.downloadUrl, downloadPath)
|
||||||
const executable = executableFrom(installDir)
|
core.info(
|
||||||
fs.chmodSync(executable, '755')
|
`Downloaded ${versionInfo.downloadUrl} to ${downloadPath} (size ${
|
||||||
core.info(`Provisioned Gradle executable ${executable}`)
|
fs.statSync(downloadPath).size
|
||||||
|
})`
|
||||||
return executable
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function executableFrom(installDir: string): string {
|
function executableFrom(installDir: string): string {
|
||||||
|
@ -153,6 +173,10 @@ async function httpGetString(url: string): Promise<string> {
|
||||||
return response.readBody()
|
return response.readBody()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isDistributionCacheDisabled(): boolean {
|
||||||
|
return !github.inputBoolean('wrapper-cache-enabled', true)
|
||||||
|
}
|
||||||
|
|
||||||
interface GradleVersionInfo {
|
interface GradleVersionInfo {
|
||||||
version: string
|
version: string
|
||||||
downloadUrl: string
|
downloadUrl: string
|
||||||
|
|
Loading…
Add table
Reference in a new issue