Skip to content

Commit 06deb10

Browse files
authored
Cancel existing builds on running PRs (#2845)
* Cancel existing builds on running PRs * Use the actual job branch instead of always master
1 parent cefde70 commit 06deb10

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

.buildkite/build_pr_pipeline.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
steps:
2+
- key: "cancel-existing-builds"
3+
command: ".buildkite/scripts/cancel_running_pr.sh || true"
24
- key: "build-pr-setup"
35
label: "setup"
46
command: ".buildkite/scripts/build_pr_commit_status.sh pending"

.buildkite/hooks/pre-command

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export BUILD_PR_MACHINE_TYPE="n2-standard-4"
2828
# https://buildkite.com/docs/pipelines/managing-log-output#redacted-environment-variables
2929
if [[ "$BUILDKITE_PIPELINE_SLUG" == "docs-build-pr" ]];then
3030
export GITHUB_TOKEN=$(retry 5 vault kv get -field=value secret/ci/elastic-docs/docs_preview_cleaner)
31-
31+
export BUILDKITE_API_TOKEN=$(retry 5 vault kv get -field=value secret/ci/elastic-docs/buildkite_token)
3232
if [[ ${GITHUB_PR_BASE_REPO:="unset"} == "docs" ]]; then
3333
# Docs PR require a full rebuild - so let's boost the builds so they don't take 2 hours
3434
export BUILD_PR_MACHINE_TYPE="n2-highcpu-32"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
set +x
4+
5+
# This script should only be invoked by the Buildkite PR bot
6+
if [ -z ${GITHUB_PR_NUMBER+set} ] || [ -z ${GITHUB_PR_BASE_REPO+set} ];then
7+
echo "One of the following env. variable GITHUB_PR_NUMBER, GITHUB_PR_BASE_REPO is missing - exiting."
8+
exit 1
9+
fi
10+
11+
running_builds_url="https://api.buildkite.com/v2/organizations/elastic/pipelines/${BUILDKITE_PIPELINE_SLUG}/builds"
12+
running_builds_url+="?branch=${BUILDKITE_BRANCH}&state[]=scheduled&state[]=running"
13+
jq_filter="map(select(any(.meta_data; .repo_pr == \"${GITHUB_PR_BASE_REPO}_${GITHUB_PR_NUMBER}\"))) | .[] .number"
14+
15+
for bn in $(curl -sH "Authorization: Bearer ${BUILDKITE_API_TOKEN}" $running_builds_url | jq -c "${jq_filter}"); do
16+
if [ "$bn" != "${BUILDKITE_BUILD_NUMBER}" ];then
17+
echo "Cancelling build ${bn} targetting the same PR"
18+
cancel_url="https://api.buildkite.com/v2/organizations/elastic/pipelines/${BUILDKITE_PIPELINE_SLUG}/builds/${bn}/cancel"
19+
curl --silent -X PUT -H "Authorization: Bearer ${BUILDKITE_API_TOKEN}" "${cancel_url}" > /dev/null
20+
fi
21+
done
22+
23+

0 commit comments

Comments
 (0)