gradle-build-action/src/main.ts

86 lines
2.6 KiB
TypeScript
Raw Normal View History

2020-06-13 11:44:30 +00:00
import * as core from '@actions/core'
import * as path from 'path'
import {parseArgsStringToArgv} from 'string-argv'
2019-09-21 14:01:53 +00:00
import * as caches from './caches'
2020-06-13 11:44:30 +00:00
import * as execution from './execution'
import * as gradlew from './gradlew'
import * as provision from './provision'
2019-09-21 14:01:53 +00:00
2020-02-14 05:46:51 +00:00
// Invoked by GitHub Actions
2020-06-13 11:54:27 +00:00
export async function run(): Promise<void> {
try {
const workspaceDirectory = process.env[`GITHUB_WORKSPACE`] || ''
const buildRootDirectory = resolveBuildRootDirectory(workspaceDirectory)
await caches.restore(buildRootDirectory)
2019-09-21 14:01:53 +00:00
const args: string[] = parseCommandLineArguments()
2020-06-13 11:54:27 +00:00
const result = await execution.execute(
await resolveGradleExecutable(
workspaceDirectory,
buildRootDirectory
),
buildRootDirectory,
args
2020-06-13 11:44:30 +00:00
)
2019-09-21 14:01:53 +00:00
2019-09-23 10:11:18 +00:00
if (result.buildScanUrl) {
2020-06-13 11:44:30 +00:00
core.setOutput('build-scan-url', result.buildScanUrl)
2019-09-21 14:01:53 +00:00
}
2020-06-13 11:54:27 +00:00
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}`
)
}
} else {
if (result.buildScanUrl) {
core.notice(`Gradle build succeeded: ${result.buildScanUrl}`)
}
2019-09-23 10:11:18 +00:00
}
2019-09-21 14:01:53 +00:00
} catch (error) {
core.setFailed(String(error))
if (error instanceof Error && error.stack) {
core.info(error.stack)
}
2019-09-21 14:01:53 +00:00
}
2019-09-20 21:06:59 +00:00
}
2020-06-13 11:44:30 +00:00
run()
2019-09-21 14:01:53 +00:00
async function resolveGradleExecutable(
workspaceDirectory: string,
buildRootDirectory: string
): Promise<string> {
const gradleVersion = core.getInput('gradle-version')
if (gradleVersion !== '' && gradleVersion !== 'wrapper') {
2019-10-28 12:30:27 +00:00
return path.resolve(await provision.gradleVersion(gradleVersion))
2019-09-21 14:01:53 +00:00
}
const gradleExecutable = core.getInput('gradle-executable')
if (gradleExecutable !== '') {
return path.resolve(workspaceDirectory, gradleExecutable)
2019-09-21 14:01:53 +00:00
}
return gradlew.locateGradleWrapperScript(buildRootDirectory)
2019-09-21 14:01:53 +00:00
}
function resolveBuildRootDirectory(baseDirectory: string): string {
const buildRootDirectory = core.getInput('build-root-directory')
2020-06-13 14:02:48 +00:00
const resolvedBuildRootDirectory =
buildRootDirectory === ''
2020-06-13 14:02:48 +00:00
? path.resolve(baseDirectory)
: path.resolve(baseDirectory, buildRootDirectory)
return resolvedBuildRootDirectory
2019-09-21 14:01:53 +00:00
}
function parseCommandLineArguments(): string[] {
const input = core.getInput('arguments')
return parseArgsStringToArgv(input)
2019-09-21 14:01:53 +00:00
}