mirror of
				https://github.com/gradle/gradle-build-action.git
				synced 2025-11-04 09:58:56 +08:00 
			
		
		
		
	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:
		
							
								
								
									
										2
									
								
								dist/main/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								dist/main/index.js
									
									
									
									
										vendored
									
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
							
								
								
									
										2
									
								
								dist/main/index.js.map
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								dist/main/index.js.map
									
									
									
									
										vendored
									
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							@@ -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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										52
									
								
								src/gradle-init.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								src/gradle-init.ts
									
									
									
									
									
										Normal 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
 | 
			
		||||
}
 | 
			
		||||
@@ -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(
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user