Move error handling from startProxy to runWrapper in start-proxy action

This commit is contained in:
Michael B. Gale
2025-10-01 15:43:08 +01:00
parent a506145f31
commit 6de1d741f6
2 changed files with 150 additions and 145 deletions

View File

@@ -49322,6 +49322,9 @@ async function delay(milliseconds, opts) {
}
});
}
function wrapError(error2) {
return error2 instanceof Error ? error2 : new Error(String(error2));
}
function getErrorMessage(error2) {
return error2 instanceof Error ? error2.message : String(error2);
}
@@ -49612,79 +49615,80 @@ function generateCertificateAuthority() {
async function runWrapper() {
persistInputs();
const logger = getActionsLogger();
const tempDir = getTemporaryDirectory();
const proxyLogFilePath = path.resolve(tempDir, "proxy.log");
core8.saveState("proxy-log-file", proxyLogFilePath);
const credentials = getCredentials(
logger,
getOptionalInput("registry_secrets"),
getOptionalInput("registries_credentials"),
getOptionalInput("language")
);
if (credentials.length === 0) {
logger.info("No credentials found, skipping proxy setup.");
return;
}
logger.info(
`Credentials loaded for the following registries:
try {
const tempDir = getTemporaryDirectory();
const proxyLogFilePath = path.resolve(tempDir, "proxy.log");
core8.saveState("proxy-log-file", proxyLogFilePath);
const credentials = getCredentials(
logger,
getOptionalInput("registry_secrets"),
getOptionalInput("registries_credentials"),
getOptionalInput("language")
);
if (credentials.length === 0) {
logger.info("No credentials found, skipping proxy setup.");
return;
}
logger.info(
`Credentials loaded for the following registries:
${credentials.map((c) => credentialToStr(c)).join("\n")}`
);
const ca = generateCertificateAuthority();
const proxyConfig = {
all_credentials: credentials,
ca
};
const proxyBin = await getProxyBinaryPath(logger);
await startProxy(proxyBin, proxyConfig, proxyLogFilePath, logger);
);
const ca = generateCertificateAuthority();
const proxyConfig = {
all_credentials: credentials,
ca
};
const proxyBin = await getProxyBinaryPath(logger);
await startProxy(proxyBin, proxyConfig, proxyLogFilePath, logger);
} catch (unwrappedError) {
const error2 = wrapError(unwrappedError);
core8.setFailed(`start-proxy action failed: ${error2.message}`);
}
}
async function startProxy(binPath, config, logFilePath, logger) {
const host = "127.0.0.1";
let port = 49152;
try {
let subprocess = void 0;
let tries = 5;
let subprocessError = void 0;
while (tries-- > 0 && !subprocess && !subprocessError) {
subprocess = (0, import_child_process.spawn)(
binPath,
["-addr", `${host}:${port}`, "-config", "-", "-logfile", logFilePath],
{
detached: true,
stdio: ["pipe", "ignore", "ignore"]
}
);
subprocess.unref();
if (subprocess.pid) {
core8.saveState("proxy-process-pid", `${subprocess.pid}`);
let subprocess = void 0;
let tries = 5;
let subprocessError = void 0;
while (tries-- > 0 && !subprocess && !subprocessError) {
subprocess = (0, import_child_process.spawn)(
binPath,
["-addr", `${host}:${port}`, "-config", "-", "-logfile", logFilePath],
{
detached: true,
stdio: ["pipe", "ignore", "ignore"]
}
subprocess.on("error", (error2) => {
subprocessError = error2;
});
subprocess.on("exit", (code) => {
if (code !== 0) {
port = Math.floor(Math.random() * (65535 - 49152) + 49152);
subprocess = void 0;
}
});
subprocess.stdin?.write(JSON.stringify(config));
subprocess.stdin?.end();
await delay(1e3);
);
subprocess.unref();
if (subprocess.pid) {
core8.saveState("proxy-process-pid", `${subprocess.pid}`);
}
if (subprocessError) {
throw subprocessError;
}
logger.info(`Proxy started on ${host}:${port}`);
core8.setOutput("proxy_host", host);
core8.setOutput("proxy_port", port.toString());
core8.setOutput("proxy_ca_certificate", config.ca.cert);
const registry_urls = config.all_credentials.filter((credential) => credential.url !== void 0).map((credential) => ({
type: credential.type,
url: credential.url
}));
core8.setOutput("proxy_urls", JSON.stringify(registry_urls));
} catch (error2) {
core8.setFailed(`start-proxy action failed: ${getErrorMessage(error2)}`);
subprocess.on("error", (error2) => {
subprocessError = error2;
});
subprocess.on("exit", (code) => {
if (code !== 0) {
port = Math.floor(Math.random() * (65535 - 49152) + 49152);
subprocess = void 0;
}
});
subprocess.stdin?.write(JSON.stringify(config));
subprocess.stdin?.end();
await delay(1e3);
}
if (subprocessError) {
throw subprocessError;
}
logger.info(`Proxy started on ${host}:${port}`);
core8.setOutput("proxy_host", host);
core8.setOutput("proxy_port", port.toString());
core8.setOutput("proxy_ca_certificate", config.ca.cert);
const registry_urls = config.all_credentials.filter((credential) => credential.url !== void 0).map((credential) => ({
type: credential.type,
url: credential.url
}));
core8.setOutput("proxy_urls", JSON.stringify(registry_urls));
}
async function getProxyBinaryPath(logger) {
const proxyFileName = process.platform === "win32" ? `${UPDATEJOB_PROXY}.exe` : UPDATEJOB_PROXY;