mirror of
https://github.com/cloudflare/wrangler-action.git
synced 2024-11-22 01:53:24 +01:00
Merge pull request #141 from cloudflare/jacobmgevans/add-more-error-handling
Additional Error Handling
This commit is contained in:
commit
bb0133dcfc
2 changed files with 68 additions and 49 deletions
10
.changeset/itchy-buses-grow.md
Normal file
10
.changeset/itchy-buses-grow.md
Normal 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
|
107
src/index.ts
107
src/index.ts
|
@ -5,6 +5,7 @@ import {
|
||||||
setFailed,
|
setFailed,
|
||||||
endGroup,
|
endGroup,
|
||||||
startGroup,
|
startGroup,
|
||||||
|
error,
|
||||||
} from "@actions/core";
|
} from "@actions/core";
|
||||||
import { execSync, exec } from "node:child_process";
|
import { execSync, exec } from "node:child_process";
|
||||||
import { existsSync } from "node:fs";
|
import { existsSync } from "node:fs";
|
||||||
|
@ -55,10 +56,28 @@ function semverCompare(version1: string, version2: string) {
|
||||||
async function main() {
|
async function main() {
|
||||||
installWrangler();
|
installWrangler();
|
||||||
authenticationSetup();
|
authenticationSetup();
|
||||||
await execCommands(getMultilineInput("preCommands"), "Pre");
|
await execCommands(getMultilineInput("preCommands"), "pre");
|
||||||
await uploadSecrets();
|
await uploadSecrets();
|
||||||
await wranglerCommands();
|
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 = ".") {
|
function checkWorkingDirectory(workingDirectory = ".") {
|
||||||
|
@ -99,7 +118,7 @@ async function execCommands(commands: string[], cmdType: string) {
|
||||||
if (!commands.length) {
|
if (!commands.length) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
startGroup(`🚀 ${cmdType} Commands Group`);
|
startGroup(`🚀 Running ${cmdType}Commands`);
|
||||||
|
|
||||||
const arrPromises = commands.map(async (command) => {
|
const arrPromises = commands.map(async (command) => {
|
||||||
const cmd = command.startsWith("wrangler")
|
const cmd = command.startsWith("wrangler")
|
||||||
|
@ -108,13 +127,17 @@ async function execCommands(commands: string[], cmdType: string) {
|
||||||
|
|
||||||
info(`🚀 Executing command: ${cmd}`);
|
info(`🚀 Executing command: ${cmd}`);
|
||||||
|
|
||||||
return await execAsync(cmd, {
|
return await runProcess(cmd, {
|
||||||
cwd: config["workingDirectory"],
|
cwd: config["workingDirectory"],
|
||||||
env: process.env,
|
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();
|
endGroup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,7 +168,7 @@ async function legacyUploadSecrets(
|
||||||
const command = `echo ${getSecret(
|
const command = `echo ${getSecret(
|
||||||
secret,
|
secret,
|
||||||
)} | ${getNpxCmd()} wrangler secret put ${secret}`;
|
)} | ${getNpxCmd()} wrangler secret put ${secret}`;
|
||||||
return environment ? command.concat(`--env ${environment}`) : command;
|
return environment ? command.concat(` --env ${environment}`) : command;
|
||||||
})
|
})
|
||||||
.map(
|
.map(
|
||||||
async (command) =>
|
async (command) =>
|
||||||
|
@ -215,59 +238,45 @@ function getVarArgs() {
|
||||||
return envVarArray.length > 0 ? `--var ${envVarArray.join(" ").trim()}` : "";
|
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() {
|
async function wranglerCommands() {
|
||||||
|
startGroup("🚀 Running Wrangler Commands");
|
||||||
const commands = config["COMMANDS"];
|
const commands = config["COMMANDS"];
|
||||||
const environment = config["ENVIRONMENT"];
|
const environment = config["ENVIRONMENT"];
|
||||||
|
|
||||||
if (!commands.length) {
|
if (!commands.length) {
|
||||||
defaultCommandBehavior();
|
const wranglerVersion = config["WRANGLER_VERSION"];
|
||||||
return;
|
const deployCommand = semverCompare("2.20.0", wranglerVersion)
|
||||||
|
? "deploy"
|
||||||
|
: "publish";
|
||||||
|
commands.push(deployCommand);
|
||||||
}
|
}
|
||||||
startGroup("🚀 Executing Wrangler Commands");
|
|
||||||
|
|
||||||
const arrPromises = commands.map(async (command) => {
|
const arrPromises = commands.map(async (command) => {
|
||||||
if (environment.length > 0 && !command.includes(`--env ${environment}`)) {
|
if (environment.length > 0 && !command.includes(`--env`)) {
|
||||||
command.concat(`--env ${environment}`);
|
command = command.concat(` --env ${environment}`);
|
||||||
}
|
}
|
||||||
const result = await execAsync(
|
|
||||||
`${getNpxCmd()} wrangler ${command} ${getVarArgs()}`,
|
const cmd = `${getNpxCmd()} wrangler ${command} ${
|
||||||
{
|
(command.startsWith("deploy") || command.startsWith("publish")) &&
|
||||||
cwd: config["workingDirectory"],
|
!command.includes(`--var`)
|
||||||
env: process.env,
|
? getVarArgs()
|
||||||
},
|
: ""
|
||||||
);
|
}`.trim();
|
||||||
info(result.stdout);
|
|
||||||
return result;
|
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();
|
endGroup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue