Skip to content

Commit

Permalink
Merge pull request #76 from buildkite-plugins/toote_task_def_env_vars
Browse files Browse the repository at this point in the history
Support for task environment variables
  • Loading branch information
pzeballos authored Sep 17, 2022
2 parents a494eec + 19c0cc5 commit 250472c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 17 additions & 0 deletions hooks/command
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +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_vars=()
while read -r line ; do
[[ -n "$line" ]] && env_vars+=("$line")
done <<< "$(plugin_read_list ENV)"

if [[ $region != "" ]]; then
# shellcheck disable=SC2034 # Used by the aws cli
Expand Down Expand Up @@ -119,6 +123,19 @@ for image in "${images[@]}"; do
image_idx=$((image_idx+1))
done

## This adds the env vars to each container
image_idx=0
for image in "${images[@]}"; do
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 "--- :ecs: Registering new task definition for ${task_family}"
register_command="aws ecs register-task-definition \
--family ${task_family} \
Expand Down
2 changes: 2 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ configuration:
type: string
deployment-config:
type: string
env:
type: array
required:
- cluster
- service
Expand Down
40 changes: 40 additions & 0 deletions tests/command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ 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_TASK_DEFINITION=examples/multiple-images.json

expected_multiple_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]'
Expand All @@ -77,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
Expand Down

0 comments on commit 250472c

Please sign in to comment.