Use pre-installed Gradle when version matches

By executing Gradle on the PATH, we can avoid downloading and installing
a Gradle version that is already available on the runner.

Fixes #270
This commit is contained in:
daz 2024-07-19 12:19:14 -06:00
parent 7da993afd5
commit 5d7c18409c
No known key found for this signature in database
3 changed files with 20 additions and 6 deletions

View file

@ -24,9 +24,6 @@ jobs:
fail-fast: false fail-fast: false
matrix: matrix:
os: ${{fromJSON(inputs.runner-os)}} os: ${{fromJSON(inputs.runner-os)}}
include:
- os: windows-latest
script-suffix: '.bat'
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
steps: steps:
- name: Checkout sources - name: Checkout sources

View file

@ -25,9 +25,6 @@ jobs:
fail-fast: false fail-fast: false
matrix: matrix:
os: ${{fromJSON(inputs.runner-os)}} os: ${{fromJSON(inputs.runner-os)}}
include:
- os: windows-latest
script-suffix: '.bat'
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
steps: steps:
- name: Checkout sources - name: Checkout sources

View file

@ -1,9 +1,11 @@
import * as fs from 'fs' import * as fs from 'fs'
import * as os from 'os' import * as os from 'os'
import * as path from 'path' import * as path from 'path'
import which from 'which'
import * as httpm from '@actions/http-client' import * as httpm from '@actions/http-client'
import * as core from '@actions/core' import * as core from '@actions/core'
import * as cache from '@actions/cache' import * as cache from '@actions/cache'
import * as exec from '@actions/exec'
import * as toolCache from '@actions/tool-cache' import * as toolCache from '@actions/tool-cache'
import * as gradlew from './gradlew' import * as gradlew from './gradlew'
@ -95,6 +97,12 @@ async function findGradleVersionDeclaration(version: string): Promise<GradleVers
async function installGradleVersion(versionInfo: GradleVersionInfo): Promise<string> { async function installGradleVersion(versionInfo: GradleVersionInfo): Promise<string> {
return core.group(`Provision Gradle ${versionInfo.version}`, async () => { return core.group(`Provision Gradle ${versionInfo.version}`, async () => {
const preInstalledGradle = await findGradleVersionOnPath(versionInfo)
if (preInstalledGradle !== undefined) {
core.info(`Gradle version ${versionInfo.version} is already available on PATH. Not installing.`)
return preInstalledGradle
}
return locateGradleAndDownloadIfRequired(versionInfo) return locateGradleAndDownloadIfRequired(versionInfo)
}) })
} }
@ -184,3 +192,15 @@ interface GradleVersionInfo {
version: string version: string
downloadUrl: 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(`Gradle ${versionInfo.version}`)) {
return gradleExecutable
}
}
return undefined
}