Skip to content

Commit 97fb7ac

Browse files
authored
Merge pull request #77 from buildkite-plugins/toote_container_defs_task_defs
Container/Task/Service definitions
2 parents 250472c + 8fa5f8d commit 97fb7ac

File tree

8 files changed

+179
-37
lines changed

8 files changed

+179
-37
lines changed

.buildkite/pipeline.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
steps:
22
- label: ":docker: :hammer:"
33
plugins:
4-
docker-compose#v3.9.0:
4+
docker-compose#v3.10.0:
55
run: tests
66

77
- label: ":shell: Lint"

README.md

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ steps:
1818
- ecs-deploy#v1.4.1:
1919
cluster: "my-ecs-cluster"
2020
service: "my-service"
21-
task-definition: "examples/hello-world.json"
21+
container-definitions: "examples/hello-world.json"
2222
task-family: "hello-world"
2323
image: "${ECR_REPOSITORY}/hello-world:${BUILDKITE_BUILD_NUMBER}"
2424
```
@@ -37,11 +37,62 @@ The name of the ECS service.
3737

3838
Example: `"my-service"`
3939

40+
### `container-definitions`
41+
42+
The file path to the ECS container definition JSON file. This JSON file must be an array of objects, each corresponding to one of the images you defined in the `image` parameter.
43+
44+
Example: `"ecs/containers.json"`
45+
```json
46+
[
47+
{
48+
"essential": true,
49+
"image": "amazon/amazon-ecs-sample",
50+
"memory": 100,
51+
"name": "sample",
52+
"portMappings": [
53+
{
54+
"containerPort": 80,
55+
"hostPort": 80
56+
}
57+
]
58+
},
59+
{
60+
"essential": true,
61+
"image": "amazon/amazon-ecs-sample",
62+
"memory": 100,
63+
"name": "sample",
64+
"portMappings": [
65+
{
66+
"containerPort": 80,
67+
"hostPort": 80
68+
}
69+
]
70+
}
71+
]
72+
```
73+
4074
### `task-definition`
4175

42-
The file path to the ECS task definition JSON file.
76+
The file path to the ECS task definition JSON file. Parameters specified in this file will be overridden by other arguments if set. Setting the `containers` property in this file will have no effect, define those parameters in `container-definitions`
4377

4478
Example: `"ecs/task.json"`
79+
```json
80+
{
81+
"networkMode": "awsvpc"
82+
}
83+
```
84+
85+
### `service-definition`
86+
87+
The file path to the ECS service definition JSON file. Parameters specified in this file will be overridden by other arguments if set, e.g. `cluster`, `desired-count`, etc. Note that currently this json input will only be used when creating the service, NOT when updating it.
88+
89+
Example: `"ecs/service.json"`
90+
```json
91+
{
92+
"schedulingStrategy": "DAEMON",
93+
"propagateTags": "TASK_DEFINITION"
94+
}
95+
```
4596

4697
### `task-family`
4798

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
version: '2'
22
services:
33
tests:
4-
image: buildkite/plugin-tester:v3.0.0
4+
image: buildkite/plugin-tester:v3.0.1
55
volumes:
66
- ".:/plugin:ro"

examples/service-definition.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"schedulingStrategy": "DAEMON",
3+
"propagateTags": "TASK_DEFINITION"
4+
}

examples/task-definition.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"networkMode": "awsvpc"
3+
}

hooks/command

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ images=()
3131
while read -r line ; do
3232
[[ -n "$line" ]] && images+=("$line")
3333
done <<< "$(plugin_read_list IMAGE)"
34-
task_definition=${BUILDKITE_PLUGIN_ECS_DEPLOY_TASK_DEFINITION?}
34+
task_definition=${BUILDKITE_PLUGIN_ECS_DEPLOY_TASK_DEFINITION:-""}
35+
service_definition=${BUILDKITE_PLUGIN_ECS_DEPLOY_SERVICE_DEFINITION:-""}
36+
container_definitions=${BUILDKITE_PLUGIN_ECS_DEPLOY_CONTAINER_DEFINITIONS?}
3537
desired_count=${BUILDKITE_PLUGIN_ECS_DEPLOY_DESIRED_COUNT:-"1"}
3638
task_role_arn=${BUILDKITE_PLUGIN_ECS_DEPLOY_TASK_ROLE_ARN:-""}
3739
target_group=${BUILDKITE_PLUGIN_ECS_DEPLOY_TARGET_GROUP:-""}
@@ -60,9 +62,9 @@ min_max_percent=(${deployment_config//\// })
6062
min_deploy_perc=${min_max_percent[0]}
6163
max_deploy_perc=${min_max_percent[1]}
6264

63-
if [[ $(jq '. | keys |first' "$task_definition") != "0" ]]; then
65+
if [[ $(jq '. | keys |first' "$container_definitions") != "0" ]]; then
6466
echo "^^^"
65-
echo "Invalid Task Definition"
67+
echo "Invalid Container Definitions"
6668
echo 'JSON definition should be in the format of [{"image": "..."}]'
6769
exit 1
6870
fi
@@ -74,6 +76,11 @@ function create_service() {
7476
local desired_count=$4
7577
local target_group_arguments
7678
target_group_arguments=$(generate_target_group_arguments "$5" "$6" "$7")
79+
80+
local service_definition_json="{}"
81+
if [[ -n "${service_definition}" ]]; then
82+
service_definition_json=$(cat "${service_definition}")
83+
fi
7784

7885
# shellcheck disable=SC2016
7986
service_defined=$(aws ecs describe-services --cluster "$cluster_name" --service "$service_name" --query 'services[?status==`ACTIVE`].status' --output text |wc -l)
@@ -86,7 +93,8 @@ function create_service() {
8693
--task-definition "$task_definition" \
8794
--desired-count "$desired_count" \
8895
--deployment-configuration "maximumPercent=${max_deploy_perc},minimumHealthyPercent=${min_deploy_perc}" \
89-
$target_group_arguments
96+
$target_group_arguments \
97+
--cli-input-json "$service_definition_json"
9098
fi
9199
}
92100

@@ -115,7 +123,7 @@ function generate_target_group_arguments() {
115123

116124
## This is the template definition of your containers
117125
image_idx=0
118-
container_definitions_json=$(cat "${task_definition}")
126+
container_definitions_json=$(cat "${container_definitions}")
119127
for image in "${images[@]}"; do
120128
container_definitions_json=$(echo "$container_definitions_json" | jq --arg IMAGE "$image" \
121129
".[${image_idx}].image=\$IMAGE"
@@ -149,6 +157,13 @@ if [[ -n "${execution_role}" ]]; then
149157
register_command+=" --execution-role-arn ${execution_role}"
150158
fi
151159

160+
if [[ -n "${task_definition}" ]]; then
161+
task_definition_json=$(cat "${task_definition}")
162+
register_command+=" --cli-input-json '${task_definition_json}'"
163+
fi
164+
165+
echo "--- :ecs: register command:"
166+
152167
json_output=$(eval "$register_command")
153168
register_exit_code=$?
154169

plugin.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ configuration:
88
properties:
99
cluster:
1010
type: string
11+
container-definitions:
12+
type: string
1113
service:
1214
type: string
1315
task-definition:
1416
type: string
1517
task-family:
1618
type: string
19+
service-definition:
20+
type: string
1721
image:
1822
type: [ string, array ]
1923
desired-count:
@@ -37,6 +41,6 @@ configuration:
3741
required:
3842
- cluster
3943
- service
40-
- task-definition
44+
- container-definitions
4145
- task-family
4246
- image

0 commit comments

Comments
 (0)