Skip to content

Commit e01aafd

Browse files
committed
look three builds back
1 parent 5a059e2 commit e01aafd

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

.buildkite/scripts/check_queue.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
last_build_url="https://api.buildkite.com/v2/organizations/elastic/pipelines/${BUILDKITE_PIPELINE_SLUG}/builds?branch=${BUILDKITE_BRANCH}"
44
cancel_build_url="https://api.buildkite.com/v2/organizations/elastic/pipelines/${BUILDKITE_PIPELINE_SLUG}/builds/${BUILDKITE_JOB_ID}/cancel"
55

6-
LAST_BUILD_STATE=$(curl -s -H "Authorization: Bearer ${BUILDKITE_API_TOKEN}" $last_successful_build_url | jq -r '.[1].status')
6+
# Don't look at this build (it's running now!)
7+
# Don't look at the last build (it's okay if it's still running!)
8+
# Look three builds back instead (if this build is still running,
9+
# it means there's already one in the queue and we can safely cancel this one)
10+
THIRD_TO_LAST_BUILD_STATE=$(curl -s -H "Authorization: Bearer ${BUILDKITE_API_TOKEN}" $last_successful_build_url | jq -r '.[2].status')
711

8-
echo "Determining if the last build is currently blocked."
9-
if [[ "$LAST_BUILD_STATE" == "blocked" ]]; then
12+
echo "Determining if there are multiple builds waiting."
13+
if [[ "$THIRD_TO_LAST_BUILD_STATE" == "running" ]]; then
1014
echo "The pipeline is congested. Canceling this build."
1115
curl -sX PUT -H "Authorization: Bearer ${BUILDKITE_API_TOKEN}" $cancel_build_url
1216
else

0 commit comments

Comments
 (0)