Skip to content

Commit

Permalink
Use init script to capture build scan URL
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bigdaz committed Sep 28, 2021
1 parent 5576baa commit a7174b8
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 19 deletions.
2 changes: 1 addition & 1 deletion dist/main/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/main/index.js.map

Large diffs are not rendered by default.

37 changes: 22 additions & 15 deletions src/execution.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
import * as exec from '@actions/exec'
import fs from 'fs'
import path from 'path'
import {writeInitScript} from './gradle-init'

export async function execute(
executable: string,
root: string,
argv: string[]
args: string[]
): Promise<BuildResult> {
let publishing = false
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,
ignoreReturnCode: true,
listeners: {
stdline: (line: string) => {
if (line.includes('Publishing build scan...')) {
publishing = true
}
if (publishing && line.startsWith('http')) {
buildScanUrl = line.trim()
publishing = false
}
}
}
ignoreReturnCode: true
})

if (fs.existsSync(buildScanFile)) {
buildScanUrl = fs.readFileSync(buildScanFile, 'utf-8')
}

return new BuildResultImpl(status, buildScanUrl)
}

Expand Down
52 changes: 52 additions & 0 deletions src/gradle-init.ts
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 0 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ export async function run(): Promise<void> {
await caches.restore(buildRootDirectory)

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(
await resolveGradleExecutable(
Expand Down

0 comments on commit a7174b8

Please sign in to comment.