From cd5486be8da1a21da29ee5e25853dc47647ac1d6 Mon Sep 17 00:00:00 2001 From: Tim Shaker Date: Tue, 5 Mar 2024 15:07:10 -0800 Subject: [PATCH] add minimum_health_color input --- action.yml | 3 +++ src/getTargetEnv.ts | 30 +++++++----------------------- src/inputs.test.ts | 5 +++++ src/inputs.ts | 20 ++++++++++++++++++++ src/main.shared_alb.test.ts | 1 + src/main.test.ts | 1 + src/swapCNAMEs.ts | 4 ++-- 7 files changed, 39 insertions(+), 25 deletions(-) diff --git a/action.yml b/action.yml index 60918f6..1ad0921 100644 --- a/action.yml +++ b/action.yml @@ -30,6 +30,9 @@ inputs: green_env: description: "Name of the green environment." required: true + minimum_health_color: + description: "Minimum health color (Green, Yellow, Red, or Grey) required for the target environment to be considered healthy." + default: "Green" option_settings: description: "Path to a JSON file consisting of an array of option settings to use when updating an existing evironment or creating a new environment." platform_branch_name: diff --git a/src/getTargetEnv.ts b/src/getTargetEnv.ts index 7094635..4768b21 100644 --- a/src/getTargetEnv.ts +++ b/src/getTargetEnv.ts @@ -4,7 +4,7 @@ import { EnvironmentDescription, } from "@aws-sdk/client-elastic-beanstalk"; import { ebClient } from "./clients"; -import { ActionInputs } from "./inputs"; +import { ActionInputs, mapHealthColorToInt } from "./inputs"; import { getEnvironments } from "./getEnvironments"; import { terminateEnvironment } from "./terminateEnvironment"; import { setDescribeEventsInterval } from "./setDescribeEventsInterval"; @@ -50,27 +50,11 @@ export async function getTargetEnv( ); } - switch (targetEnv.Health) { - case "Green": - console.log("Target environment's health is Green."); - return targetEnv; - - case "Yellow": - console.log("Target environment's health is Yellow."); - await terminateEnvironment(inputs, targetEnv); - return null; - - case "Red": - console.log("Target environment's health is Red."); - await terminateEnvironment(inputs, targetEnv); - return null; - - case "Grey": - console.log("Target environment's health is Grey."); - await terminateEnvironment(inputs, targetEnv); - return null; - - default: - throw new Error("Target environment is unknown."); + console.log(`Target environment's health is ${targetEnv.Health}.`); + if (mapHealthColorToInt(targetEnv.Health) < inputs.minimumHealthColor) { + await terminateEnvironment(inputs, targetEnv); + return null; + } else { + return targetEnv; } } diff --git a/src/inputs.test.ts b/src/inputs.test.ts index 0cf3b12..9b18049 100644 --- a/src/inputs.test.ts +++ b/src/inputs.test.ts @@ -18,6 +18,7 @@ describe("checkInputs", () => { disableTerminationProtection: false, enableTerminationProtection: false, greenEnv: `my-green-env`, + minimumHealthColor: 3, optionSettings: undefined, platformBranchName: "Docker running on 64bit Amazon Linux 2023", createEnvironment: true, @@ -50,6 +51,7 @@ describe("checkInputs", () => { enableTerminationProtection: false, greenEnv: `same`, optionSettings: undefined, + minimumHealthColor: 3, platformBranchName: "Docker running on 64bit Amazon Linux 2023", createEnvironment: true, productionCNAME: `prod-cname`, @@ -81,6 +83,7 @@ describe("checkInputs", () => { enableTerminationProtection: false, greenEnv: "my-green-env", optionSettings: undefined, + minimumHealthColor: 3, platformBranchName: "Docker running on 64bit Amazon Linux 2023", createEnvironment: true, productionCNAME: `prod-cname`, @@ -113,6 +116,7 @@ describe("checkInputs", () => { enableTerminationProtection: false, greenEnv: `my-green-env`, optionSettings: undefined, + minimumHealthColor: 3, platformBranchName: "Docker running on 64bit Amazon Linux 2023", productionCNAME: `same`, sourceBundle: undefined, @@ -143,6 +147,7 @@ describe("checkInputs", () => { disableTerminationProtection: false, enableTerminationProtection: false, greenEnv: `my-green-env`, + minimumHealthColor: 3, optionSettings: "test.json", platformBranchName: "Docker running on 64bit Amazon Linux 2023", productionCNAME: `prod-cname`, diff --git a/src/inputs.ts b/src/inputs.ts index c36c87b..77d2ee0 100644 --- a/src/inputs.ts +++ b/src/inputs.ts @@ -21,6 +21,11 @@ export function getInputs() { "enable_termination_protection" ), greenEnv: core.getInput("green_env", { required: true }), + minimumHealthColor: mapHealthColorToInt( + core.getInput("minimum_health_color", { + required: true, + }) + ), optionSettings: core.getInput("option_settings") ? JSON.parse(fs.readFileSync(core.getInput("option_settings"))) : undefined, @@ -89,3 +94,18 @@ export function checkInputs(inputs: ActionInputs) { throw new Error("option_settings must be an array"); } } + +export function mapHealthColorToInt(healthColor: string) { + switch (healthColor.toUpperCase()) { + case "GREEN": + return 3; + case "YELLOW": + return 2; + case "RED": + return 1; + case "GREY": + return 0; + default: + throw new Error("Invalid health color"); + } +} diff --git a/src/main.shared_alb.test.ts b/src/main.shared_alb.test.ts index 40fd290..2e58a16 100644 --- a/src/main.shared_alb.test.ts +++ b/src/main.shared_alb.test.ts @@ -28,6 +28,7 @@ const inputs = { disableTerminationProtection: false, enableTerminationProtection: false, greenEnv: `my-green-env-${key}`, + minimumHealthColor: 3, optionSettings: undefined, platformBranchName: "Docker running on 64bit Amazon Linux 2023", productionCNAME: `shared-alb-prod-${key}`, diff --git a/src/main.test.ts b/src/main.test.ts index fa0dc3d..1339de1 100644 --- a/src/main.test.ts +++ b/src/main.test.ts @@ -18,6 +18,7 @@ const inputs = { disableTerminationProtection: false, enableTerminationProtection: false, greenEnv: `my-green-env-${key}`, + minimumHealthColor: 3, optionSettings: undefined, platformBranchName: "Docker running on 64bit Amazon Linux 2023", productionCNAME: `blue-green-test-prod-${key}`, diff --git a/src/swapCNAMEs.ts b/src/swapCNAMEs.ts index 23d8500..e42f200 100644 --- a/src/swapCNAMEs.ts +++ b/src/swapCNAMEs.ts @@ -3,7 +3,7 @@ import { waitUntilEnvironmentUpdated, } from "@aws-sdk/client-elastic-beanstalk"; import { ebClient } from "./clients"; -import { ActionInputs } from "./inputs"; +import { ActionInputs, mapHealthColorToInt } from "./inputs"; import { getEnvironments } from "./getEnvironments"; const core = require("@actions/core"); @@ -15,7 +15,7 @@ export async function swapCNAMEs(inputs: ActionInputs) { return; } - if (stagingEnv.Health !== "Green") { + if (mapHealthColorToInt(stagingEnv.Health) < inputs.minimumHealthColor) { throw new Error(`Target environment is not healthy. Cannot swap CNAMEs.`); }