Skip to content

Instantly share code, notes, and snippets.

@lkebin
Created August 23, 2019 13:32
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 lkebin/95266ddb6a7c53d4e2418f444fdb5836 to your computer and use it in GitHub Desktop.
Save lkebin/95266ddb6a7c53d4e2418f444fdb5836 to your computer and use it in GitHub Desktop.
Run command after Docker image update, but nothing to do if no update
#!/bin/sh
script=$(basename $0)
image=$1
arg=$@
function current() {
local a=$(docker inspect ${image} -f '{{index .RepoDigests 0}}')
echo ${a#*@}
}
function latest() {
docker pull ${image} | grep Digest | awk '{print $2}'
}
function usage() {
echo "Usage: ${script} name[:tag|@digest] [-- cmd [arguments]]"
}
function cmd() {
echo ${arg#*--}
}
function main() {
if [[ "$image" == "" ]]; then
usage
exit 1
fi
# Get current first
currentHash=$(current)
# Get latest second
latestHash=$(latest)
echo "OLD:[$latestHash]"
echo "NEW:[$currentHash]"
if [[ "$latestHash" != "$currentHash" ]]; then
exec $(cmd)
fi
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment