Skip to content

Commit

Permalink
Merge pull request #6 from tmshkr/dev
Browse files Browse the repository at this point in the history
add minimum_health_color input
  • Loading branch information
tmshkr authored Mar 5, 2024
2 parents 4bfe938 + cd5486b commit 7757e7f
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 25 deletions.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 7 additions & 23 deletions src/getTargetEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
}
}
5 changes: 5 additions & 0 deletions src/inputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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`,
Expand Down Expand Up @@ -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`,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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`,
Expand Down
20 changes: 20 additions & 0 deletions src/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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");
}
}
1 change: 1 addition & 0 deletions src/main.shared_alb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down
1 change: 1 addition & 0 deletions src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down
4 changes: 2 additions & 2 deletions src/swapCNAMEs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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.`);
}

Expand Down

0 comments on commit 7757e7f

Please sign in to comment.