Use init script to capture build scan URL

Instead of parsing the log output, we instead register a
buildScanPublished listener and record the build scan URL
to a file. This file is subsequently read to report the
build scan URL.

Fixes #30
This commit is contained in:
Daz DeBoer 2021-09-27 22:54:22 -06:00
parent 5576baa56b
commit a7174b82a2
No known key found for this signature in database
GPG key ID: DD6B9F0B06683D5D
5 changed files with 76 additions and 19 deletions

2
dist/main/index.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -1,29 +1,36 @@
import * as exec from '@actions/exec' import * as exec from '@actions/exec'
import fs from 'fs'
import path from 'path'
import {writeInitScript} from './gradle-init'
export async function execute( export async function execute(
executable: string, executable: string,
root: string, root: string,
argv: string[] args: string[]
): Promise<BuildResult> { ): Promise<BuildResult> {
let publishing = false
let buildScanUrl: string | undefined let buildScanUrl: string | undefined
const status: number = await exec.exec(executable, argv, { // TODO: instead of running with no-daemon, run `--stop` in post action.
args.push('--no-daemon')
const initScript = writeInitScript()
args.push('--init-script')
args.push(initScript)
const buildScanFile = path.resolve(root, 'gradle-build-scan.txt')
if (fs.existsSync(buildScanFile)) {
fs.unlinkSync(buildScanFile)
}
const status: number = await exec.exec(executable, args, {
cwd: root, cwd: root,
ignoreReturnCode: true, ignoreReturnCode: true
listeners: {
stdline: (line: string) => {
if (line.includes('Publishing build scan...')) {
publishing = true
}
if (publishing && line.startsWith('http')) {
buildScanUrl = line.trim()
publishing = false
}
}
}
}) })
if (fs.existsSync(buildScanFile)) {
buildScanUrl = fs.readFileSync(buildScanFile, 'utf-8')
}
return new BuildResultImpl(status, buildScanUrl) return new BuildResultImpl(status, buildScanUrl)
} }

52
src/gradle-init.ts Normal file
View file

@ -0,0 +1,52 @@
import fs from 'fs'
import path from 'path'
import * as core from '@actions/core'
export function writeInitScript(): string {
const tmpDir = process.env['RUNNER_TEMP'] || ''
const initScript = path.resolve(tmpDir, 'build-scan-capture.init.gradle')
core.info(`Writing init script: ${initScript}`)
if (fs.existsSync(initScript)) {
return initScript
}
fs.writeFileSync(
initScript,
`
import org.gradle.util.GradleVersion
// Don't run against the included builds (if the main build has any).
def isTopLevelBuild = gradle.getParent() == null
if (isTopLevelBuild) {
def version = GradleVersion.current().baseVersion
def atLeastGradle5 = version >= GradleVersion.version("5.0")
def atLeastGradle6 = version >= GradleVersion.version("6.0")
if (atLeastGradle6) {
settingsEvaluated { settings ->
if (settings.pluginManager.hasPlugin("com.gradle.enterprise")) {
registerCallbacks(settings.extensions["gradleEnterprise"], settings.rootProject.name)
}
}
} else if (atLeastGradle5) {
projectsEvaluated { gradle ->
if (gradle.rootProject.pluginManager.hasPlugin("com.gradle.build-scan")) {
registerCallbacks(gradle.rootProject.extensions["gradleEnterprise"], gradle.rootProject.name)
}
}
}
}
def registerCallbacks(gradleEnterprise, rootProjectName) {
gradleEnterprise.with {
buildScan {
def scanFile = new File("gradle-build-scan.txt")
buildScanPublished { buildScan ->
scanFile.text = buildScan.buildScanUri
}
}
}
}
`
)
return initScript
}

View file

@ -16,8 +16,6 @@ export async function run(): Promise<void> {
await caches.restore(buildRootDirectory) await caches.restore(buildRootDirectory)
const args: string[] = parseCommandLineArguments() const args: string[] = parseCommandLineArguments()
// TODO: instead of running with no-daemon, run `--stop` in post action.
args.push('--no-daemon')
const result = await execution.execute( const result = await execution.execute(
await resolveGradleExecutable( await resolveGradleExecutable(