diff --git a/README.md b/README.md index 6921ab9..7150ac4 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,12 @@ Example: `"my-task"` ### Optional +### `deregister-tasks` (boolean) + +Controls if previous task definitions are set to `INACTIVE` after a successful deploy. Default: `false` + +Requires the `ecs:DeregisterTaskDefinition` IAM Action + #### `env` (array) An array of environment variables to add to *every* image's task definition in the `NAME=VALUE` format diff --git a/hooks/command b/hooks/command index f50ebf2..d647786 100755 --- a/hooks/command +++ b/hooks/command @@ -156,6 +156,9 @@ if [ -n "${task_compat_reqs}" ]; then register_command+=(--requires-compatibilities "${task_compat_reqs}") fi +# Task Deregistration Control +task_deregistration=${BUILDKITE_PLUGIN_ECS_DEPLOY_DEREGISTER_TASKS:-false} + echo "--- :ecs: register command:" json_output=$("${register_command[@]}") @@ -185,7 +188,6 @@ aws ecs wait services-stable \ --cluster "${cluster}" \ --services "${service_name}" || deploy_exitcode=$? - service_events=$(aws ecs describe-services \ ${aws_default_args[@]+"${aws_default_args[@]}"} \ --cluster "${cluster}" \ @@ -195,8 +197,12 @@ service_events=$(aws ecs describe-services \ if [[ $deploy_exitcode -eq 0 ]]; then echo "--- :ecs: Service is up 🚀" echo "$service_events" + + if [[ "${task_deregistration}" == "true" ]]; then + deregister_old_task_definitions "${task_family}" "${task_revision}" + fi else echo "+++ :ecs: Service failed to deploy ❌" echo "$service_events" exit $deploy_exitcode -fi +fi \ No newline at end of file diff --git a/lib/plugin.bash b/lib/plugin.bash index 650d6c2..0c49c49 100644 --- a/lib/plugin.bash +++ b/lib/plugin.bash @@ -53,3 +53,33 @@ function prefix_read_list_into_result() { function plugin_read_list_into_result() { prefix_read_list_into_result "BUILDKITE_PLUGIN_${PLUGIN_PREFIX}_${1}" } + +# Deregisters task definitions that are not the current one +deregister_old_task_definitions() { + local task_family=$1 + local task_revision=$2 + + echo "--- :ecs: Fetching old task definitions to de-register" + echo "$task_family" + # Fetch all `ACTIVE` task definitions for the family + all_active_task_defs=$(aws ecs list-task-definitions \ + --family-prefix "${task_family}" \ + --query 'taskDefinitionArns[]' \ + --output text) + + # Array + readarray -t active_task_defs <<<"$all_active_task_defs" + + # Remove the current task definition from the list + for i in "${!active_task_defs[@]}"; do + if [[ "${active_task_defs[$i]}" == *":${task_revision}"* ]]; then + unset 'active_task_defs[$i]' + fi + done + + # De-register the old task definitions + for task_def in "${active_task_defs[@]}"; do + echo "Deregistering $task_def" + aws ecs deregister-task-definition --task-definition "$task_def" + done +} \ No newline at end of file diff --git a/plugin.yml b/plugin.yml index 8f2b583..8ce4a81 100644 --- a/plugin.yml +++ b/plugin.yml @@ -10,6 +10,8 @@ configuration: type: string container-definitions: type: string + deregister-tasks: + type: boolean env: type: array execution-role: diff --git a/tests/command.bats b/tests/command.bats index eabd117..39b3289 100644 --- a/tests/command.bats +++ b/tests/command.bats @@ -142,3 +142,25 @@ setup() { unstub aws } + +@test "Remove all but the current task definition" { + export BUILDKITE_PLUGIN_ECS_DEPLOY_DEREGISTER_TASKS=true + + stub aws \ + "ecs describe-task-definition --task-definition hello-world --query 'taskDefinition' : echo '{}'" \ + "ecs register-task-definition --family hello-world --container-definitions \* : echo '{\"taskDefinition\":{\"revision\":2}}'" \ + "ecs update-service --cluster my-cluster --service my-service --task-definition hello-world:2 : echo ok" \ + "ecs wait services-stable --cluster my-cluster --services my-service : echo ok" \ + "ecs describe-services --cluster my-cluster --services my-service --query 'services[].events' --output text : echo ok" \ + "ecs list-task-definitions --family-prefix hello-world --query 'taskDefinitionArns[]' --output text : echo arn:aws:ecs:region:account:task-definition/hello-world:1" \ + "ecs deregister-task-definition --task-definition arn:aws:ecs:region:account:task-definition/hello-world:1 : echo De-registering arn:aws:ecs:region:account:task-definition/hello-world:1" + + run "$PWD/hooks/command" + + assert_success + assert_output --partial "Service is up 🚀" + assert_output --partial "Fetching old task definitions to de-register" + assert_output --partial "Deregistering arn:aws:ecs:region:account:task-definition/hello-world:1" + + unstub aws +} \ No newline at end of file