Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Execute script if new docker image is available
#!/bin/bash
# Example usage:
# ./docker-image-update-check.sh gitlab/gitlab-ce update-gitlab.sh
IMAGE="$1"
COMMAND="$2"
echo "Fetching Docker Hub token..."
token=$(curl --silent "https://auth.docker.io/token?scope=repository:$IMAGE:pull&service=registry.docker.io" | jq -r '.token')
echo -n "Fetching remote digest... "
digest=$(curl --silent -H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
-H "Authorization: Bearer $token" \
"https://registry.hub.docker.com/v2/$IMAGE/manifests/latest" | jq -r '.config.digest')
echo "$digest"
echo -n "Fetching local digest... "
local_digest=$(docker images -q --no-trunc $IMAGE:latest)
echo "$local_digest"
if [ "$digest" != "$local_digest" ] ; then
echo "Update available. Executing update command..."
($COMMAND)
else
echo "Already up to date. Nothing to do."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.