2021-03-30 18:57:12 +05:30
|
|
|
/***************************************************************************************************
|
|
|
|
* Copyright (c) Red Hat, Inc. All rights reserved.
|
|
|
|
* Licensed under the MIT License. See LICENSE file in the project root for license information.
|
|
|
|
**************************************************************************************************/
|
|
|
|
|
|
|
|
import * as core from "@actions/core";
|
|
|
|
import * as exec from "@actions/exec";
|
|
|
|
import * as path from "path";
|
2021-11-30 20:41:07 +05:30
|
|
|
import { promises as fs } from "fs";
|
|
|
|
import * as os from "os";
|
2021-03-30 18:57:12 +05:30
|
|
|
|
|
|
|
interface ExecResult {
|
|
|
|
exitCode: number;
|
|
|
|
stdout: string;
|
|
|
|
stderr: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function execute(
|
|
|
|
executable: string,
|
|
|
|
args: string[],
|
|
|
|
execOptions: exec.ExecOptions & { group?: boolean } = {},
|
|
|
|
): Promise<ExecResult> {
|
|
|
|
let stdout = "";
|
|
|
|
let stderr = "";
|
|
|
|
|
|
|
|
const finalExecOptions = { ...execOptions };
|
|
|
|
finalExecOptions.ignoreReturnCode = true; // the return code is processed below
|
|
|
|
|
|
|
|
finalExecOptions.listeners = {
|
|
|
|
stdline: (line): void => {
|
|
|
|
stdout += `${line}\n`;
|
|
|
|
},
|
|
|
|
errline: (line): void => {
|
|
|
|
stderr += `${line}\n`;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
if (execOptions.group) {
|
|
|
|
const groupName = [ executable, ...args ].join(" ");
|
|
|
|
core.startGroup(groupName);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const exitCode = await exec.exec(executable, args, finalExecOptions);
|
|
|
|
|
|
|
|
if (execOptions.ignoreReturnCode !== true && exitCode !== 0) {
|
|
|
|
// Throwing the stderr as part of the Error makes the stderr show up in the action outline,
|
|
|
|
// which saves some clicking when debugging.
|
|
|
|
let error = `${path.basename(executable)} exited with code ${exitCode}`;
|
|
|
|
if (stderr) {
|
|
|
|
error += `\n${stderr}`;
|
|
|
|
}
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
exitCode,
|
|
|
|
stdout,
|
|
|
|
stderr,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
finally {
|
|
|
|
if (execOptions.group) {
|
|
|
|
core.endGroup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-30 20:41:07 +05:30
|
|
|
|
|
|
|
export async function getDockerConfigJson(): Promise<string> {
|
|
|
|
const dockerConfigPath = path.join(os.homedir(), ".docker", "config.json");
|
2024-03-16 17:35:46 +01:00
|
|
|
return fs.readFile(dockerConfigPath, "utf-8")
|
2024-03-16 22:18:33 +05:30
|
|
|
.catch((err) => { if (err.code === "ENOENT") { return `{"auths":{}}`; } throw err; });
|
2021-11-30 20:41:07 +05:30
|
|
|
}
|