Skip to content

Instantly share code, notes, and snippets.

@zimbatm
Created May 10, 2018 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zimbatm/c106f1007843889d92d6bb115d43b479 to your computer and use it in GitHub Desktop.
Save zimbatm/c106f1007843889d92d6bb115d43b479 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# Usage: ./skopeo-push-maybe <SOURCE-IMAGE> <DEST-IMAGE>
#
# Like skopeo copy but assumes that if the image already exists there is no
# need to re-upload the image.
#
# Needs skopeo 0.1.29+
set -uo pipefail
# Config
source=$1
dest=$2
## Main
# Avoid re-uploading images
out=$(skopeo inspect "${dest}" 2>&1)
es=$?
if [[ $es = 0 ]]; then
echo "image already exists"
exit 0
fi
# Ugly differentiator on error message output since all exit with the same
# exit status
if echo "$out" | grep "manifest unknown" >/dev/null; then
# image doesn't exist, upload
exec skopeo copy "$source" "$dest"
fi
# some other error
echo "$out"
exit "$es"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment