gradle-build-action/src/main.ts
Daz DeBoer 15a8123fbc Validate presense of gradle wrapper to provide better feedback
- Provide a more useful error message when no Gradle wrapper can be located,
  and 'gradle-version' or 'gradle-executable' is not used.
- Add test for case where wrapper is missing.
  This isn't really a "test" per-se, but this failing build invocation makes it
  easy to verify the GitHub action behaviour when the build is misconfigured.
2021-07-08 08:01:44 -06:00

83 lines
2.7 KiB
TypeScript

import * as core from '@actions/core'
import * as path from 'path'
import {parseArgsStringToArgv} from 'string-argv'
import * as github from './github-utils'
import * as cacheWrapper from './cache-wrapper'
import * as execution from './execution'
import * as gradlew from './gradlew'
import * as provision from './provision'
// Invoked by GitHub Actions
export async function run(): Promise<void> {
try {
const workspaceDirectory = process.env[`GITHUB_WORKSPACE`] || ''
const buildRootDirectory = resolveBuildRootDirectory(workspaceDirectory)
const result = await execution.execute(
await resolveGradleExecutable(
workspaceDirectory,
buildRootDirectory
),
buildRootDirectory,
parseCommandLineArguments()
)
if (result.buildScanUrl) {
core.setOutput('build-scan-url', result.buildScanUrl)
}
if (result.status !== 0) {
core.setFailed(`Gradle process exited with status ${result.status}`)
}
} catch (error) {
core.setFailed(error.message)
}
}
run()
async function resolveGradleExecutable(
workspaceDirectory: string,
buildRootDirectory: string
): Promise<string> {
const gradleVersion = github.inputOrNull('gradle-version')
if (gradleVersion !== null && gradleVersion !== 'wrapper') {
return path.resolve(await provision.gradleVersion(gradleVersion))
}
const gradleExecutable = github.inputOrNull('gradle-executable')
if (gradleExecutable !== null) {
if (gradleExecutable.endsWith(gradlew.wrapperFilename())) {
await cacheWrapper.restoreCachedWrapperDist(
path.resolve(gradleExecutable, '..')
)
}
return path.resolve(workspaceDirectory, gradleExecutable)
}
const wrapperDirectory = github.inputOrNull('wrapper-directory')
const gradlewDirectory =
wrapperDirectory !== null
? path.resolve(workspaceDirectory, wrapperDirectory)
: buildRootDirectory
gradlew.validateGradleWrapper(gradlewDirectory)
await cacheWrapper.restoreCachedWrapperDist(gradlewDirectory)
return path.resolve(gradlewDirectory, gradlew.wrapperFilename())
}
function resolveBuildRootDirectory(baseDirectory: string): string {
const buildRootDirectory = github.inputOrNull('build-root-directory')
const resolvedBuildRootDirectory =
buildRootDirectory === null
? path.resolve(baseDirectory)
: path.resolve(baseDirectory, buildRootDirectory)
return resolvedBuildRootDirectory
}
function parseCommandLineArguments(): string[] {
const input = github.inputOrNull('arguments')
return input === null ? [] : parseArgsStringToArgv(input)
}