agones.dev/agones@v1.54.0/ci/cancelot.sh (about) 1 #!/usr/bin/env bash 2 # Copyright 2023 Google LLC All Rights Reserved. 3 # 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 16 # 17 # Provides automation for cancelling Cloud Builds 18 # Use as a first step to cancel previous builds currently in progress or queued for the same branch name and trigger id. 19 # Similar to: https://github.com/GoogleCloudPlatform/cloud-builders-community/tree/master/cancelot 20 # 21 # Usage within Cloud Build step: 22 # steps: 23 # - name: 'gcr.io/cloud-builders/gcloud-slim:latest' 24 # entrypoint: 'bash' 25 # args: ['./cancelot.sh', '--current_build_id', '$BUILD_ID'] 26 27 # Exit script when command fails 28 set -o errexit 29 # Return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status 30 set -o pipefail 31 32 CMDNAME=${0##*/} 33 echoerr() { echo "$@" 1>&2; } 34 35 usage() { 36 cat <<USAGE >&2 37 Usage: 38 $CMDNAME --current_build_id \$BUILD_ID 39 --current_build_id \$BUILD_ID Current Build Id 40 USAGE 41 exit 1 42 } 43 44 # Process arguments 45 while [[ $# -gt 0 ]]; do 46 case "$1" in 47 --current_build_id) 48 CURRENT_BUILD_ID="$2" 49 if [[ $CURRENT_BUILD_ID == "" ]]; then break; fi 50 shift 2 51 ;; 52 --help) 53 usage 54 ;; 55 *) 56 echoerr "Unknown argument: $1" 57 usage 58 ;; 59 esac 60 done 61 62 if [[ "$CURRENT_BUILD_ID" == "" ]]; then 63 echo "Error: you need to provide Build Id" 64 usage 65 fi 66 67 # Note BUILD_BRANCH and BUILD_TRIGGER_ID could be empty 68 QUERY_BUILD=$(gcloud builds describe "$CURRENT_BUILD_ID" --format="csv[no-heading](createTime, buildTriggerId, substitutions.BRANCH_NAME)") 69 IFS="," read -r BUILD_CREATE_TIME BUILD_TRIGGER_ID BUILD_BRANCH <<<"$QUERY_BUILD" 70 71 FILTERS="id!=$CURRENT_BUILD_ID AND createTime<$BUILD_CREATE_TIME AND substitutions.BRANCH_NAME=$BUILD_BRANCH AND buildTriggerId=$BUILD_TRIGGER_ID" 72 73 echo "Filtering ongoing builds for branch '$BUILD_BRANCH' trigger id '$BUILD_TRIGGER_ID' created before: $BUILD_CREATE_TIME" 74 75 # Get ongoing build ids to cancel (+status) 76 while IFS=$'\n' read -r line; do CANCEL_BUILDS+=("$line"); done < <(gcloud builds list --ongoing --filter="$FILTERS" --format="value(id, status)") 77 78 BUILDS_COUNT=${#CANCEL_BUILDS[@]} 79 echo "Found $BUILDS_COUNT builds to cancel" 80 if [[ $BUILDS_COUNT -eq 0 ]]; then 81 exit 0 82 fi 83 84 # Cancel builds one by one to get output for each 85 # printf '%s\n' "${CANCEL_BUILDS[@]}" 86 echo "BUILD ID CURRENT STATUS" 87 for build in "${CANCEL_BUILDS[@]}"; do 88 echo "$build" 89 ID=$(echo "$build" | awk '{print $1;}') 90 gcloud builds cancel "$ID" >/dev/null || true 91 done