Unverified Commit 0e8b9655 authored by Daz DeBoer's avatar Daz DeBoer
Browse files

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.
parent 367ce74a
Loading
Loading
Loading
Loading
+11 −10
Changes for src/execution.ts: 11 added lines, 10 removed lines.
Original line number Diff line number Diff line
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import fs from 'fs'
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

    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)
    }

    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,
        ignoreReturnCode: true
    })
@@ -19,14 +23,11 @@ export async function execute(executable: string, root: string, args: string[]):
        buildScanUrl = fs.readFileSync(buildScanFile, 'utf-8')
    }

    return new BuildResultImpl(status, buildScanUrl)
    if (status !== 0) {
        if (buildScanUrl) {
            core.setFailed(`Gradle build failed: ${buildScanUrl}`)
        } else {
            core.setFailed(`Gradle build failed: process exited with status ${status}`)
        }

export interface BuildResult {
    readonly status: number
    readonly buildScanUrl?: string
    }

class BuildResultImpl implements BuildResult {
    constructor(readonly status: number, readonly buildScanUrl?: string) {}
}
+11 −16
Changes for src/main.ts: 11 added lines, 16 removed lines.
Original line number Diff line number Diff line
@@ -5,7 +5,6 @@ import {parseArgsStringToArgv} from 'string-argv'

import * as caches from './caches'
import * as execution from './execution'
import * as gradlew from './gradlew'
import * as provision from './provision'

/**
@@ -19,20 +18,16 @@ export async function run(): Promise<void> {

        await caches.restore(gradleUserHome)

        const args: string[] = parseCommandLineArguments()

        const result = await execution.execute(
            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}`)
        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()
        if (args.length > 0) {
            await execution.executeGradleBuild(executable, buildRootDirectory, args)
        }
    } catch (error) {
        core.setFailed(String(error))
@@ -44,7 +39,7 @@ export async function run(): Promise<void> {

run()

async function resolveGradleExecutable(workspaceDirectory: string, buildRootDirectory: string): Promise<string> {
async function provisionGradle(workspaceDirectory: string): Promise<string | undefined> {
    const gradleVersion = core.getInput('gradle-version')
    if (gradleVersion !== '' && gradleVersion !== 'wrapper') {
        return path.resolve(await provision.gradleVersion(gradleVersion))
@@ -55,7 +50,7 @@ async function resolveGradleExecutable(workspaceDirectory: string, buildRootDire
        return path.resolve(workspaceDirectory, gradleExecutable)
    }

    return gradlew.locateGradleWrapperScript(buildRootDirectory)
    return undefined
}

function resolveBuildRootDirectory(baseDirectory: string): string {