Last active
May 16, 2023 00:48
-
-
Save orrisroot/efb5eaff0cf2d5c280360abeac3951d0 to your computer and use it in GitHub Desktop.
Detect updates to the Docker base image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
REQUIRED_COMMANDS=("skopeo" "jq" "basename") | |
for COMMAND in "${REQUIRED_COMMANDS}"; do | |
if ! command -v ${COMMAND} > /dev/null; then | |
echo "Error: ${COMMAND} command is required." 1>&2 | |
exit 2 | |
fi | |
done | |
if [ $# -ne 2 ]; then | |
echo "Usage: $(basename $0) BASE_IMAGE_NAME:TAG IMAGE_NAME:TAG" | |
exit 2 | |
fi | |
BASE_CONTAINER=$1 | |
TARGET_CONTAINER=$2 | |
BASE_JSON=$(skopeo inspect --config docker://${BASE_CONTAINER}) | |
[ $? -eq 0 ] || exit 1 | |
TARGET_JSON=$(skopeo inspect --config docker://${TARGET_CONTAINER}) | |
[ $? -eq 0 ] || exit 1 | |
BASE_LAYER_ID=$(echo ${BASE_JSON} | jq "(.rootfs.diff_ids | reverse)[0]") | |
FOUND_LAYER_ID=$(echo ${TARGET_JSON} | jq ".rootfs.diff_ids[] | select(. == ${BASE_LAYER_ID})") | |
if [ "${BASE_LAYER_ID}" != "${FOUND_LAYER_ID}" ]; then | |
echo "The Docker image needs to be updated." | |
echo "- ${TARGET_CONTAINER}" | |
echo " |- ${BASE_CONTAINER} [newer version found]" | |
echo "" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment