-
Notifications
You must be signed in to change notification settings - Fork 823
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CI step to cancel previous running build for the same branch (#3006)
- Loading branch information
Showing
3 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters