diff --git a/entrypoint.sh b/entrypoint.sh index 2bb04a0a00cef24446716fe4d9f26e2b3b6defa9..e788283d173435ea33262edc85ca22d8793fe8e5 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -17,6 +17,9 @@ die() { ########################################################################################################################## +# Set content type +CONTENT_TYPE="Content-Type: application/vnd.docker.distribution.manifest.v2+json" + # Check if env vars were set test -z "${CI_SERVER_HOST}" && should_die "CI_SERVER_HOST is missing" test -z "${CI_REGISTRY}" && should_die "CI_REGISTRY is missing" @@ -25,31 +28,30 @@ test -z "${CI_REGISTRY_PASSWORD}" && should_die "CI_REGISTRY_PASSWORD is missing test -z "${CI_PROJECT_PATH}" && should_die "CI_PROJECT_PATH is missing" test -z "${CI_COMMIT_SHA}" && should_die "CI_COMMIT_SHA is missing" +########################################################################################################################## +test -n "${SHOULD_DIE}" && die "Incomplete config!" +########################################################################################################################## + +# DEBUG +test -z "${RETAG_DEBUG}" || set -x + # Define default custom image name test -z "${CUSTOM_IMAGENAME}" && test -z "${CI_COMMIT_TAG}" && CUSTOM_IMAGENAME="${CI_COMMIT_REF_SLUG}" test -z "${CUSTOM_IMAGENAME}" && IMAGE_PATH="${CI_PROJECT_PATH}" || IMAGE_PATH="${CI_PROJECT_PATH}/${CUSTOM_IMAGENAME}" # Define default image old tag -test -z "${REGISTRY_TAG_OLD}" && test -z "${CI_COMMIT_TAG}" && REGISTRY_TAG_OLD="${CI_COMMIT_SHA}" || REGISTRY_TAG_OLD="${CI_COMMIT_TAG}" +test -z "${REGISTRY_TAG_OLD}" && ( test -z "${CI_COMMIT_TAG}" && REGISTRY_TAG_OLD="${CI_COMMIT_SHA}" || REGISTRY_TAG_OLD="${CI_COMMIT_TAG}" ) # Define default image new tag test -z "${REGISTRY_TAG_NEW}" && REGISTRY_TAG_NEW=latest -########################################################################################################################## -test -n "${SHOULD_DIE}" && die "Incomplete config!" -########################################################################################################################## - -# Set content type -CONTENT_TYPE="Content-Type: application/vnd.docker.distribution.manifest.v2+json" -# Set manifest root url -MANIFEST_URL=${REGISTRY_SCHEMA:-https}://${CI_REGISTRY}/v2/${IMAGE_PATH}/manifests - ########################################################################################################################## TEMPFILE=$(mktemp) ########################################################################################################################## # DEBUG -test -z "${RETAG_DEBUG}" || echo "Old image: ${CI_REGISTRY}/${IMAGE_PATH}:${REGISTRY_TAG_OLD}" +echo "Image: ${CI_REGISTRY}/${IMAGE_PATH}" +echo "Old tag: ${REGISTRY_TAG_OLD}" # Fetch JWT token TOKEN=$(curl -s \ @@ -59,16 +61,17 @@ TOKEN=$(curl -s \ -d scope="repository:${IMAGE_PATH}:push,pull,delete" \ | jq -r '.token') test -n "${TOKEN}" || die "Couldn't get token" -test -z "${RETAG_DEBUG}" || echo "Token: [${TOKEN}]" BEARER="Authorization:Bearer $TOKEN" +# Set manifest root url +MANIFEST_URL=${REGISTRY_SCHEMA:-https}://${CI_REGISTRY}/v2/${IMAGE_PATH}/manifests + # Fetch image manifest MANIFEST=$(curl -s \ -H "${BEARER}" \ -H "${CONTENT_TYPE}" \ -G ${MANIFEST_URL}/${REGISTRY_TAG_OLD}) test -n "${MANIFEST}" || die "Couldn't get manifest" -test -z "${RETAG_DEBUG}" || echo "Manifest: [${MANIFEST}]" # Push image manifest RESPONCE=$(curl -s \ @@ -78,9 +81,8 @@ RESPONCE=$(curl -s \ -X PUT \ -d "$MANIFEST") test -z "${RESPONCE}" || die "Couldn't get responce" -test -z "${RETAG_DEBUG}" || echo "Responce: [${RESPONCE}]" -echo "Image tag: ${CI_REGISTRY}/${IMAGE_PATH}:${REGISTRY_TAG_NEW}" +echo "New tag: ${REGISTRY_TAG_NEW}" ########################################################################################################################## rm -f "${TEMPFILE}"