Skip to content

Commit

Permalink
Add CI step to cancel previous running build for the same branch (#3006)
Browse files Browse the repository at this point in the history
  • Loading branch information
gongmax authored Mar 2, 2023
1 parent f5f11f4 commit 7f3fe1a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
76 changes: 76 additions & 0 deletions ci/cancelot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#
# Provides automation for cancelling Cloud Builds
# Use as a first step to cancel previous builds currently in progress or queued for the same branch name and trigger id.
# Similar to: https://github.com/GoogleCloudPlatform/cloud-builders-community/tree/master/cancelot
#
# Usage within Cloud Build step:
# steps:
# - name: 'gcr.io/cloud-builders/gcloud-slim:latest'
# entrypoint: 'bash'
# args: ['./cancelot.sh', '--current_build_id', '$BUILD_ID']

# Exit script when command fails
set -o errexit
# Return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status
set -o pipefail

CMDNAME=${0##*/}
echoerr() { echo "$@" 1>&2; }

usage() {
cat <<USAGE >&2
Usage:
$CMDNAME --current_build_id \$BUILD_ID
--current_build_id \$BUILD_ID Current Build Id
USAGE
exit 1
}

# Process arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--current_build_id)
CURRENT_BUILD_ID="$2"
if [[ $CURRENT_BUILD_ID == "" ]]; then break; fi
shift 2
;;
--help)
usage
;;
*)
echoerr "Unknown argument: $1"
usage
;;
esac
done

if [[ "$CURRENT_BUILD_ID" == "" ]]; then
echo "Error: you need to provide Build Id"
usage
fi

# Note BUILD_BRANCH and BUILD_TRIGGER_ID could be empty
QUERY_BUILD=$(gcloud builds describe "$CURRENT_BUILD_ID" --format="csv[no-heading](createTime, buildTriggerId, substitutions.BRANCH_NAME)")
IFS="," read -r BUILD_CREATE_TIME BUILD_TRIGGER_ID BUILD_BRANCH <<<"$QUERY_BUILD"

FILTERS="id!=$CURRENT_BUILD_ID AND createTime<$BUILD_CREATE_TIME AND substitutions.BRANCH_NAME=$BUILD_BRANCH AND buildTriggerId=$BUILD_TRIGGER_ID"

echo "Filtering ongoing builds for branch '$BUILD_BRANCH' trigger id '$BUILD_TRIGGER_ID' created before: $BUILD_CREATE_TIME"

# Get ongoing build ids to cancel (+status)
while IFS=$'\n' read -r line; do CANCEL_BUILDS+=("$line"); done < <(gcloud builds list --ongoing --filter="$FILTERS" --format="value(id, status)")

BUILDS_COUNT=${#CANCEL_BUILDS[@]}
echo "Found $BUILDS_COUNT builds to cancel"
if [[ $BUILDS_COUNT -eq 0 ]]; then
exit 0
fi

# Cancel builds one by one to get output for each
# printf '%s\n' "${CANCEL_BUILDS[@]}"
echo "BUILD ID CURRENT STATUS"
for build in "${CANCEL_BUILDS[@]}"; do
echo "$build"
ID=$(echo "$build" | awk '{print $1;}')
gcloud builds cancel "$ID" >/dev/null || true
done
File renamed without changes.
11 changes: 10 additions & 1 deletion cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@

steps:

#
# Cancel the previous build for the same branch
#

- name: 'gcr.io/cloud-builders/gcloud-slim:latest'
id: cancelot
entrypoint: 'bash'
args: ['./ci/cancelot.sh', '--current_build_id', '$BUILD_ID']

#
# Print Docker version
#
Expand Down Expand Up @@ -314,7 +323,7 @@ steps:
testClusterLocation="${region}"
testCluster="gke-autopilot-e2e-test-cluster-${version//./-}"
fi
{ gcloud builds submit . --config=e2e-test-cloudbuild.yaml \
{ gcloud builds submit . --config=./ci/e2e-test-cloudbuild.yaml \
--substitutions _FEATURE_WITH_GATE=$featureWithGate,_FEATURE_WITHOUT_GATE=$featureWithoutGate,_CLOUD_PRODUCT=$cloudProduct,_TEST_CLUSTER_NAME=$testCluster,_TEST_CLUSTER_LOCATION=$testClusterLocation,_REGISTRY=${_REGISTRY} \
|& sed "s/^/${cloudProduct}-${version}: /"; } &
pids+=($!)
Expand Down

0 comments on commit 7f3fe1a

Please sign in to comment.