mirror of
https://github.com/cloudflare/wrangler-action.git
synced 2024-11-24 02:44:45 +01:00
Refactor error handling to stop execution on errors. Fixes #160.
This commit is contained in:
parent
b076e889fb
commit
e5251df521
2 changed files with 88 additions and 85 deletions
5
.changeset/stale-spiders-turn.md
Normal file
5
.changeset/stale-spiders-turn.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"wrangler-action": patch
|
||||
---
|
||||
|
||||
Refactored error handling to stop execution when action fails. Previously, the action would continue executing to completion if one of the steps encountered an error. Fixes #160.
|
54
src/index.ts
54
src/index.ts
|
@ -80,6 +80,7 @@ function semverCompare(version1: string, version2: string) {
|
|||
}
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
installWrangler();
|
||||
authenticationSetup();
|
||||
await execCommands(getMultilineInput("preCommands"), "pre");
|
||||
|
@ -87,6 +88,10 @@ async function main() {
|
|||
await wranglerCommands();
|
||||
await execCommands(getMultilineInput("postCommands"), "post");
|
||||
info("🏁 Wrangler Action completed", true);
|
||||
} catch (err) {
|
||||
error(`${err}`);
|
||||
setFailed("🚨 Action failed");
|
||||
}
|
||||
}
|
||||
|
||||
async function runProcess(
|
||||
|
@ -103,7 +108,7 @@ async function runProcess(
|
|||
} catch (err: any) {
|
||||
err.stdout && info(err.stdout.toString());
|
||||
err.stderr && error(err.stderr.toString(), true);
|
||||
throw err;
|
||||
throw new Error(`\`${command}\` returned non-zero exit code.`);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -113,19 +118,19 @@ function checkWorkingDirectory(workingDirectory = ".") {
|
|||
if (existsSync(normalizedPath)) {
|
||||
return normalizedPath;
|
||||
} else {
|
||||
setFailed(`🚨 Directory ${workingDirectory} does not exist.`);
|
||||
throw new Error(`Directory ${workingDirectory} does not exist.`);
|
||||
}
|
||||
} catch (error) {
|
||||
setFailed(
|
||||
`🚨 While checking/creating directory ${workingDirectory} received ${error}`,
|
||||
throw new Error(
|
||||
`While checking/creating directory ${workingDirectory} received ${error}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function installWrangler() {
|
||||
if (config["WRANGLER_VERSION"].startsWith("1")) {
|
||||
setFailed(
|
||||
`🚨 Wrangler v1 is no longer supported by this action. Please use major version 2 or greater`,
|
||||
throw new Error(
|
||||
`Wrangler v1 is no longer supported by this action. Please use major version 2 or greater`,
|
||||
);
|
||||
}
|
||||
startGroup("📥 Installing Wrangler");
|
||||
|
@ -145,8 +150,9 @@ async function execCommands(commands: string[], cmdType: string) {
|
|||
if (!commands.length) {
|
||||
return;
|
||||
}
|
||||
startGroup(`🚀 Running ${cmdType}Commands`);
|
||||
|
||||
startGroup(`🚀 Running ${cmdType}Commands`);
|
||||
try {
|
||||
const arrPromises = commands.map(async (command) => {
|
||||
const cmd = command.startsWith("wrangler")
|
||||
? `${getNpxCmd()} ${command}`
|
||||
|
@ -160,25 +166,23 @@ async function execCommands(commands: string[], cmdType: string) {
|
|||
});
|
||||
});
|
||||
|
||||
await Promise.all(arrPromises).catch((result) => {
|
||||
result.stdout && info(result.stdout.toString());
|
||||
result.stderr && error(result.stderr.toString(), true);
|
||||
setFailed(`🚨 ${cmdType}Commands failed`);
|
||||
});
|
||||
await Promise.all(arrPromises);
|
||||
} finally {
|
||||
endGroup();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A helper function to get the secret from the environment variables.
|
||||
*/
|
||||
function getSecret(secret: string) {
|
||||
if (!secret) {
|
||||
setFailed("No secret provided");
|
||||
throw new Error("Secret name cannot be blank.");
|
||||
}
|
||||
|
||||
const value = process.env[secret];
|
||||
if (!value) {
|
||||
setFailed(`Secret ${secret} not found`);
|
||||
throw new Error(`Value for secret ${secret} not found.`);
|
||||
}
|
||||
|
||||
return value;
|
||||
|
@ -189,7 +193,6 @@ async function legacyUploadSecrets(
|
|||
environment?: string,
|
||||
workingDirectory?: string,
|
||||
) {
|
||||
try {
|
||||
const arrPromises = secrets
|
||||
.map((secret) => {
|
||||
const command = `echo ${getSecret(
|
||||
|
@ -206,9 +209,6 @@ async function legacyUploadSecrets(
|
|||
);
|
||||
|
||||
await Promise.all(arrPromises);
|
||||
} catch {
|
||||
setFailed(`🚨 Error uploading secrets`);
|
||||
}
|
||||
}
|
||||
|
||||
async function uploadSecrets() {
|
||||
|
@ -245,8 +245,8 @@ async function uploadSecrets() {
|
|||
stdio: "ignore",
|
||||
});
|
||||
info(`✅ Uploaded secrets`);
|
||||
} catch {
|
||||
setFailed(`🚨 Error uploading secrets`);
|
||||
} catch (err) {
|
||||
throw new Error(`Error uploading secrets: ${err}`);
|
||||
} finally {
|
||||
endGroup();
|
||||
}
|
||||
|
@ -258,7 +258,7 @@ function getVarArgs() {
|
|||
if (process.env[envVar] && process.env[envVar]?.length !== 0) {
|
||||
return `${envVar}:${process.env[envVar]!}`;
|
||||
} else {
|
||||
setFailed(`🚨 ${envVar} not found in variables.`);
|
||||
throw new Error(`Value for var ${envVar} not found in environment.`);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -267,6 +267,7 @@ function getVarArgs() {
|
|||
|
||||
async function wranglerCommands() {
|
||||
startGroup("🚀 Running Wrangler Commands");
|
||||
try {
|
||||
const commands = config["COMMANDS"];
|
||||
const environment = config["ENVIRONMENT"];
|
||||
|
||||
|
@ -298,16 +299,13 @@ async function wranglerCommands() {
|
|||
});
|
||||
});
|
||||
|
||||
await Promise.all(arrPromises).catch((result) => {
|
||||
result.stdout && info(result.stdout.toString());
|
||||
result.stderr && error(result.stderr.toString());
|
||||
setFailed(`🚨 Command failed`);
|
||||
});
|
||||
|
||||
await Promise.all(arrPromises);
|
||||
} finally {
|
||||
endGroup();
|
||||
}
|
||||
}
|
||||
|
||||
main().catch(() => setFailed("🚨 Action failed"));
|
||||
main();
|
||||
|
||||
export {
|
||||
wranglerCommands,
|
||||
|
|
Loading…
Reference in a new issue