Skip to content

Commit 7757e7f

Browse files
authored
Merge pull request #6 from tmshkr/dev
add minimum_health_color input
2 parents 4bfe938 + cd5486b commit 7757e7f

File tree

7 files changed

+39
-25
lines changed

7 files changed

+39
-25
lines changed

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ inputs:
3030
green_env:
3131
description: "Name of the green environment."
3232
required: true
33+
minimum_health_color:
34+
description: "Minimum health color (Green, Yellow, Red, or Grey) required for the target environment to be considered healthy."
35+
default: "Green"
3336
option_settings:
3437
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."
3538
platform_branch_name:

src/getTargetEnv.ts

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
EnvironmentDescription,
55
} from "@aws-sdk/client-elastic-beanstalk";
66
import { ebClient } from "./clients";
7-
import { ActionInputs } from "./inputs";
7+
import { ActionInputs, mapHealthColorToInt } from "./inputs";
88
import { getEnvironments } from "./getEnvironments";
99
import { terminateEnvironment } from "./terminateEnvironment";
1010
import { setDescribeEventsInterval } from "./setDescribeEventsInterval";
@@ -50,27 +50,11 @@ export async function getTargetEnv(
5050
);
5151
}
5252

53-
switch (targetEnv.Health) {
54-
case "Green":
55-
console.log("Target environment's health is Green.");
56-
return targetEnv;
57-
58-
case "Yellow":
59-
console.log("Target environment's health is Yellow.");
60-
await terminateEnvironment(inputs, targetEnv);
61-
return null;
62-
63-
case "Red":
64-
console.log("Target environment's health is Red.");
65-
await terminateEnvironment(inputs, targetEnv);
66-
return null;
67-
68-
case "Grey":
69-
console.log("Target environment's health is Grey.");
70-
await terminateEnvironment(inputs, targetEnv);
71-
return null;
72-
73-
default:
74-
throw new Error("Target environment is unknown.");
53+
console.log(`Target environment's health is ${targetEnv.Health}.`);
54+
if (mapHealthColorToInt(targetEnv.Health) < inputs.minimumHealthColor) {
55+
await terminateEnvironment(inputs, targetEnv);
56+
return null;
57+
} else {
58+
return targetEnv;
7559
}
7660
}

src/inputs.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ describe("checkInputs", () => {
1818
disableTerminationProtection: false,
1919
enableTerminationProtection: false,
2020
greenEnv: `my-green-env`,
21+
minimumHealthColor: 3,
2122
optionSettings: undefined,
2223
platformBranchName: "Docker running on 64bit Amazon Linux 2023",
2324
createEnvironment: true,
@@ -50,6 +51,7 @@ describe("checkInputs", () => {
5051
enableTerminationProtection: false,
5152
greenEnv: `same`,
5253
optionSettings: undefined,
54+
minimumHealthColor: 3,
5355
platformBranchName: "Docker running on 64bit Amazon Linux 2023",
5456
createEnvironment: true,
5557
productionCNAME: `prod-cname`,
@@ -81,6 +83,7 @@ describe("checkInputs", () => {
8183
enableTerminationProtection: false,
8284
greenEnv: "my-green-env",
8385
optionSettings: undefined,
86+
minimumHealthColor: 3,
8487
platformBranchName: "Docker running on 64bit Amazon Linux 2023",
8588
createEnvironment: true,
8689
productionCNAME: `prod-cname`,
@@ -113,6 +116,7 @@ describe("checkInputs", () => {
113116
enableTerminationProtection: false,
114117
greenEnv: `my-green-env`,
115118
optionSettings: undefined,
119+
minimumHealthColor: 3,
116120
platformBranchName: "Docker running on 64bit Amazon Linux 2023",
117121
productionCNAME: `same`,
118122
sourceBundle: undefined,
@@ -143,6 +147,7 @@ describe("checkInputs", () => {
143147
disableTerminationProtection: false,
144148
enableTerminationProtection: false,
145149
greenEnv: `my-green-env`,
150+
minimumHealthColor: 3,
146151
optionSettings: "test.json",
147152
platformBranchName: "Docker running on 64bit Amazon Linux 2023",
148153
productionCNAME: `prod-cname`,

src/inputs.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ export function getInputs() {
2121
"enable_termination_protection"
2222
),
2323
greenEnv: core.getInput("green_env", { required: true }),
24+
minimumHealthColor: mapHealthColorToInt(
25+
core.getInput("minimum_health_color", {
26+
required: true,
27+
})
28+
),
2429
optionSettings: core.getInput("option_settings")
2530
? JSON.parse(fs.readFileSync(core.getInput("option_settings")))
2631
: undefined,
@@ -89,3 +94,18 @@ export function checkInputs(inputs: ActionInputs) {
8994
throw new Error("option_settings must be an array");
9095
}
9196
}
97+
98+
export function mapHealthColorToInt(healthColor: string) {
99+
switch (healthColor.toUpperCase()) {
100+
case "GREEN":
101+
return 3;
102+
case "YELLOW":
103+
return 2;
104+
case "RED":
105+
return 1;
106+
case "GREY":
107+
return 0;
108+
default:
109+
throw new Error("Invalid health color");
110+
}
111+
}

src/main.shared_alb.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const inputs = {
2828
disableTerminationProtection: false,
2929
enableTerminationProtection: false,
3030
greenEnv: `my-green-env-${key}`,
31+
minimumHealthColor: 3,
3132
optionSettings: undefined,
3233
platformBranchName: "Docker running on 64bit Amazon Linux 2023",
3334
productionCNAME: `shared-alb-prod-${key}`,

src/main.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const inputs = {
1818
disableTerminationProtection: false,
1919
enableTerminationProtection: false,
2020
greenEnv: `my-green-env-${key}`,
21+
minimumHealthColor: 3,
2122
optionSettings: undefined,
2223
platformBranchName: "Docker running on 64bit Amazon Linux 2023",
2324
productionCNAME: `blue-green-test-prod-${key}`,

src/swapCNAMEs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
waitUntilEnvironmentUpdated,
44
} from "@aws-sdk/client-elastic-beanstalk";
55
import { ebClient } from "./clients";
6-
import { ActionInputs } from "./inputs";
6+
import { ActionInputs, mapHealthColorToInt } from "./inputs";
77
import { getEnvironments } from "./getEnvironments";
88
const core = require("@actions/core");
99

@@ -15,7 +15,7 @@ export async function swapCNAMEs(inputs: ActionInputs) {
1515
return;
1616
}
1717

18-
if (stagingEnv.Health !== "Green") {
18+
if (mapHealthColorToInt(stagingEnv.Health) < inputs.minimumHealthColor) {
1919
throw new Error(`Target environment is not healthy. Cannot swap CNAMEs.`);
2020
}
2121

0 commit comments

Comments
 (0)