Merge pull request #141 from cloudflare/jacobmgevans/add-more-error-handling

Additional Error Handling
This commit is contained in:
Jacob M-G Evans 2023-08-10 14:54:16 -05:00 committed by GitHub
commit bb0133dcfc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 49 deletions

View file

@ -0,0 +1,10 @@
---
"wrangler-action": patch
---
Added more error logging when a command fails to execute
Previously, we prevented any error logs from propagating too far to prevent leaking of any potentially sensitive information. However, this made it difficult for developers to debug their code.
In this release, we have updated our error handling to allow for more error messaging from pre/post and custom commands. We still discourage the use of these commands for secrets or other sensitive information, but we believe this change will make it easier for developers to debug their code.
Relates to #137

View file

@ -5,6 +5,7 @@ import {
setFailed,
endGroup,
startGroup,
error,
} from "@actions/core";
import { execSync, exec } from "node:child_process";
import { existsSync } from "node:fs";
@ -55,10 +56,28 @@ function semverCompare(version1: string, version2: string) {
async function main() {
installWrangler();
authenticationSetup();
await execCommands(getMultilineInput("preCommands"), "Pre");
await execCommands(getMultilineInput("preCommands"), "pre");
await uploadSecrets();
await wranglerCommands();
await execCommands(getMultilineInput("postCommands"), "Post");
await execCommands(getMultilineInput("postCommands"), "post");
}
async function runProcess(
command: Parameters<typeof execAsync>[0],
options: Parameters<typeof execAsync>[1],
) {
try {
const result = await execAsync(command, options);
result.stdout && info(result.stdout.toString());
result.stderr && error(result.stderr.toString());
return result;
} catch (err: any) {
err.stdout && info(err.stdout.toString());
err.stderr && error(err.stderr.toString());
throw err;
}
}
function checkWorkingDirectory(workingDirectory = ".") {
@ -99,7 +118,7 @@ async function execCommands(commands: string[], cmdType: string) {
if (!commands.length) {
return;
}
startGroup(`🚀 ${cmdType} Commands Group`);
startGroup(`🚀 Running ${cmdType}Commands`);
const arrPromises = commands.map(async (command) => {
const cmd = command.startsWith("wrangler")
@ -108,13 +127,17 @@ async function execCommands(commands: string[], cmdType: string) {
info(`🚀 Executing command: ${cmd}`);
return await execAsync(cmd, {
return await runProcess(cmd, {
cwd: config["workingDirectory"],
env: process.env,
});
});
await Promise.all(arrPromises);
await Promise.all(arrPromises).catch((result) => {
result.stdout && info(result.stdout.toString());
result.stderr && error(result.stderr.toString());
setFailed(`🚨 ${cmdType}Commands failed`);
});
endGroup();
}
@ -145,7 +168,7 @@ async function legacyUploadSecrets(
const command = `echo ${getSecret(
secret,
)} | ${getNpxCmd()} wrangler secret put ${secret}`;
return environment ? command.concat(`--env ${environment}`) : command;
return environment ? command.concat(` --env ${environment}`) : command;
})
.map(
async (command) =>
@ -215,59 +238,45 @@ function getVarArgs() {
return envVarArray.length > 0 ? `--var ${envVarArray.join(" ").trim()}` : "";
}
function defaultCommandBehavior() {
const environment = config["ENVIRONMENT"];
const wranglerVersion = config["WRANGLER_VERSION"];
const workingDirectory = config["workingDirectory"];
const deployCommand = semverCompare("2.20.0", wranglerVersion)
? "deploy"
: "publish";
info(`📌 No Wrangler commands were provided, executing default deployment.`);
if (environment.length === 0) {
execSync(
`${getNpxCmd()} wrangler ${deployCommand} ${getVarArgs()}`.trim(),
{
cwd: workingDirectory,
env: process.env,
},
);
} else {
execSync(
`${getNpxCmd()} wrangler ${deployCommand} --env ${environment} ${getVarArgs()}`.trim(),
{ cwd: workingDirectory, env: process.env },
);
}
endGroup();
}
async function wranglerCommands() {
startGroup("🚀 Running Wrangler Commands");
const commands = config["COMMANDS"];
const environment = config["ENVIRONMENT"];
if (!commands.length) {
defaultCommandBehavior();
return;
const wranglerVersion = config["WRANGLER_VERSION"];
const deployCommand = semverCompare("2.20.0", wranglerVersion)
? "deploy"
: "publish";
commands.push(deployCommand);
}
startGroup("🚀 Executing Wrangler Commands");
const arrPromises = commands.map(async (command) => {
if (environment.length > 0 && !command.includes(`--env ${environment}`)) {
command.concat(`--env ${environment}`);
if (environment.length > 0 && !command.includes(`--env`)) {
command = command.concat(` --env ${environment}`);
}
const result = await execAsync(
`${getNpxCmd()} wrangler ${command} ${getVarArgs()}`,
{
cwd: config["workingDirectory"],
env: process.env,
},
);
info(result.stdout);
return result;
const cmd = `${getNpxCmd()} wrangler ${command} ${
(command.startsWith("deploy") || command.startsWith("publish")) &&
!command.includes(`--var`)
? getVarArgs()
: ""
}`.trim();
info(`🚀 Executing command: ${cmd}`);
return await runProcess(cmd, {
cwd: config["workingDirectory"],
env: process.env,
});
});
await Promise.all(arrPromises);
await Promise.all(arrPromises).catch((result) => {
result.stdout && info(result.stdout.toString());
result.stderr && error(result.stderr.toString());
setFailed(`🚨 Command failed`);
});
endGroup();
}