Skip to content

Commit bd03fae

Browse files
authored
Merge pull request #115 from nocache/events-since-deploy
Only report events since deploy
2 parents be64327 + e493567 commit bd03fae

File tree

4 files changed

+47
-28
lines changed

4 files changed

+47
-28
lines changed

hooks/command

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ fi
173173
task_revision=$(jq '.taskDefinition.revision' <<< "$json_output")
174174
echo "Registered ${task_family}:${task_revision}"
175175

176+
# Take advantage of the fact that ISO8601 format is sortable
177+
update_time=$(date +'%Y-%m-%dT%H:%M:%S')
176178
echo "--- :ecs: Updating service for ${service_name}"
177179
aws ecs update-service \
178180
${aws_default_args[@]+"${aws_default_args[@]}"} \
@@ -188,12 +190,12 @@ aws ecs wait services-stable \
188190
--cluster "${cluster}" \
189191
--services "${service_name}" || deploy_exitcode=$?
190192

191-
193+
## Get events since the time when we began the update, in ascending order
192194
service_events=$(aws ecs describe-services \
193195
${aws_default_args[@]+"${aws_default_args[@]}"} \
194196
--cluster "${cluster}" \
195197
--services "${service_name}" \
196-
--query 'services[].events' --output text)
198+
--query "services[].events[?createdAt >= '${update_time}']" --output text | sort)
197199

198200
if [[ $deploy_exitcode -eq 0 ]]; then
199201
echo "--- :ecs: Service is up 🚀"

tests/command.bats

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ setup() {
2020
"ecs register-task-definition --family hello-world --container-definitions \* : echo '{\"taskDefinition\":{\"revision\":1}}'" \
2121
"ecs update-service --cluster my-cluster --service my-service --task-definition hello-world:1 : echo ok" \
2222
"ecs wait services-stable --cluster my-cluster --services my-service : echo ok" \
23-
"ecs describe-services --cluster my-cluster --services my-service --query 'services[].events' --output text : echo ok"
23+
"ecs describe-services --cluster my-cluster --services my-service --query \* --output text : echo ok"
2424

2525
run "$PWD/hooks/command"
2626

@@ -43,7 +43,7 @@ setup() {
4343
"ecs register-task-definition --family hello-world --container-definitions $'$expected_multiple_container_definition' : echo '{\"taskDefinition\":{\"revision\":1}}'" \
4444
"ecs update-service --cluster my-cluster --service my-service --task-definition hello-world:1 : echo ok" \
4545
"ecs wait services-stable --cluster my-cluster --services my-service : echo ok" \
46-
"ecs describe-services --cluster my-cluster --services my-service --query 'services[].events' --output text : echo ok"
46+
"ecs describe-services --cluster my-cluster --services my-service --query \* --output text : echo ok"
4747

4848
run "$PWD/hooks/command"
4949

@@ -61,28 +61,27 @@ setup() {
6161
export BUILDKITE_PLUGIN_ECS_DEPLOY_ENV_0="FOO=bar"
6262
export BUILDKITE_PLUGIN_ECS_DEPLOY_ENV_1="BAZ=bing"
6363

64-
6564
# first command stubbed saves the container definition to ${TMP_DIR}/container_definition for later review and manipulation
6665
# we should be stubbing a lot more calls, but we don't care about those so let the stubbing fail
6766
stub aws \
6867
"ecs describe-task-definition --task-definition hello-world --query 'taskDefinition' : echo '{}'" \
6968
"ecs register-task-definition --family hello-world --container-definitions \* : echo \"\$6\" > ${_TMP_DIR}/container_definition ; echo '{\"taskDefinition\":{\"revision\":1}}'" \
7069
"ecs update-service --cluster my-cluster --service my-service --task-definition hello-world:1 : echo ok" \
7170
"ecs wait services-stable --cluster my-cluster --services my-service : echo ok" \
72-
"ecs describe-services --cluster my-cluster --services my-service --query 'services[].events' --output text : echo ok"
71+
"ecs describe-services --cluster my-cluster --services my-service --query \* --output text : echo ok"
7372

7473
run "$PWD/hooks/command"
7574

7675
assert_success
7776

7877
# check that the definition was updated accordingly
79-
assert_equal "$(jq -r '.[0].environment[0].name' "${_TMP_DIR}"/container_definition)" 'FOO'
78+
assert_equal "$(jq -r '.[0].environment[0].name' "${_TMP_DIR}"/container_definition)" 'FOO'
8079
assert_equal "$(jq -r '.[0].environment[0].value' "${_TMP_DIR}"/container_definition)" 'bar'
81-
assert_equal "$(jq -r '.[1].environment[0].name' "${_TMP_DIR}"/container_definition)" 'FOO'
80+
assert_equal "$(jq -r '.[1].environment[0].name' "${_TMP_DIR}"/container_definition)" 'FOO'
8281
assert_equal "$(jq -r '.[1].environment[0].value' "${_TMP_DIR}"/container_definition)" 'bar'
83-
assert_equal "$(jq -r '.[0].environment[1].name' "${_TMP_DIR}"/container_definition)" 'BAZ'
82+
assert_equal "$(jq -r '.[0].environment[1].name' "${_TMP_DIR}"/container_definition)" 'BAZ'
8483
assert_equal "$(jq -r '.[0].environment[1].value' "${_TMP_DIR}"/container_definition)" 'bing'
85-
assert_equal "$(jq -r '.[1].environment[1].name' "${_TMP_DIR}"/container_definition)" 'BAZ'
84+
assert_equal "$(jq -r '.[1].environment[1].name' "${_TMP_DIR}"/container_definition)" 'BAZ'
8685
assert_equal "$(jq -r '.[1].environment[1].value' "${_TMP_DIR}"/container_definition)" 'bing'
8786

8887
unstub aws
@@ -96,7 +95,7 @@ setup() {
9695
"ecs register-task-definition --family hello-world --container-definitions \* --task-role-arn arn:aws:iam::012345678910:role/world : echo '{\"taskDefinition\":{\"revision\":1}}'" \
9796
"ecs update-service --cluster my-cluster --service my-service --task-definition hello-world:1 : echo ok" \
9897
"ecs wait services-stable --cluster my-cluster --services my-service : echo ok" \
99-
"ecs describe-services --cluster my-cluster --services my-service --query 'services[].events' --output text : echo ok"
98+
"ecs describe-services --cluster my-cluster --services my-service --query \* --output text : echo ok"
10099

101100
run "$PWD/hooks/command"
102101

@@ -114,7 +113,7 @@ setup() {
114113
"ecs register-task-definition --family hello-world --container-definitions \* --execution-role-arn arn:aws:iam::012345678910:role/world : echo '{\"taskDefinition\":{\"revision\":1}}'" \
115114
"ecs update-service --cluster my-cluster --service my-service --task-definition hello-world:1 : echo ok" \
116115
"ecs wait services-stable --cluster my-cluster --services my-service : echo ok" \
117-
"ecs describe-services --cluster my-cluster --services my-service --query 'services[].events' --output text : echo ok"
116+
"ecs describe-services --cluster my-cluster --services my-service --query \* --output text : echo ok"
118117

119118
run "$PWD/hooks/command"
120119

@@ -124,7 +123,6 @@ setup() {
124123
unstub aws
125124
}
126125

127-
128126
@test "Region parameter is applied to all AWS calls" {
129127
export BUILDKITE_PLUGIN_ECS_DEPLOY_REGION=custom-region
130128

@@ -133,7 +131,7 @@ setup() {
133131
"ecs register-task-definition --region custom-region --family hello-world --container-definitions \* : echo '{\"taskDefinition\":{\"revision\":1}}'" \
134132
"ecs update-service --region custom-region --cluster my-cluster --service my-service --task-definition hello-world:1 : echo ok" \
135133
"ecs wait services-stable --region custom-region --cluster my-cluster --services my-service : echo ok" \
136-
"ecs describe-services --region custom-region --cluster my-cluster --services my-service --query 'services[].events' --output text : echo ok"
134+
"ecs describe-services --region custom-region --cluster my-cluster --services my-service --query \* --output text : echo ok"
137135

138136
run "$PWD/hooks/command"
139137

@@ -142,3 +140,22 @@ setup() {
142140

143141
unstub aws
144142
}
143+
144+
@test "Filters past events and sorts them" {
145+
current_date="2024-05-04T12:34:56.789000+08:00"
146+
stub date "+'%Y-%m-%dT%H:%M:%S' : echo $current_date"
147+
stub aws \
148+
"ecs describe-task-definition \* \* \* \* : echo '{}'" \
149+
"ecs register-task-definition \* \* \* \* : echo '{}'" \
150+
"ecs update-service \* \* \* \* \* \* : echo ok" \
151+
"ecs wait services-stable \* \* \* \* : echo ok" \
152+
"ecs describe-services \* \* \* \* --query \"services[].events[?createdAt >= '$current_date']\" --output text : echo '2019-12-12T00:00:01.000Z\tnope\n$current_date\tok'"
153+
154+
run "$PWD/hooks/command"
155+
156+
assert_success
157+
assert_output --partial "ok"
158+
159+
unstub aws
160+
unstub date
161+
}

tests/deprecated-options.bats

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ setup() {
4545
"ecs register-task-definition --family hello-world --container-definitions \* : echo '{\"taskDefinition\":{\"revision\":1}}'" \
4646
"ecs update-service --cluster my-cluster --service my-service --task-definition hello-world:1 : echo ok" \
4747
"ecs wait services-stable --cluster my-cluster --services my-service : echo ok" \
48-
"ecs describe-services --cluster my-cluster --services my-service --query 'services[].events' --output text : echo ok"
48+
"ecs describe-services --cluster my-cluster --services my-service --query \* --output text : echo ok"
4949

5050
run "$PWD/hooks/command"
5151

@@ -58,4 +58,4 @@ setup() {
5858
assert_output --partial "target-group parameter has been deprecated"
5959

6060
unstub aws
61-
}
61+
}

tests/existing-data.bats

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ setup() {
1919
"ecs register-task-definition --family hello-world --container-definitions \* : echo '{\"taskDefinition\":{\"revision\":1}}'" \
2020
"ecs update-service --cluster my-cluster --service my-service --task-definition hello-world:1 : echo ok" \
2121
"ecs wait services-stable --cluster my-cluster --services my-service : echo ok" \
22-
"ecs describe-services --cluster my-cluster --services my-service --query 'services[].events' --output text : echo ok"
22+
"ecs describe-services --cluster my-cluster --services my-service --query \* --output text : echo ok"
2323

2424
run "$PWD/hooks/command"
2525

@@ -39,7 +39,7 @@ setup() {
3939
"ecs register-task-definition --family hello-world --container-definitions \* : echo '{\"taskDefinition\":{\"revision\":1}}'" \
4040
"ecs update-service --cluster my-cluster --service my-service --task-definition hello-world:1 : echo ok" \
4141
"ecs wait services-stable --cluster my-cluster --services my-service : echo ok" \
42-
"ecs describe-services --cluster my-cluster --services my-service --query 'services[].events' --output text : echo ok"
42+
"ecs describe-services --cluster my-cluster --services my-service --query \* --output text : echo ok"
4343

4444
run "$PWD/hooks/command"
4545

@@ -63,20 +63,20 @@ setup() {
6363
"ecs register-task-definition --family hello-world --container-definitions \* : echo \"\$6\" > ${_TMP_DIR}/container_definition ; echo '{\"taskDefinition\":{\"revision\":1}}'" \
6464
"ecs update-service --cluster my-cluster --service my-service --task-definition hello-world:1 : echo ok" \
6565
"ecs wait services-stable --cluster my-cluster --services my-service : echo ok" \
66-
"ecs describe-services --cluster my-cluster --services my-service --query 'services[].events' --output text : echo ok"
66+
"ecs describe-services --cluster my-cluster --services my-service --query \* --output text : echo ok"
6767

6868
run "$PWD/hooks/command"
6969

7070
assert_success
7171

7272
# check that the definition was updated accordingly
73-
assert_equal "$(jq -r '.[0].environment[0].name' "${_TMP_DIR}"/container_definition)" 'FOO'
73+
assert_equal "$(jq -r '.[0].environment[0].name' "${_TMP_DIR}"/container_definition)" 'FOO'
7474
assert_equal "$(jq -r '.[0].environment[0].value' "${_TMP_DIR}"/container_definition)" 'bar'
75-
assert_equal "$(jq -r '.[1].environment[0].name' "${_TMP_DIR}"/container_definition)" 'FOO'
75+
assert_equal "$(jq -r '.[1].environment[0].name' "${_TMP_DIR}"/container_definition)" 'FOO'
7676
assert_equal "$(jq -r '.[1].environment[0].value' "${_TMP_DIR}"/container_definition)" 'bar'
77-
assert_equal "$(jq -r '.[0].environment[1].name' "${_TMP_DIR}"/container_definition)" 'BAZ'
77+
assert_equal "$(jq -r '.[0].environment[1].name' "${_TMP_DIR}"/container_definition)" 'BAZ'
7878
assert_equal "$(jq -r '.[0].environment[1].value' "${_TMP_DIR}"/container_definition)" 'bing'
79-
assert_equal "$(jq -r '.[1].environment[1].name' "${_TMP_DIR}"/container_definition)" 'BAZ'
79+
assert_equal "$(jq -r '.[1].environment[1].name' "${_TMP_DIR}"/container_definition)" 'BAZ'
8080
assert_equal "$(jq -r '.[1].environment[1].value' "${_TMP_DIR}"/container_definition)" 'bing'
8181

8282
unstub aws
@@ -90,7 +90,7 @@ setup() {
9090
"ecs register-task-definition --family hello-world --container-definitions \* --task-role-arn arn:aws:iam::012345678910:role/world : echo '{\"taskDefinition\":{\"revision\":1}}'" \
9191
"ecs update-service --cluster my-cluster --service my-service --task-definition hello-world:1 : echo ok" \
9292
"ecs wait services-stable --cluster my-cluster --services my-service : echo ok" \
93-
"ecs describe-services --cluster my-cluster --services my-service --query 'services[].events' --output text : echo ok"
93+
"ecs describe-services --cluster my-cluster --services my-service --query \* --output text : echo ok"
9494

9595
run "$PWD/hooks/command"
9696

@@ -108,7 +108,7 @@ setup() {
108108
"ecs register-task-definition --family hello-world --container-definitions \* --execution-role-arn arn:aws:iam::012345678910:role/world : echo '{\"taskDefinition\":{\"revision\":1}}'" \
109109
"ecs update-service --cluster my-cluster --service my-service --task-definition hello-world:1 : echo ok" \
110110
"ecs wait services-stable --cluster my-cluster --services my-service : echo ok" \
111-
"ecs describe-services --cluster my-cluster --services my-service --query 'services[].events' --output text : echo ok"
111+
"ecs describe-services --cluster my-cluster --services my-service --query \* --output text : echo ok"
112112

113113
run "$PWD/hooks/command"
114114

@@ -119,15 +119,15 @@ setup() {
119119
}
120120

121121
@test "Run a deploy when the container definition is incorrect" {
122-
122+
123123
stub aws \
124124
"ecs describe-task-definition --task-definition hello-world --query taskDefinition : echo '{}'"
125125

126126
run "$PWD/hooks/command"
127-
127+
128128
assert_failure
129129
assert_output --partial 'Invalid container definition (should be in the format of [{"image": "..."}] )'
130-
130+
131131
unstub aws
132132
}
133133

0 commit comments

Comments
 (0)