#! /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 if [ -z "${REPO_URL}" ]; then REPO_URL="https://hub.docker.com/v2/repositories" fi if [ -z "${BASE_REPO_URL}" ]; then BASE_REPO_URL="${REPO_URL}" fi if [ -z "${TARGET_REPO_URL}" ]; then TARGET_REPO_URL="${REPO_URL}" fi if [ -z "${BASE_REPO_USERNAME}" && -n "${REPO_USERNAME}" ]; then BASE_REPO_USERNAME="${REPO_USERNAME}" fi if [ -z "${BASE_REPO_PASSWORD}" && -n "${REPO_PASSWORD}" ]; then BASE_REPO_PASSWORD="${REPO_PASSWORD}" fi if [ -z "${TARGET_REPO_USERNAME}" && -n "${REPO_USERNAME}" ]; then TARGET_REPO_USERNAME="${REPO_USERNAME}" fi if [ -z "${TARGET_REPO_PASSWORD}" && -n "${REPO_PASSWORD}" ]; then BASE_REPO_PASSWORD="${REPO_PASSWORD}" 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}" # if no tag is give default to latest echo "${BASE}" | grep -q ":" || BASE="${BASE}:latest" echo "${TARGET}" | grep -q ":" || TARGET="${TARGET}:latest" # move the 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/:.*//') # build the url to the repo BASE_URL="${BASE_REPO_URL}/${BASE_REPO}/tags/${BASE_TAG}" TARGET_URL="${TARGET_REPO_URL}/${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