From da778a22ae74b208db270fb87ab168c23fb045f8 Mon Sep 17 00:00:00 2001 From: Aleks Clark Date: Thu, 27 May 2021 07:15:51 -0500 Subject: [PATCH 01/12] tweak --- hooks/command | 4 ++++ plugin.yml | 2 ++ 2 files changed, 6 insertions(+) diff --git a/hooks/command b/hooks/command index ed65a8f..119068c 100755 --- a/hooks/command +++ b/hooks/command @@ -40,6 +40,7 @@ target_container=${BUILDKITE_PLUGIN_ECS_DEPLOY_TARGET_CONTAINER_NAME:-""} target_port=${BUILDKITE_PLUGIN_ECS_DEPLOY_TARGET_CONTAINER_PORT:-""} execution_role=${BUILDKITE_PLUGIN_ECS_DEPLOY_EXECUTION_ROLE:-""} region=${BUILDKITE_PLUGIN_ECS_DEPLOY_REGION:-""} +env_file_url=${BUILDKITE_PLUGIN_ECS_DEPLOY_ENV_FILE_URL:-""} if [[ $region != "" ]]; then # shellcheck disable=SC2034 # Used by the aws cli @@ -119,6 +120,9 @@ for image in "${images[@]}"; do image_idx=$((image_idx+1)) done +echo "--- :thisisfine: " +echo "$container_definitions_json" + echo "--- :ecs: Registering new task definition for ${task_family}" register_command="aws ecs register-task-definition \ --family ${task_family} \ diff --git a/plugin.yml b/plugin.yml index 7f1125b..0f12389 100644 --- a/plugin.yml +++ b/plugin.yml @@ -32,6 +32,8 @@ configuration: type: string deployment-config: type: string + env-file-url: + type: string required: - cluster - service From fa9191e1d0e1f2f88c35c1310fd504dbd046f64d Mon Sep 17 00:00:00 2001 From: Aleks Clark Date: Fri, 28 May 2021 07:57:23 -0500 Subject: [PATCH 02/12] add env array to plugin def --- hooks/command | 21 ++++++++++++++++++++- plugin.yml | 4 ++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/hooks/command b/hooks/command index 119068c..dc075fb 100755 --- a/hooks/command +++ b/hooks/command @@ -40,7 +40,10 @@ target_container=${BUILDKITE_PLUGIN_ECS_DEPLOY_TARGET_CONTAINER_NAME:-""} target_port=${BUILDKITE_PLUGIN_ECS_DEPLOY_TARGET_CONTAINER_PORT:-""} execution_role=${BUILDKITE_PLUGIN_ECS_DEPLOY_EXECUTION_ROLE:-""} region=${BUILDKITE_PLUGIN_ECS_DEPLOY_REGION:-""} -env_file_url=${BUILDKITE_PLUGIN_ECS_DEPLOY_ENV_FILE_URL:-""} +env_vars=() +while read -r line ; do + [[ -n "$line" ]] && images+=("$line") +done <<< "$(plugin_read_list ENV)" if [[ $region != "" ]]; then # shellcheck disable=SC2034 # Used by the aws cli @@ -120,6 +123,22 @@ for image in "${images[@]}"; do image_idx=$((image_idx+1)) done +## This adds the env vars to each container +image_idx=0 +container_definitions_json=$(cat "${task_definition}") +for image in "${images[@]}"; do + container_definitions_json=$(echo "$container_definitions_json" | jq ".[${image_idx}].environment=[]" + ) + for env_var in "${env_vars[@]}"; do + # shellcheck disable=SC2206 + var_val=(${env_var//=/ }) + container_definitions_json=$(echo "$container_definitions_json" | jq --arg ENVVAR "$var_val[0]" --arg ENVVAL "$var_val[1]" \ + ".[${image_idx}].environment += {'name': \$ENVVAR, value: \$ENVVAL}" + ) + done + image_idx=$((image_idx+1)) +done + echo "--- :thisisfine: " echo "$container_definitions_json" diff --git a/plugin.yml b/plugin.yml index 0f12389..33ff1eb 100644 --- a/plugin.yml +++ b/plugin.yml @@ -32,8 +32,8 @@ configuration: type: string deployment-config: type: string - env-file-url: - type: string + env: + type: array required: - cluster - service From 77b848d666b68a440095ff742403d1d1fa8dd8ee Mon Sep 17 00:00:00 2001 From: Aleks Clark Date: Fri, 28 May 2021 08:05:09 -0500 Subject: [PATCH 03/12] hopefully fix bug --- hooks/command | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hooks/command b/hooks/command index dc075fb..3bf44a2 100755 --- a/hooks/command +++ b/hooks/command @@ -42,7 +42,7 @@ execution_role=${BUILDKITE_PLUGIN_ECS_DEPLOY_EXECUTION_ROLE:-""} region=${BUILDKITE_PLUGIN_ECS_DEPLOY_REGION:-""} env_vars=() while read -r line ; do - [[ -n "$line" ]] && images+=("$line") + [[ -n "$line" ]] && env_vars+=("$line") done <<< "$(plugin_read_list ENV)" if [[ $region != "" ]]; then From 3e2e48973fff6cd6e3b15a4a629237e192594947 Mon Sep 17 00:00:00 2001 From: Aleks Clark Date: Fri, 28 May 2021 08:06:23 -0500 Subject: [PATCH 04/12] debug --- hooks/command | 1 + 1 file changed, 1 insertion(+) diff --git a/hooks/command b/hooks/command index 3bf44a2..da1457e 100755 --- a/hooks/command +++ b/hooks/command @@ -124,6 +124,7 @@ for image in "${images[@]}"; do done ## This adds the env vars to each container +echo "--- :thisisfine: attempting env var load" image_idx=0 container_definitions_json=$(cat "${task_definition}") for image in "${images[@]}"; do From 75a024ea6b276dc2f59d1b7dc623b4f192cc66e0 Mon Sep 17 00:00:00 2001 From: Aleks Clark Date: Fri, 28 May 2021 08:09:24 -0500 Subject: [PATCH 05/12] quote --- hooks/command | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hooks/command b/hooks/command index da1457e..da4a543 100755 --- a/hooks/command +++ b/hooks/command @@ -134,7 +134,7 @@ for image in "${images[@]}"; do # shellcheck disable=SC2206 var_val=(${env_var//=/ }) container_definitions_json=$(echo "$container_definitions_json" | jq --arg ENVVAR "$var_val[0]" --arg ENVVAL "$var_val[1]" \ - ".[${image_idx}].environment += {'name': \$ENVVAR, value: \$ENVVAL}" + ".[${image_idx}].environment += {'name': \$ENVVAR, 'value': \$ENVVAL}" ) done image_idx=$((image_idx+1)) From 9a861ad69bf0caf257f02a661396b96ea0695e17 Mon Sep 17 00:00:00 2001 From: Aleks Clark Date: Fri, 28 May 2021 08:17:05 -0500 Subject: [PATCH 06/12] quote better --- hooks/command | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hooks/command b/hooks/command index da4a543..a1f744d 100755 --- a/hooks/command +++ b/hooks/command @@ -134,7 +134,7 @@ for image in "${images[@]}"; do # shellcheck disable=SC2206 var_val=(${env_var//=/ }) container_definitions_json=$(echo "$container_definitions_json" | jq --arg ENVVAR "$var_val[0]" --arg ENVVAL "$var_val[1]" \ - ".[${image_idx}].environment += {'name': \$ENVVAR, 'value': \$ENVVAL}" + '.[${image_idx}].environment += {"name": \$ENVVAR, "value": \$ENVVAL}' ) done image_idx=$((image_idx+1)) From 2aa641543f0ae3cac092d9d707ed8170bd208498 Mon Sep 17 00:00:00 2001 From: Aleks Clark Date: Fri, 28 May 2021 08:25:26 -0500 Subject: [PATCH 07/12] quote better --- hooks/command | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hooks/command b/hooks/command index a1f744d..bbf815f 100755 --- a/hooks/command +++ b/hooks/command @@ -134,7 +134,7 @@ for image in "${images[@]}"; do # shellcheck disable=SC2206 var_val=(${env_var//=/ }) container_definitions_json=$(echo "$container_definitions_json" | jq --arg ENVVAR "$var_val[0]" --arg ENVVAL "$var_val[1]" \ - '.[${image_idx}].environment += {"name": \$ENVVAR, "value": \$ENVVAL}' + ".[${image_idx}].environment += [{\"name\": \$ENVVAR, \"value\": \$ENVVAL}]" ) done image_idx=$((image_idx+1)) From 9cd14f27663c3e309838f9f89fd0faae3d757af4 Mon Sep 17 00:00:00 2001 From: Aleks Clark Date: Fri, 28 May 2021 08:41:32 -0500 Subject: [PATCH 08/12] fix a couple of bugs --- hooks/command | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/hooks/command b/hooks/command index bbf815f..674503c 100755 --- a/hooks/command +++ b/hooks/command @@ -126,14 +126,13 @@ done ## This adds the env vars to each container echo "--- :thisisfine: attempting env var load" image_idx=0 -container_definitions_json=$(cat "${task_definition}") for image in "${images[@]}"; do container_definitions_json=$(echo "$container_definitions_json" | jq ".[${image_idx}].environment=[]" ) for env_var in "${env_vars[@]}"; do # shellcheck disable=SC2206 var_val=(${env_var//=/ }) - container_definitions_json=$(echo "$container_definitions_json" | jq --arg ENVVAR "$var_val[0]" --arg ENVVAL "$var_val[1]" \ + container_definitions_json=$(echo "$container_definitions_json" | jq --arg ENVVAR "${var_val[0]}" --arg ENVVAL "${var_val[1]}" \ ".[${image_idx}].environment += [{\"name\": \$ENVVAR, \"value\": \$ENVVAL}]" ) done From ba4e939faab844e67182de855c8ca6d628c164c3 Mon Sep 17 00:00:00 2001 From: Aleks Clark Date: Fri, 28 May 2021 08:43:33 -0500 Subject: [PATCH 09/12] remove debug statements --- hooks/command | 4 ---- 1 file changed, 4 deletions(-) diff --git a/hooks/command b/hooks/command index 674503c..2f87c14 100755 --- a/hooks/command +++ b/hooks/command @@ -124,7 +124,6 @@ for image in "${images[@]}"; do done ## This adds the env vars to each container -echo "--- :thisisfine: attempting env var load" image_idx=0 for image in "${images[@]}"; do container_definitions_json=$(echo "$container_definitions_json" | jq ".[${image_idx}].environment=[]" @@ -139,9 +138,6 @@ for image in "${images[@]}"; do image_idx=$((image_idx+1)) done -echo "--- :thisisfine: " -echo "$container_definitions_json" - echo "--- :ecs: Registering new task definition for ${task_family}" register_command="aws ecs register-task-definition \ --family ${task_family} \ From c04df0414a1ac7d912a69093b011aef2a8f19cc1 Mon Sep 17 00:00:00 2001 From: Aleks Clark Date: Fri, 28 May 2021 08:51:24 -0500 Subject: [PATCH 10/12] add documentation and correct a bug --- README.md | 4 ++++ hooks/command | 2 -- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e472d0c..b042381 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,10 @@ Example: `"0/100"` The region we deploy the ECS Service to. +### `env` (optional) + +An array of environment variables to add to *every* image's task definition + ## AWS Roles At a minimum this plugin requires the following AWS permissions to be granted to the agent running this step: diff --git a/hooks/command b/hooks/command index 2f87c14..9a38d9d 100755 --- a/hooks/command +++ b/hooks/command @@ -126,8 +126,6 @@ done ## This adds the env vars to each container image_idx=0 for image in "${images[@]}"; do - container_definitions_json=$(echo "$container_definitions_json" | jq ".[${image_idx}].environment=[]" - ) for env_var in "${env_vars[@]}"; do # shellcheck disable=SC2206 var_val=(${env_var//=/ }) From 553f9234a47ec5f4a7c85e9c9fb05348e3414968 Mon Sep 17 00:00:00 2001 From: Aleks Clark Date: Fri, 18 Jun 2021 06:36:24 -0500 Subject: [PATCH 11/12] update specs --- tests/command.bats | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/command.bats b/tests/command.bats index 3b5022b..ade416b 100644 --- a/tests/command.bats +++ b/tests/command.bats @@ -52,6 +52,9 @@ expected_container_definition='[\n {\n "essential": true,\n "image": "hel export BUILDKITE_PLUGIN_ECS_DEPLOY_TASK_FAMILY=hello-world export BUILDKITE_PLUGIN_ECS_DEPLOY_IMAGE_0=hello-world:llamas export BUILDKITE_PLUGIN_ECS_DEPLOY_IMAGE_1=hello-world:alpacas + export BUILDKITE_PLUGIN_ECS_DEPLOY_ENV_0="FOO=bar" + export BUILDKITE_PLUGIN_ECS_DEPLOY_ENV_1="BAZ=bing" + export BUILDKITE_PLUGIN_ECS_DEPLOY_TASK_DEFINITION=examples/multiple-images.json expected_container_definition='[\n {\n "essential": true,\n "image": "hello-world:llamas",\n "memory": 100,\n "name": "sample",\n "portMappings": [\n {\n "containerPort": 80,\n "hostPort": 80\n }\n ]\n },\n {\n "essential": true,\n "image": "hello-world:alpacas",\n "memory": 100,\n "name": "sample",\n "portMappings": [\n {\n "containerPort": 80,\n "hostPort": 80\n }\n ]\n }\n]' From 19c0cc5d855013813629664d991fadbe16b333ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Tue, 13 Sep 2022 02:20:09 -0300 Subject: [PATCH 12/12] Updated tests to separate new functionality --- tests/command.bats | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/tests/command.bats b/tests/command.bats index ade416b..3a42abf 100644 --- a/tests/command.bats +++ b/tests/command.bats @@ -52,8 +52,6 @@ expected_container_definition='[\n {\n "essential": true,\n "image": "hel export BUILDKITE_PLUGIN_ECS_DEPLOY_TASK_FAMILY=hello-world export BUILDKITE_PLUGIN_ECS_DEPLOY_IMAGE_0=hello-world:llamas export BUILDKITE_PLUGIN_ECS_DEPLOY_IMAGE_1=hello-world:alpacas - export BUILDKITE_PLUGIN_ECS_DEPLOY_ENV_0="FOO=bar" - export BUILDKITE_PLUGIN_ECS_DEPLOY_ENV_1="BAZ=bing" export BUILDKITE_PLUGIN_ECS_DEPLOY_TASK_DEFINITION=examples/multiple-images.json @@ -80,6 +78,45 @@ expected_container_definition='[\n {\n "essential": true,\n "image": "hel unset BUILDKITE_PLUGIN_ECS_DEPLOY_IMAGE_1 } +@test "Add env vars on multiple images" { + export BUILDKITE_BUILD_NUMBER=1 + export BUILDKITE_PLUGIN_ECS_DEPLOY_CLUSTER=my-cluster + export BUILDKITE_PLUGIN_ECS_DEPLOY_SERVICE=my-service + export BUILDKITE_PLUGIN_ECS_DEPLOY_TASK_FAMILY=hello-world + export BUILDKITE_PLUGIN_ECS_DEPLOY_IMAGE_0=hello-world:llamas + export BUILDKITE_PLUGIN_ECS_DEPLOY_IMAGE_1=hello-world:alpacas + export BUILDKITE_PLUGIN_ECS_DEPLOY_ENV_0="FOO=bar" + export BUILDKITE_PLUGIN_ECS_DEPLOY_ENV_1="BAZ=bing" + + export BUILDKITE_PLUGIN_ECS_DEPLOY_TASK_DEFINITION=examples/multiple-images.json + + # first command stubbed saves the container definition to ${_TMP_DIR}/container_definition for later review and manipulation + stub aws \ + "ecs register-task-definition --family hello-world --container-definitions '*' : echo \"\$6\" > ${_TMP_DIR}/container_definition ; echo '{\"taskDefinition\":{\"revision\":1}}'" + + run "$PWD/hooks/command" + + # there is no assert_success because we are just checking that the definition was updated accordingly + assert_equal $(cat ${_TMP_DIR}/container_definition | jq -r '.[0].environment[0].name') 'FOO' + assert_equal $(cat ${_TMP_DIR}/container_definition | jq -r '.[0].environment[0].value') 'bar' + assert_equal $(cat ${_TMP_DIR}/container_definition | jq -r '.[1].environment[0].name') 'FOO' + assert_equal $(cat ${_TMP_DIR}/container_definition | jq -r '.[1].environment[0].value') 'bar' + assert_equal $(cat ${_TMP_DIR}/container_definition | jq -r '.[0].environment[1].name') 'BAZ' + assert_equal $(cat ${_TMP_DIR}/container_definition | jq -r '.[0].environment[1].value') 'bing' + assert_equal $(cat ${_TMP_DIR}/container_definition | jq -r '.[1].environment[1].name') 'BAZ' + assert_equal $(cat ${_TMP_DIR}/container_definition | jq -r '.[1].environment[1].value') 'bing' + + # as the aws command is called more times than stubbed, it is unstubbed automatically + # unstub aws + unset BUILDKITE_PLUGIN_ECS_DEPLOY_CLUSTER + unset BUILDKITE_PLUGIN_ECS_DEPLOY_SERVICE + unset BUILDKITE_PLUGIN_ECS_DEPLOY_TASK_DEFINITION + unset BUILDKITE_PLUGIN_ECS_DEPLOY_IMAGE_0 + unset BUILDKITE_PLUGIN_ECS_DEPLOY_IMAGE_1 + unset BUILDKITE_PLUGIN_ECS_DEPLOY_ENV_0 + unset BUILDKITE_PLUGIN_ECS_DEPLOY_ENV_1 +} + @test "Run a deploy when service does not exist" { export BUILDKITE_BUILD_NUMBER=1 export BUILDKITE_PLUGIN_ECS_DEPLOY_CLUSTER=my-cluster