Allow action to be used without Gradle invocation

If the user supplies no 'arguments' parameter, the action will function as a
'setup-gradle' action, adding Gradle to the PATH and enabling other features
without actually running a Gradle build.

Any subsequent Gradle invocations in the workflow will benefit from:
- Save/restore of Gradle User Home
- Save/restore of configuration-cache data
- Capture of build-scan URLs

These features are enabled via Gradle User Home, so any Gradle invocation that
uses the same Gradle User Home will be included.
This commit is contained in:
Daz DeBoer 2021-12-08 09:05:04 -07:00
parent 367ce74a5f
commit 0e8b9655a0
No known key found for this signature in database
GPG Key ID: DD6B9F0B06683D5D
2 changed files with 24 additions and 28 deletions

View File

@ -1,8 +1,10 @@
import * as core from '@actions/core'
import * as exec from '@actions/exec' import * as exec from '@actions/exec'
import fs from 'fs' import fs from 'fs'
import path from 'path' import path from 'path'
import * as gradlew from './gradlew'
export async function execute(executable: string, root: string, args: string[]): Promise<BuildResult> { export async function executeGradleBuild(executable: string | undefined, root: string, args: string[]): Promise<void> {
let buildScanUrl: string | undefined let buildScanUrl: string | undefined
const buildScanFile = path.resolve(root, 'gradle-build-scan.txt') const buildScanFile = path.resolve(root, 'gradle-build-scan.txt')
@ -10,7 +12,9 @@ export async function execute(executable: string, root: string, args: string[]):
fs.unlinkSync(buildScanFile) fs.unlinkSync(buildScanFile)
} }
const status: number = await exec.exec(executable, args, { // Use the provided executable, or look for a Gradle wrapper script to run
const toExecute = executable ?? gradlew.locateGradleWrapperScript(root)
const status: number = await exec.exec(toExecute, args, {
cwd: root, cwd: root,
ignoreReturnCode: true ignoreReturnCode: true
}) })
@ -19,14 +23,11 @@ export async function execute(executable: string, root: string, args: string[]):
buildScanUrl = fs.readFileSync(buildScanFile, 'utf-8') buildScanUrl = fs.readFileSync(buildScanFile, 'utf-8')
} }
return new BuildResultImpl(status, buildScanUrl) if (status !== 0) {
} if (buildScanUrl) {
core.setFailed(`Gradle build failed: ${buildScanUrl}`)
export interface BuildResult { } else {
readonly status: number core.setFailed(`Gradle build failed: process exited with status ${status}`)
readonly buildScanUrl?: string }
} }
class BuildResultImpl implements BuildResult {
constructor(readonly status: number, readonly buildScanUrl?: string) {}
} }

View File

@ -5,7 +5,6 @@ import {parseArgsStringToArgv} from 'string-argv'
import * as caches from './caches' import * as caches from './caches'
import * as execution from './execution' import * as execution from './execution'
import * as gradlew from './gradlew'
import * as provision from './provision' import * as provision from './provision'
/** /**
@ -19,20 +18,16 @@ export async function run(): Promise<void> {
await caches.restore(gradleUserHome) await caches.restore(gradleUserHome)
const executable = await provisionGradle(workspaceDirectory)
// executable will be undefined if using Gradle wrapper
if (executable !== undefined) {
core.addPath(path.dirname(executable))
}
// Only execute if arguments have been provided
const args: string[] = parseCommandLineArguments() const args: string[] = parseCommandLineArguments()
if (args.length > 0) {
const result = await execution.execute( await execution.executeGradleBuild(executable, buildRootDirectory, args)
await resolveGradleExecutable(workspaceDirectory, buildRootDirectory),
buildRootDirectory,
args
)
if (result.status !== 0) {
if (result.buildScanUrl) {
core.setFailed(`Gradle build failed: ${result.buildScanUrl}`)
} else {
core.setFailed(`Gradle build failed: process exited with status ${result.status}`)
}
} }
} catch (error) { } catch (error) {
core.setFailed(String(error)) core.setFailed(String(error))
@ -44,7 +39,7 @@ export async function run(): Promise<void> {
run() run()
async function resolveGradleExecutable(workspaceDirectory: string, buildRootDirectory: string): Promise<string> { async function provisionGradle(workspaceDirectory: string): Promise<string | undefined> {
const gradleVersion = core.getInput('gradle-version') const gradleVersion = core.getInput('gradle-version')
if (gradleVersion !== '' && gradleVersion !== 'wrapper') { if (gradleVersion !== '' && gradleVersion !== 'wrapper') {
return path.resolve(await provision.gradleVersion(gradleVersion)) return path.resolve(await provision.gradleVersion(gradleVersion))
@ -55,7 +50,7 @@ async function resolveGradleExecutable(workspaceDirectory: string, buildRootDire
return path.resolve(workspaceDirectory, gradleExecutable) return path.resolve(workspaceDirectory, gradleExecutable)
} }
return gradlew.locateGradleWrapperScript(buildRootDirectory) return undefined
} }
function resolveBuildRootDirectory(baseDirectory: string): string { function resolveBuildRootDirectory(baseDirectory: string): string {