Skip to content

Commit a6de594

Browse files
author
Patrick Robinson
authored
Merge pull request #32 from buildkite-plugins/support-elbs
Support Classic Load Balancers
2 parents ecab00c + c2b5b54 commit a6de594

File tree

3 files changed

+82
-13
lines changed

3 files changed

+82
-13
lines changed

hooks/command

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ task_definition=${BUILDKITE_PLUGIN_ECS_DEPLOY_TASK_DEFINITION?}
3535
desired_count=${BUILDKITE_PLUGIN_ECS_DEPLOY_DESIRED_COUNT:-"1"}
3636
task_role_arn=${BUILDKITE_PLUGIN_ECS_DEPLOY_TASK_ROLE_ARN:-""}
3737
target_group=${BUILDKITE_PLUGIN_ECS_DEPLOY_TARGET_GROUP:-""}
38-
# Resolve any runtime environment variables it has
39-
target_group=$(eval "echo $target_group")
38+
load_balancer_name=${BUILDKITE_PLUGIN_ECS_DEPLOY_LOAD_BALANCER_NAME:-""}
4039
target_container=${BUILDKITE_PLUGIN_ECS_DEPLOY_TARGET_CONTAINER_NAME:-""}
4140
target_port=${BUILDKITE_PLUGIN_ECS_DEPLOY_TARGET_CONTAINER_PORT:-""}
4241

42+
# Resolve any runtime environment variables it has
43+
target_group=$(eval "echo $target_group")
44+
load_balancer_name=$(eval "echo $load_balancer_name")
45+
4346
function create_service() {
4447
local cluster_name=$1
4548
local task_definition=$2
@@ -62,8 +65,20 @@ function generate_target_group_arguments() {
6265
local target_container=$2
6366
local target_port=$3
6467
local target_group_arguments=""
65-
if [[ -n $target_group ]] && [[ -n $target_container ]] && [[ -n $target_port ]]; then
66-
target_group_arguments="--load-balancers targetGroupArn=${target_group},containerName=${target_container},containerPort=${target_port}"
68+
if [[ -n $target_container ]] && [[ -n $target_port ]]; then
69+
local load_balancer_ref=""
70+
if [[ -n $target_group ]]; then
71+
load_balancer_ref="targetGroupArn=${target_group}"
72+
elif [[ -n $load_balancer_name ]]; then
73+
load_balancer_ref="loadBalancerName=${load_balancer_name}"
74+
else
75+
echo "+++ ^^^"
76+
# shellcheck disable=SC2016
77+
echo '+++ You must specify either `target-group` or `load-balancer-name`'
78+
exit 1
79+
fi
80+
81+
target_group_arguments="--load-balancers ${load_balancer_ref},containerName=${target_container},containerPort=${target_port}"
6782
fi
6883
echo "$target_group_arguments"
6984
}
@@ -78,10 +93,6 @@ for image in "${images[@]}"; do
7893
image_idx=$((image_idx+1))
7994
done
8095

81-
echo "jq --arg IMAGE $image \
82-
'.taskDefinition.containerDefinitions[0].image=\$IMAGE' \
83-
$task_definition" > /tmp/foo
84-
8596
echo "--- :ecs: Registering new task definition for ${task_family}"
8697
register_command="aws ecs register-task-definition \
8798
--family ${task_family} \
@@ -107,12 +118,15 @@ create_service "$cluster" "${task_family}:${task_revision}" "$service_name" "$de
107118

108119
# shellcheck disable=SC2016
109120
lb_config=$(aws ecs describe-services --cluster "$cluster" --services "$service_name" --query 'services[?status==`ACTIVE`]' |jq -r '.[0].loadBalancers[0]')
110-
error="+++ Cannot update a service to add a load balancer. First delete the service and then run again, or rename the service to force a new one to be created"
121+
error="+++ ^^^
122+
+++ Cannot update a service to add/remove a load balancer. First delete the service and then run again, or rename the service to force a new one to be created"
111123

112124
# No easy way to tell if the target group has changed, since describe-services only returns the load balancer name
113-
if [[ "$lb_config" == "null" ]] && [[ -n $target_group ]]; then
114-
echo "$error"
115-
exit 1
125+
if [[ "$lb_config" == "null" ]]; then
126+
if [[ -n "$target_group" ]] || [[ -n "$load_balancer_name" ]]; then
127+
echo "$error"
128+
exit 1
129+
fi
116130
fi
117131

118132
if [[ "$lb_config" == "null" ]]; then
@@ -121,6 +135,12 @@ if [[ "$lb_config" == "null" ]]; then
121135
elif [[ $(echo "$lb_config" |jq -r '.containerName') != "$target_container" ]] || [[ $(echo "$lb_config" |jq -r '.containerPort') -ne $target_port ]]; then
122136
echo "$error"
123137
exit 1
138+
elif [[ -n "$target_group" ]] && [[ $(echo "$lb_config" |jq -r '.targetGroupArn') != "$target_group" ]]; then
139+
echo "$error"
140+
exit 1
141+
elif [[ -n "$load_balancer_name" ]] && [[ $(echo "$lb_config" |jq -r '.loadBalancerName') != "$load_balancer_name" ]]; then
142+
echo "$error"
143+
exit 1
124144
fi
125145

126146
echo "--- :ecs: Updating service for ${service_name}"

plugin.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ configuration:
2222
type: string
2323
target-group:
2424
type: string
25+
load-balanccer-name:
26+
type: string
2527
target-container-name:
2628
type: string
2729
target-container-port:

tests/command.bats

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ load '/usr/local/lib/bats/load.bash'
165165
"'.taskDefinition.revision' : echo 1" \
166166
"-r '.[0].loadBalancers[0]' : echo alb" \
167167
"-r .containerName : echo nginx" \
168-
"-r .containerPort : echo 80"
168+
"-r .containerPort : echo 80" \
169+
"-r .targetGroupArn : echo 'arn:aws:elasticloadbalancing:us-east-1:012345678910:targetgroup/alb/e987e1234cd12abc'"
169170

170171
stub aws \
171172
"ecs register-task-definition --family hello-world --container-definitions '{\"json\":true}' : echo '{\"taskDefinition\":{\"revision\":1}}'" \
@@ -186,4 +187,50 @@ load '/usr/local/lib/bats/load.bash'
186187
unset BUILDKITE_PLUGIN_ECS_DEPLOY_CLUSTER
187188
unset BUILDKITE_PLUGIN_ECS_DEPLOY_SERVICE
188189
unset BUILDKITE_PLUGIN_ECS_DEPLOY_TASK_DEFINITION
190+
unset BUILDKITE_PLUGIN_ECS_DEPLOY_TARGET_GROUP
191+
unset BUILDKITE_PLUGIN_ECS_DEPLOY_TARGET_CONTAINER_NAME
192+
unset BUILDKITE_PLUGIN_ECS_DEPLOY_TARGET_CONTAINER_PORT
189193
}
194+
195+
@test "Run a deploy with ELBv1" {
196+
export BUILDKITE_BUILD_NUMBER=1
197+
export BUILDKITE_PLUGIN_ECS_DEPLOY_CLUSTER=my-cluster
198+
export BUILDKITE_PLUGIN_ECS_DEPLOY_SERVICE=my-service
199+
export BUILDKITE_PLUGIN_ECS_DEPLOY_TASK_FAMILY=hello-world
200+
export BUILDKITE_PLUGIN_ECS_DEPLOY_IMAGE=hello-world:llamas
201+
export BUILDKITE_PLUGIN_ECS_DEPLOY_TASK_DEFINITION=examples/hello-world.json
202+
export BUILDKITE_PLUGIN_ECS_DEPLOY_LOAD_BALANCER_NAME=nginx-elb
203+
export BUILDKITE_PLUGIN_ECS_DEPLOY_TARGET_CONTAINER_NAME=nginx
204+
export BUILDKITE_PLUGIN_ECS_DEPLOY_TARGET_CONTAINER_PORT=80
205+
206+
stub jq \
207+
"--arg IMAGE hello-world:llamas '.[0].image=\$IMAGE' : echo '{\"json\":true}'" \
208+
"'.taskDefinition.revision' : echo 1" \
209+
"-r '.[0].loadBalancers[0]' : echo alb" \
210+
"-r .containerName : echo nginx" \
211+
"-r .containerPort : echo 80" \
212+
"-r .loadBalancerName : echo nginx-elb"
213+
214+
stub aws \
215+
"ecs register-task-definition --family hello-world --container-definitions '{\"json\":true}' : echo '{\"taskDefinition\":{\"revision\":1}}'" \
216+
"ecs describe-services --cluster my-cluster --service my-service --query 'services[?status==\`ACTIVE\`].status' --output text : echo -n ''" \
217+
"ecs create-service --cluster my-cluster --service-name my-service --task-definition hello-world:1 --desired-count 1 --load-balancers loadBalancerName=nginx-elb,containerName=nginx,containerPort=80 : echo -n ''" \
218+
"ecs describe-services --cluster my-cluster --services my-service --query 'services[?status==\`ACTIVE\`]' : echo '[{\"loadBalancerName\": \"alb\",\"containerName\": \"nginx\",\"containerPort\": 80}]'" \
219+
"ecs update-service --cluster my-cluster --service my-service --task-definition hello-world:1 : echo ok" \
220+
"ecs wait services-stable --cluster my-cluster --services my-service : echo ok" \
221+
"ecs describe-services --cluster my-cluster --service my-service : echo ok"
222+
223+
run "$PWD/hooks/command"
224+
225+
assert_success
226+
assert_output --partial "Service is up 🚀"
227+
228+
unstub aws
229+
unstub jq
230+
unset BUILDKITE_PLUGIN_ECS_DEPLOY_CLUSTER
231+
unset BUILDKITE_PLUGIN_ECS_DEPLOY_SERVICE
232+
unset BUILDKITE_PLUGIN_ECS_DEPLOY_TASK_DEFINITION
233+
unset BUILDKITE_PLUGIN_ECS_DEPLOY_TARGET_CONTAINER_NAME
234+
unset BUILDKITE_PLUGIN_ECS_DEPLOY_TARGET_CONTAINER_PORT
235+
unset BUILDKITE_PLUGIN_ECS_DEPLOY_LOAD_BALANCER_NAME
236+
}

0 commit comments

Comments
 (0)