mirror of
				https://github.com/gradle/gradle-build-action.git
				synced 2025-11-04 09:58:56 +08:00 
			
		
		
		
	Initial impl
This commit is contained in:
		
							
								
								
									
										40
									
								
								src/execution.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								src/execution.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,40 @@
 | 
			
		||||
import * as exec from "@actions/exec";
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
export async function execute(executable: string, root: string, argv: string[]): Promise<BuildResult> {
 | 
			
		||||
 | 
			
		||||
    let publishing = false;
 | 
			
		||||
    let buildScanLink: any = null;
 | 
			
		||||
 | 
			
		||||
    await exec.exec(executable, argv, {
 | 
			
		||||
        cwd: root,
 | 
			
		||||
        listeners: {
 | 
			
		||||
            stdline: (line: string) => {
 | 
			
		||||
                if (line.startsWith("Publishing build scan...")) {
 | 
			
		||||
                    publishing = true;
 | 
			
		||||
                }
 | 
			
		||||
                if (publishing && line.length == 0) {
 | 
			
		||||
                    publishing = false
 | 
			
		||||
                }
 | 
			
		||||
                if (publishing && line.startsWith("http")) {
 | 
			
		||||
                    buildScanLink = line.trim();
 | 
			
		||||
                    publishing = false
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    if (buildScanLink != null) {
 | 
			
		||||
        return new BuildResultImpl(buildScanLink.toString());
 | 
			
		||||
    }
 | 
			
		||||
    return new BuildResultImpl(null as unknown as string);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export interface BuildResult {
 | 
			
		||||
    buildScanUrl: string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class BuildResultImpl implements BuildResult {
 | 
			
		||||
    constructor(readonly buildScanUrl: string) {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										9
									
								
								src/gradlew.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/gradlew.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,9 @@
 | 
			
		||||
export function wrapperFilename() {
 | 
			
		||||
    const isWindows = process.platform === "win32";
 | 
			
		||||
    return isWindows ? "gradlew.bat" : "gradlew";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function installScriptFilename() {
 | 
			
		||||
    const isWindows = process.platform === "win32";
 | 
			
		||||
    return isWindows ? "gradle.bat" : "gradle";
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										76
									
								
								src/main.ts
									
									
									
									
									
								
							
							
						
						
									
										76
									
								
								src/main.ts
									
									
									
									
									
								
							@@ -1,12 +1,74 @@
 | 
			
		||||
import * as core from '@actions/core';
 | 
			
		||||
import * as core from "@actions/core";
 | 
			
		||||
import * as path from "path";
 | 
			
		||||
import {parseArgsStringToArgv} from "string-argv";
 | 
			
		||||
 | 
			
		||||
import * as execution from "./execution";
 | 
			
		||||
import * as gradlew from "./gradlew";
 | 
			
		||||
import * as provision from "./provision";
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// Invoked by Github Actions
 | 
			
		||||
async function run() {
 | 
			
		||||
  try {
 | 
			
		||||
    const myInput = core.getInput('myInput');
 | 
			
		||||
    core.debug(`Hello ${myInput}`);
 | 
			
		||||
  } catch (error) {
 | 
			
		||||
    core.setFailed(error.message);
 | 
			
		||||
  }
 | 
			
		||||
    try {
 | 
			
		||||
 | 
			
		||||
        const baseDirectory = process.env[`GITHUB_WORKSPACE`] || "";
 | 
			
		||||
 | 
			
		||||
        let result = await execution.execute(
 | 
			
		||||
            await resolveGradleExecutable(baseDirectory),
 | 
			
		||||
            resolveBuildRootDirectory(baseDirectory),
 | 
			
		||||
            parseCommandLineArguments()
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        if (result.buildScanUrl != null) {
 | 
			
		||||
            core.setOutput("build-scan-url", result.buildScanUrl);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    } catch (error) {
 | 
			
		||||
        core.setFailed(error.message);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
run();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async function resolveGradleExecutable(baseDirectory: string): Promise<string> {
 | 
			
		||||
 | 
			
		||||
    const gradleVersion = inputOrNull("gradle-version");
 | 
			
		||||
    if (gradleVersion != null) {
 | 
			
		||||
        return provision.gradleVersion(gradleVersion)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const gradleExecutable = inputOrNull("gradle-executable");
 | 
			
		||||
    if (gradleExecutable != null) {
 | 
			
		||||
        return path.join(baseDirectory, gradleExecutable)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const wrapperDirectory = inputOrNull("wrapper-directory");
 | 
			
		||||
    const executableDirectory = wrapperDirectory != null
 | 
			
		||||
        ? path.join(baseDirectory, wrapperDirectory)
 | 
			
		||||
        : baseDirectory;
 | 
			
		||||
 | 
			
		||||
    return path.join(executableDirectory, gradlew.wrapperFilename());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
function resolveBuildRootDirectory(baseDirectory: string): string {
 | 
			
		||||
    let buildRootDirectory = inputOrNull("build-root-directory");
 | 
			
		||||
    return buildRootDirectory == null ? baseDirectory : path.join(baseDirectory, buildRootDirectory);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
function parseCommandLineArguments(): string[] {
 | 
			
		||||
    const input = inputOrNull("arguments");
 | 
			
		||||
    return input == null ? [] : parseArgsStringToArgv(input)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
function inputOrNull(name: string): string | null {
 | 
			
		||||
    const inputString = core.getInput(name);
 | 
			
		||||
    if (inputString.length == 0) {
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
    return inputString
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										163
									
								
								src/provision.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										163
									
								
								src/provision.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,163 @@
 | 
			
		||||
import * as fs from "fs";
 | 
			
		||||
import * as path from "path";
 | 
			
		||||
import * as httpm from 'typed-rest-client/HttpClient';
 | 
			
		||||
import * as unzip from "unzipper"
 | 
			
		||||
import * as core from "@actions/core";
 | 
			
		||||
import * as io from '@actions/io';
 | 
			
		||||
import * as toolCache from "@actions/tool-cache";
 | 
			
		||||
 | 
			
		||||
import * as gradlew from "./gradlew";
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @return Gradle executable
 | 
			
		||||
 */
 | 
			
		||||
export async function gradleVersion(gradleVersion: string): Promise<string> {
 | 
			
		||||
    switch (gradleVersion) {
 | 
			
		||||
        case "current":
 | 
			
		||||
            return gradleCurrent();
 | 
			
		||||
        case "rc":
 | 
			
		||||
            return gradleReleaseCandidate();
 | 
			
		||||
        case "nightly":
 | 
			
		||||
            return gradleNightly();
 | 
			
		||||
        case "release-nightly":
 | 
			
		||||
            return gradleReleaseNightly();
 | 
			
		||||
        default:
 | 
			
		||||
            return gradle(gradleVersion);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
const gradleVersionsBaseUrl = "https://services.gradle.org/versions";
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async function gradleCurrent(): Promise<string> {
 | 
			
		||||
    const json = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/current`);
 | 
			
		||||
    return provisionGradle(json.version, json.downloadUrl);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async function gradleReleaseCandidate(): Promise<string> {
 | 
			
		||||
    const json = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/release-candidate`);
 | 
			
		||||
    if (json != null) {
 | 
			
		||||
        return provisionGradle(json.version, json.downloadUrl);
 | 
			
		||||
    }
 | 
			
		||||
    return gradleCurrent();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async function gradleNightly(): Promise<string> {
 | 
			
		||||
    const json = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/nightly`);
 | 
			
		||||
    return provisionGradle(json.version, json.downloadUrl);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async function gradleReleaseNightly(): Promise<string> {
 | 
			
		||||
    const json = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/release-nightly`);
 | 
			
		||||
    return provisionGradle(json.version, json.downloadUrl);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async function gradle(version: string): Promise<string> {
 | 
			
		||||
    const declaration = await findGradleVersionDeclaration(version);
 | 
			
		||||
    if (declaration == null) {
 | 
			
		||||
        throw new Error(`Gradle version ${version} does not exists`);
 | 
			
		||||
    }
 | 
			
		||||
    return provisionGradle(declaration.version, declaration.downloadUrl);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async function gradleVersionDeclaration(url: string): Promise<any | null> {
 | 
			
		||||
    const httpc = new httpm.HttpClient("gradle-github-action");
 | 
			
		||||
    const response = await httpc.get(url);
 | 
			
		||||
    const body = await response.readBody();
 | 
			
		||||
    const json = JSON.parse(body);
 | 
			
		||||
    return (json == null || json.version == null || json.version.length <= 0)
 | 
			
		||||
        ? null
 | 
			
		||||
        : json
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async function findGradleVersionDeclaration(version: string): Promise<any | null> {
 | 
			
		||||
    const httpc = new httpm.HttpClient("gradle-github-action");
 | 
			
		||||
    const response = await httpc.get(`${gradleVersionsBaseUrl}/all`);
 | 
			
		||||
    const body = await response.readBody();
 | 
			
		||||
    const json = JSON.parse(body);
 | 
			
		||||
    const found = json.find(entry => {
 | 
			
		||||
        return entry.version == version;
 | 
			
		||||
    });
 | 
			
		||||
    return found != undefined ? found : null
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function provisionGradle(version: string, url: string): Promise<string> {
 | 
			
		||||
 | 
			
		||||
    const cachedInstall: string = toolCache.find("gradle", version);
 | 
			
		||||
    if (cachedInstall != null && cachedInstall.length > 0) {
 | 
			
		||||
        const cachedExecutable = executableFrom(cachedInstall);
 | 
			
		||||
        core.info(`Provisioned Gradle executable ${cachedExecutable}`);
 | 
			
		||||
        return cachedExecutable;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const home = process.env["HOME"] || "";
 | 
			
		||||
    const tmpdir = path.join(home, "gradle-provision-tmpdir");
 | 
			
		||||
    const downloadsDir = path.join(tmpdir, "downloads");
 | 
			
		||||
    const installsDir = path.join(tmpdir, "installs");
 | 
			
		||||
    await io.mkdirP(downloadsDir);
 | 
			
		||||
    await io.mkdirP(installsDir);
 | 
			
		||||
 | 
			
		||||
    core.info(`Downloading ${url}`);
 | 
			
		||||
 | 
			
		||||
    const downloadPath = path.join(downloadsDir, `gradle-${version}-bin.zip`);
 | 
			
		||||
    await httpDownload(url, downloadPath);
 | 
			
		||||
    core.info(`Downloaded at ${downloadPath}, size ${fs.statSync(downloadPath).size}`);
 | 
			
		||||
 | 
			
		||||
    await extractZip(downloadPath, installsDir);
 | 
			
		||||
    const installDir = path.join(installsDir, `gradle-${version}`);
 | 
			
		||||
    core.info(`Extracted in ${installDir}`);
 | 
			
		||||
 | 
			
		||||
    const executable = executableFrom(installDir);
 | 
			
		||||
    fs.chmodSync(executable, "755");
 | 
			
		||||
    core.info(`Provisioned Gradle executable ${executable}`);
 | 
			
		||||
 | 
			
		||||
    toolCache.cacheDir(installDir, "gradle", version);
 | 
			
		||||
 | 
			
		||||
    return executable;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
function executableFrom(installDir: string): string {
 | 
			
		||||
    return path.join(installDir, "bin", `${gradlew.installScriptFilename()}`);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async function httpDownload(url: string, path: string): Promise<void> {
 | 
			
		||||
    return new Promise<void>(function (resolve, reject) {
 | 
			
		||||
        const httpc = new httpm.HttpClient("gradle-github-action");
 | 
			
		||||
        const writeStream = fs.createWriteStream(path);
 | 
			
		||||
        httpc.get(url).then(response => {
 | 
			
		||||
            response.message.pipe(writeStream)
 | 
			
		||||
                .on("close", () => {
 | 
			
		||||
                    resolve();
 | 
			
		||||
                })
 | 
			
		||||
                .on("error", err => {
 | 
			
		||||
                    reject(err)
 | 
			
		||||
                });
 | 
			
		||||
        }).catch(reason => {
 | 
			
		||||
            reject(reason);
 | 
			
		||||
        });
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async function extractZip(zip: string, destination: string): Promise<void> {
 | 
			
		||||
    return new Promise<void>(function (resolve, reject) {
 | 
			
		||||
        fs.createReadStream(zip)
 | 
			
		||||
            .pipe(unzip.Extract({"path": destination}))
 | 
			
		||||
            .on("close", () => {
 | 
			
		||||
                resolve();
 | 
			
		||||
            })
 | 
			
		||||
            .on("error", err => {
 | 
			
		||||
                reject(err)
 | 
			
		||||
            });
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user