11#! /bin/bash
22set -euo pipefail
33
4+ # Reads either a value or a list from plugin config
5+ function plugin_read_list() {
6+ prefix_read_list " BUILDKITE_PLUGIN_ECS_DEPLOY_$1 "
7+ }
8+
9+ # Reads either a value or a list from the given env prefix
10+ function prefix_read_list() {
11+ local prefix=" $1 "
12+ local parameter=" ${prefix} _0"
13+
14+ if [[ -n " ${! parameter:- } " ]]; then
15+ local i=0
16+ local parameter=" ${prefix} _${i} "
17+ while [[ -n " ${! parameter:- } " ]]; do
18+ echo " ${! parameter} "
19+ i=$(( i+ 1 ))
20+ parameter=" ${prefix} _${i} "
21+ done
22+ elif [[ -n " ${! prefix:- } " ]]; then
23+ echo " ${! prefix} "
24+ fi
25+ }
26+
427cluster=${BUILDKITE_PLUGIN_ECS_DEPLOY_CLUSTER?}
528task_family=${BUILDKITE_PLUGIN_ECS_DEPLOY_TASK_FAMILY?}
629service_name=${BUILDKITE_PLUGIN_ECS_DEPLOY_SERVICE?}
7- image=${BUILDKITE_PLUGIN_ECS_DEPLOY_IMAGE?}
30+ images=()
31+ while read -r line ; do
32+ [[ -n " $line " ]] && images+=(" $line " )
33+ done <<< " $(plugin_read_list IMAGE)"
834task_definition=${BUILDKITE_PLUGIN_ECS_DEPLOY_TASK_DEFINITION?}
935desired_count=${BUILDKITE_PLUGIN_ECS_DEPLOY_DESIRED_COUNT:- " 1" }
1036task_role_arn=${BUILDKITE_PLUGIN_ECS_DEPLOY_TASK_ROLE_ARN:- " " }
1137target_group=${BUILDKITE_PLUGIN_ECS_DEPLOY_TARGET_GROUP:- " " }
12- # Resolve any runtime environment variables it has
13- target_group=$( eval " echo $target_group " )
38+ load_balancer_name=${BUILDKITE_PLUGIN_ECS_DEPLOY_LOAD_BALANCER_NAME:- " " }
1439target_container=${BUILDKITE_PLUGIN_ECS_DEPLOY_TARGET_CONTAINER_NAME:- " " }
1540target_port=${BUILDKITE_PLUGIN_ECS_DEPLOY_TARGET_CONTAINER_PORT:- " " }
41+ execution_role=${BUILDKITE_PLUGIN_ECS_DEPLOY_EXECUTION_ROLE:- " " }
42+
43+ # Resolve any runtime environment variables it has
44+ target_group=$( eval " echo $target_group " )
45+ load_balancer_name=$( eval " echo $load_balancer_name " )
46+
47+ deployment_config=${BUILDKITE_PLUGIN_ECS_DEPLOY_DEPLOYMENT_CONFIGURATION:- " 100/200" }
48+ # shellcheck disable=SC2206
49+ min_max_percent=(${deployment_config// \/ / } )
50+ min_deploy_perc=${min_max_percent[0]}
51+ max_deploy_perc=${min_max_percent[1]}
1652
1753function create_service() {
1854 local cluster_name=$1
@@ -22,10 +58,18 @@ function create_service() {
2258 local target_group_arguments
2359 target_group_arguments=$( generate_target_group_arguments " $5 " " $6 " " $7 " )
2460
61+ # shellcheck disable=SC2016
2562 service_defined=$( aws ecs describe-services --cluster " $cluster_name " --service " $service_name " --query ' services[?status==`ACTIVE`].status' --output text | wc -l)
2663 if [[ $service_defined -eq 0 ]]; then
2764 echo " --- :ecs: Creating a Service $service_name in cluster $cluster_name "
28- aws ecs create-service --cluster " $cluster_name " --service-name " $service_name " --task-definition " $task_definition " --desired-count " $desired_count " $target_group_arguments
65+ # shellcheck disable=SC2086
66+ aws ecs create-service \
67+ --cluster " $cluster_name " \
68+ --service-name " $service_name " \
69+ --task-definition " $task_definition " \
70+ --desired-count " $desired_count " \
71+ --deployment-configuration " maximumPercent=${max_deploy_perc} ,minimumHealthyPercent=${min_deploy_perc} " \
72+ $target_group_arguments
2973 fi
3074}
3175
@@ -34,21 +78,33 @@ function generate_target_group_arguments() {
3478 local target_container=$2
3579 local target_port=$3
3680 local target_group_arguments=" "
37- if [[ -n $target_group ]] && [[ -n $target_container ]] && [[ -n $target_port ]]; then
38- target_group_arguments=" --load-balancers targetGroupArn=${target_group} ,containerName=${target_container} ,containerPort=${target_port} "
81+ if [[ -n $target_container ]] && [[ -n $target_port ]]; then
82+ local load_balancer_ref=" "
83+ if [[ -n $target_group ]]; then
84+ load_balancer_ref=" targetGroupArn=${target_group} "
85+ elif [[ -n $load_balancer_name ]]; then
86+ load_balancer_ref=" loadBalancerName=${load_balancer_name} "
87+ else
88+ echo " +++ ^^^"
89+ # shellcheck disable=SC2016
90+ echo ' +++ You must specify either `target-group` or `load-balancer-name`'
91+ exit 1
92+ fi
93+
94+ target_group_arguments=" --load-balancers ${load_balancer_ref} ,containerName=${target_container} ,containerPort=${target_port} "
3995 fi
4096 echo " $target_group_arguments "
4197}
4298
4399# # This is the template definition of your containers
44- container_definitions_json= $( jq --arg IMAGE " $image " \
45- ' .[0].image=$IMAGE ' \
46- " $task_definition "
47- )
48-
49- echo " jq --arg IMAGE $image \
50- '.taskDefinition.containerDefinitions[0].image= \$ IMAGE' \
51- $task_definition " > /tmp/foo
100+ image_idx=0
101+ container_definitions_json= $( cat " ${task_definition} " )
102+ for image in " ${images[@]} " ; do
103+ container_definitions_json= $( echo " $container_definitions_json " | jq --arg IMAGE " $image " \
104+ " .[ ${image_idx} ].image= \$ IMAGE "
105+ )
106+ image_idx= $(( image_idx + 1 ))
107+ done
52108
53109echo " --- :ecs: Registering new task definition for ${task_family} "
54110register_command=" aws ecs register-task-definition \
@@ -58,6 +114,11 @@ register_command="aws ecs register-task-definition \
58114if [[ -n " ${task_role_arn} " ]]; then
59115 register_command+=" --task-role-arn ${task_role_arn} "
60116fi
117+
118+ if [[ -n " ${execution_role} " ]]; then
119+ register_command+=" --execution-role-arn ${execution_role} "
120+ fi
121+
61122json_output=$( eval " $register_command " )
62123register_exit_code=$?
63124
@@ -73,13 +134,17 @@ echo "Registered ${task_family}:${task_revision}"
73134# Create service if it doesn't already exist
74135create_service " $cluster " " ${task_family} :${task_revision} " " $service_name " " $desired_count " " $target_group " " $target_container " " $target_port "
75136
137+ # shellcheck disable=SC2016
76138lb_config=$( aws ecs describe-services --cluster " $cluster " --services " $service_name " --query ' services[?status==`ACTIVE`]' | jq -r ' .[0].loadBalancers[0]' )
77- 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"
139+ error=" +++ ^^^
140+ +++ 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"
78141
79142# No easy way to tell if the target group has changed, since describe-services only returns the load balancer name
80- if [[ " $lb_config " == " null" ]] && [[ -n $target_group ]]; then
81- echo " $error "
82- exit 1
143+ if [[ " $lb_config " == " null" ]]; then
144+ if [[ -n " $target_group " ]] || [[ -n " $load_balancer_name " ]]; then
145+ echo " $error "
146+ exit 1
147+ fi
83148fi
84149
85150if [[ " $lb_config " == " null" ]]; then
@@ -88,6 +153,12 @@ if [[ "$lb_config" == "null" ]]; then
88153elif [[ $( echo " $lb_config " | jq -r ' .containerName' ) != " $target_container " ]] || [[ $( echo " $lb_config " | jq -r ' .containerPort' ) -ne $target_port ]]; then
89154 echo " $error "
90155 exit 1
156+ elif [[ -n " $target_group " ]] && [[ $( echo " $lb_config " | jq -r ' .targetGroupArn' ) != " $target_group " ]]; then
157+ echo " $error "
158+ exit 1
159+ elif [[ -n " $load_balancer_name " ]] && [[ $( echo " $lb_config " | jq -r ' .loadBalancerName' ) != " $load_balancer_name " ]]; then
160+ echo " $error "
161+ exit 1
91162fi
92163
93164echo " --- :ecs: Updating service for ${service_name} "
0 commit comments