Skip to content

Commit

Permalink
Cancel existing builds on running PRs (#2845)
Browse files Browse the repository at this point in the history
* Cancel existing builds on running PRs

* Use the actual job branch instead of always master
  • Loading branch information
nkammah authored Dec 4, 2023
1 parent cefde70 commit 06deb10
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .buildkite/build_pr_pipeline.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
steps:
- key: "cancel-existing-builds"
command: ".buildkite/scripts/cancel_running_pr.sh || true"
- key: "build-pr-setup"
label: "setup"
command: ".buildkite/scripts/build_pr_commit_status.sh pending"
Expand Down
2 changes: 1 addition & 1 deletion .buildkite/hooks/pre-command
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export BUILD_PR_MACHINE_TYPE="n2-standard-4"
# https://buildkite.com/docs/pipelines/managing-log-output#redacted-environment-variables
if [[ "$BUILDKITE_PIPELINE_SLUG" == "docs-build-pr" ]];then
export GITHUB_TOKEN=$(retry 5 vault kv get -field=value secret/ci/elastic-docs/docs_preview_cleaner)

export BUILDKITE_API_TOKEN=$(retry 5 vault kv get -field=value secret/ci/elastic-docs/buildkite_token)
if [[ ${GITHUB_PR_BASE_REPO:="unset"} == "docs" ]]; then
# Docs PR require a full rebuild - so let's boost the builds so they don't take 2 hours
export BUILD_PR_MACHINE_TYPE="n2-highcpu-32"
Expand Down
23 changes: 23 additions & 0 deletions .buildkite/scripts/cancel_running_pr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash
set -euo pipefail
set +x

# This script should only be invoked by the Buildkite PR bot
if [ -z ${GITHUB_PR_NUMBER+set} ] || [ -z ${GITHUB_PR_BASE_REPO+set} ];then
echo "One of the following env. variable GITHUB_PR_NUMBER, GITHUB_PR_BASE_REPO is missing - exiting."
exit 1
fi

running_builds_url="https://api.buildkite.com/v2/organizations/elastic/pipelines/${BUILDKITE_PIPELINE_SLUG}/builds"
running_builds_url+="?branch=${BUILDKITE_BRANCH}&state[]=scheduled&state[]=running"
jq_filter="map(select(any(.meta_data; .repo_pr == \"${GITHUB_PR_BASE_REPO}_${GITHUB_PR_NUMBER}\"))) | .[] .number"

for bn in $(curl -sH "Authorization: Bearer ${BUILDKITE_API_TOKEN}" $running_builds_url | jq -c "${jq_filter}"); do
if [ "$bn" != "${BUILDKITE_BUILD_NUMBER}" ];then
echo "Cancelling build ${bn} targetting the same PR"
cancel_url="https://api.buildkite.com/v2/organizations/elastic/pipelines/${BUILDKITE_PIPELINE_SLUG}/builds/${bn}/cancel"
curl --silent -X PUT -H "Authorization: Bearer ${BUILDKITE_API_TOKEN}" "${cancel_url}" > /dev/null
fi
done


0 comments on commit 06deb10

Please sign in to comment.