Skip to content

Instantly share code, notes, and snippets.

@sbrichardson
Created November 3, 2017 21:25
Show Gist options
  • Save sbrichardson/6d8da95bacd270c63d52be43d5765e0f to your computer and use it in GitHub Desktop.
Save sbrichardson/6d8da95bacd270c63d52be43d5765e0f to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Checks Google Cloud container registry to see if build has finished deploying new image from source repository recent update.
# Retries 15 times, waiting 20 seconds between attempts.
#
DEPLOYMENT="$(basename -s .git `git config --get remote.origin.url`)"
PROJECT_ID="$(gcloud config get-value project)"
IMAGE_NAME="gcr.io/$(gcloud config get-value project)/$DEPLOYMENT:$(git rev-parse HEAD)"
function fail {
echo $1 >&2
exit 1
}
trap 'fail "The deployment was aborted. Message -- "' ERR
function configure {
echo "Validating configuration..."
[ ! -z "$DEPLOYMENT" ] || fail "Configuration option is not set: DEPLOYMENT"
[ ! -z "$PROJECT_ID" ] || fail "Configuration option is not set: PROJECT_ID"
[ ! -z "$IMAGE_NAME" ] || fail "Configuration option is not set: IMAGE_NAME"
}
function attempt_build {
echo "Checking build status for $IMAGE_NAME"
local n=1
local max=15
local delay=20
while true; do
gcloud container builds list | grep $IMAGE_NAME | grep -q SUCCESS && break || {
if [[ $n -lt $max ]]; then
((n++))
echo "Build not completed. Attempt $n/$max:"
sleep $delay;
else
fail "The command has failed after $n attempts."
fi
}
done
}
function set_image {
echo Container build completed, updating $DEPLOYMENT ...
kubectl set image deployment/$DEPLOYMENT $DEPLOYMENT=$IMAGE_NAME
}
configure
attempt_build
set_image
echo "$DEPLOYMENT | Deployment complete"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment