66 lines
1.8 KiB
Bash
66 lines
1.8 KiB
Bash
#! /usr/bin/env sh
|
|
|
|
set -e
|
|
|
|
cleanup() {
|
|
if [ "${CLEAN_EXIT}" = false ]; then
|
|
echo "something went wrong"
|
|
echo "continue with the pipeline"
|
|
exit 0
|
|
fi
|
|
}
|
|
|
|
trap cleanup EXIT
|
|
CLEAN_EXIT=false
|
|
|
|
if [ -z "${BASE}" ]; then
|
|
echo "the environment variable BASE is not set"
|
|
exit 1
|
|
fi
|
|
|
|
if [ -z "${TARGET}" ]; then
|
|
echo "the environment variable TARGET is not set"
|
|
exit 1
|
|
fi
|
|
|
|
|
|
# add "library/" if its a "official" docker image (it has no username)
|
|
echo "${BASE}" | grep -q "/" || BASE="library/${BASE}"
|
|
echo "${TARGET}" | grep -q "/" || TARGET="library/${TARGET}"
|
|
|
|
|
|
# move tha tag to its own var and remove it from the repo name
|
|
BASE_TAG=$(echo "${BASE}" | sed 's/[^:]*://')
|
|
TARGET_TAG=$(echo "${TARGET}" | sed 's/[^:]*://')
|
|
|
|
BASE_REPO=$(echo "${BASE}" | sed 's/:.*//')
|
|
TARGET_REPO=$(echo "${TARGET}" | sed 's/:.*//')
|
|
|
|
BASE_URL="https://hub.docker.com/v2/repositories/${BASE_REPO}/tags/${BASE_TAG}"
|
|
TARGET_URL="https://hub.docker.com/v2/repositories/${TARGET_REPO}/tags/${TARGET_TAG}"
|
|
|
|
|
|
# login if credentias are passed
|
|
HEADER=""
|
|
if [ -n "${DOCKER_USERNAME}" ] && [ -n "${DOCKER_PASSWORD}" ]; then
|
|
TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'"${DOCKER_USERNAME}"'", "password": "'"${DOCKER_PASSWORD}"'"}' https://hub.docker.com/v2/users/login/ | jq -r .token)
|
|
HEADER="Authorization: JWT ${TOKEN}"
|
|
fi
|
|
|
|
|
|
# compare the update time of the BASE and TARGET images
|
|
BASE_DATE=$(curl -s -H "${HEADER}" "${BASE_URL}" | jq -r .last_updated | sed 's/T/ /' | sed 's/\..*//' )
|
|
TARGET_DATE=$(curl -s -H "${HEADER}" "${TARGET_URL}" | jq -r .last_updated | sed 's/T/ /' | sed 's/\..*//' )
|
|
|
|
BASE_TIMESTAMP=$(date -d "${BASE_DATE}" +%s)
|
|
TARGET_TIMESTAMP=$(date -d "${TARGET_DATE}" +%s)
|
|
|
|
CLEAN_EXIT=true
|
|
|
|
if [ "$BASE_TIMESTAMP" -le "$TARGET_TIMESTAMP" ]; then
|
|
echo "no update needed"
|
|
exit 1
|
|
fi
|
|
|
|
echo "$TARGET needs updating"
|
|
exit 0 |