Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions hooks/command
Original file line number Diff line number Diff line change
Expand Up @@ -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[@]}")
Expand Down Expand Up @@ -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}" \
Expand All @@ -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
30 changes: 30 additions & 0 deletions lib/plugin.bash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file is for plugin-specific functionality, I'd suggest creating the function in the main file or creating a new library file for these kind of things

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines +65 to +68
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make sure to re-use the default options to aws commands like it is done on the hook file because it may include regions without which the commands will probably fail


# Array
readarray -t active_task_defs <<<"$all_active_task_defs"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the readarray/mapfiles function can not be assumed to exist in all environments so I'd suggest an alternative


# 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
Comment on lines +73 to +78
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looping through all active definitions to remove a single element may be a bad idea... I'd suggest doing this check in the next loop while removing them


# 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
Comment on lines +81 to +84
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make sure to re-use the default options to aws commands like it is done on the hook file because it may include regions without which the commands will probably fail

}
2 changes: 2 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ configuration:
type: string
container-definitions:
type: string
deregister-tasks:
type: boolean
env:
type: array
execution-role:
Expand Down
22 changes: 22 additions & 0 deletions tests/command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
}