gradle-build-action/src/main.ts
Daz DeBoer 0e8b9655a0
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.
2021-12-17 10:29:58 -07:00

75 lines
2.5 KiB
TypeScript

import * as core from '@actions/core'
import * as path from 'path'
import * as os from 'os'
import {parseArgsStringToArgv} from 'string-argv'
import * as caches from './caches'
import * as execution from './execution'
import * as provision from './provision'
/**
* The main entry point for the action, called by Github Actions for the step.
*/
export async function run(): Promise<void> {
try {
const workspaceDirectory = process.env[`GITHUB_WORKSPACE`] || ''
const buildRootDirectory = resolveBuildRootDirectory(workspaceDirectory)
const gradleUserHome = determineGradleUserHome(buildRootDirectory)
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()
if (args.length > 0) {
await execution.executeGradleBuild(executable, buildRootDirectory, args)
}
} catch (error) {
core.setFailed(String(error))
if (error instanceof Error && error.stack) {
core.info(error.stack)
}
}
}
run()
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))
}
const gradleExecutable = core.getInput('gradle-executable')
if (gradleExecutable !== '') {
return path.resolve(workspaceDirectory, gradleExecutable)
}
return undefined
}
function resolveBuildRootDirectory(baseDirectory: string): string {
const buildRootDirectory = core.getInput('build-root-directory')
const resolvedBuildRootDirectory =
buildRootDirectory === '' ? path.resolve(baseDirectory) : path.resolve(baseDirectory, buildRootDirectory)
return resolvedBuildRootDirectory
}
function determineGradleUserHome(rootDir: string): string {
const customGradleUserHome = process.env['GRADLE_USER_HOME']
if (customGradleUserHome) {
return path.resolve(rootDir, customGradleUserHome)
}
return path.resolve(os.homedir(), '.gradle')
}
function parseCommandLineArguments(): string[] {
const input = core.getInput('arguments')
return parseArgsStringToArgv(input)
}