Skip to content

Commit 250472c

Browse files
authored
Merge pull request #76 from buildkite-plugins/toote_task_def_env_vars
Support for task environment variables
2 parents a494eec + 19c0cc5 commit 250472c

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ Example: `"0/100"`
9999

100100
The region we deploy the ECS Service to.
101101

102+
### `env` (optional)
103+
104+
An array of environment variables to add to *every* image's task definition
105+
102106
## AWS Roles
103107

104108
At a minimum this plugin requires the following AWS permissions to be granted to the agent running this step:

hooks/command

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ target_container=${BUILDKITE_PLUGIN_ECS_DEPLOY_TARGET_CONTAINER_NAME:-""}
4040
target_port=${BUILDKITE_PLUGIN_ECS_DEPLOY_TARGET_CONTAINER_PORT:-""}
4141
execution_role=${BUILDKITE_PLUGIN_ECS_DEPLOY_EXECUTION_ROLE:-""}
4242
region=${BUILDKITE_PLUGIN_ECS_DEPLOY_REGION:-""}
43+
env_vars=()
44+
while read -r line ; do
45+
[[ -n "$line" ]] && env_vars+=("$line")
46+
done <<< "$(plugin_read_list ENV)"
4347

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

126+
## This adds the env vars to each container
127+
image_idx=0
128+
for image in "${images[@]}"; do
129+
for env_var in "${env_vars[@]}"; do
130+
# shellcheck disable=SC2206
131+
var_val=(${env_var//=/ })
132+
container_definitions_json=$(echo "$container_definitions_json" | jq --arg ENVVAR "${var_val[0]}" --arg ENVVAL "${var_val[1]}" \
133+
".[${image_idx}].environment += [{\"name\": \$ENVVAR, \"value\": \$ENVVAL}]"
134+
)
135+
done
136+
image_idx=$((image_idx+1))
137+
done
138+
122139
echo "--- :ecs: Registering new task definition for ${task_family}"
123140
register_command="aws ecs register-task-definition \
124141
--family ${task_family} \

plugin.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ configuration:
3232
type: string
3333
deployment-config:
3434
type: string
35+
env:
36+
type: array
3537
required:
3638
- cluster
3739
- service

tests/command.bats

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ expected_container_definition='[\n {\n "essential": true,\n "image": "hel
5252
export BUILDKITE_PLUGIN_ECS_DEPLOY_TASK_FAMILY=hello-world
5353
export BUILDKITE_PLUGIN_ECS_DEPLOY_IMAGE_0=hello-world:llamas
5454
export BUILDKITE_PLUGIN_ECS_DEPLOY_IMAGE_1=hello-world:alpacas
55+
5556
export BUILDKITE_PLUGIN_ECS_DEPLOY_TASK_DEFINITION=examples/multiple-images.json
5657

5758
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]'
@@ -77,6 +78,45 @@ expected_container_definition='[\n {\n "essential": true,\n "image": "hel
7778
unset BUILDKITE_PLUGIN_ECS_DEPLOY_IMAGE_1
7879
}
7980

81+
@test "Add env vars on multiple images" {
82+
export BUILDKITE_BUILD_NUMBER=1
83+
export BUILDKITE_PLUGIN_ECS_DEPLOY_CLUSTER=my-cluster
84+
export BUILDKITE_PLUGIN_ECS_DEPLOY_SERVICE=my-service
85+
export BUILDKITE_PLUGIN_ECS_DEPLOY_TASK_FAMILY=hello-world
86+
export BUILDKITE_PLUGIN_ECS_DEPLOY_IMAGE_0=hello-world:llamas
87+
export BUILDKITE_PLUGIN_ECS_DEPLOY_IMAGE_1=hello-world:alpacas
88+
export BUILDKITE_PLUGIN_ECS_DEPLOY_ENV_0="FOO=bar"
89+
export BUILDKITE_PLUGIN_ECS_DEPLOY_ENV_1="BAZ=bing"
90+
91+
export BUILDKITE_PLUGIN_ECS_DEPLOY_TASK_DEFINITION=examples/multiple-images.json
92+
93+
# first command stubbed saves the container definition to ${_TMP_DIR}/container_definition for later review and manipulation
94+
stub aws \
95+
"ecs register-task-definition --family hello-world --container-definitions '*' : echo \"\$6\" > ${_TMP_DIR}/container_definition ; echo '{\"taskDefinition\":{\"revision\":1}}'"
96+
97+
run "$PWD/hooks/command"
98+
99+
# there is no assert_success because we are just checking that the definition was updated accordingly
100+
assert_equal $(cat ${_TMP_DIR}/container_definition | jq -r '.[0].environment[0].name') 'FOO'
101+
assert_equal $(cat ${_TMP_DIR}/container_definition | jq -r '.[0].environment[0].value') 'bar'
102+
assert_equal $(cat ${_TMP_DIR}/container_definition | jq -r '.[1].environment[0].name') 'FOO'
103+
assert_equal $(cat ${_TMP_DIR}/container_definition | jq -r '.[1].environment[0].value') 'bar'
104+
assert_equal $(cat ${_TMP_DIR}/container_definition | jq -r '.[0].environment[1].name') 'BAZ'
105+
assert_equal $(cat ${_TMP_DIR}/container_definition | jq -r '.[0].environment[1].value') 'bing'
106+
assert_equal $(cat ${_TMP_DIR}/container_definition | jq -r '.[1].environment[1].name') 'BAZ'
107+
assert_equal $(cat ${_TMP_DIR}/container_definition | jq -r '.[1].environment[1].value') 'bing'
108+
109+
# as the aws command is called more times than stubbed, it is unstubbed automatically
110+
# unstub aws
111+
unset BUILDKITE_PLUGIN_ECS_DEPLOY_CLUSTER
112+
unset BUILDKITE_PLUGIN_ECS_DEPLOY_SERVICE
113+
unset BUILDKITE_PLUGIN_ECS_DEPLOY_TASK_DEFINITION
114+
unset BUILDKITE_PLUGIN_ECS_DEPLOY_IMAGE_0
115+
unset BUILDKITE_PLUGIN_ECS_DEPLOY_IMAGE_1
116+
unset BUILDKITE_PLUGIN_ECS_DEPLOY_ENV_0
117+
unset BUILDKITE_PLUGIN_ECS_DEPLOY_ENV_1
118+
}
119+
80120
@test "Run a deploy when service does not exist" {
81121
export BUILDKITE_BUILD_NUMBER=1
82122
export BUILDKITE_PLUGIN_ECS_DEPLOY_CLUSTER=my-cluster

0 commit comments

Comments
 (0)