Skip to content

Cancel existing builds on running PRs #2845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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