Merge pull request #85 from gradle/dd/init-script

Use an init-script to capture buildScanPublished event
This commit is contained in:
Daz DeBoer 2021-09-28 00:08:42 -06:00 committed by GitHub
commit 204870af89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 90 additions and 20 deletions

View File

@ -1,5 +1,5 @@
plugins { plugins {
id("com.gradle.enterprise") version("3.6.4") id("com.gradle.enterprise") version("3.7")
} }
gradleEnterprise { gradleEnterprise {

View File

@ -1,3 +1,16 @@
plugins {
id("com.gradle.enterprise") version("3.7")
}
gradleEnterprise {
buildScan {
termsOfServiceUrl = "https://gradle.com/terms-of-service"
termsOfServiceAgree = "yes"
publishAlways()
uploadInBackground = false
}
}
rootProject.name = 'no-wrapper' rootProject.name = 'no-wrapper'
println "Using Gradle version: ${gradle.gradleVersion}" println "Using Gradle version: ${gradle.gradleVersion}"

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

52
src/build-scan-capture.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

@ -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 './build-scan-capture'
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)
} }

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(