125 lines
3.2 KiB
Bash
Executable File
125 lines
3.2 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Bring in the variables from SOLUTION file, supporting
|
|
# nested substitution
|
|
. SOLUTION
|
|
|
|
VARS=($(sed -nE "s,(^[^#][^=]*).*$,\1,pg" SOLUTION))
|
|
for var in ${VARS[@]}; do
|
|
export ${var}
|
|
done
|
|
|
|
RELEASE_INFO=${RELEASE_INFO:-N/A}
|
|
if [[ "${RELEASE_INFO}" == "N/A" ]]; then
|
|
TAG=${TAG:-${OS_DISTRO}-${PACKAGE_STREAM}-$(date +%Y%m%d)}
|
|
else
|
|
TAG=${TAG:-${OS_DISTRO}-${PACKAGE_STREAM}-${RELEASE_INFO}}
|
|
fi
|
|
|
|
# The "TARGET_TAG" is intended to not be a unique tag; it should
|
|
# be replaced with each build.
|
|
if [[ "${BUILD}" != "" ]] && [[ "${BUILD}" != "N/A" ]]; then
|
|
TARGET_TAG="${OS_DISTRO}-${OS_RELEASE}-${BRANCH}"
|
|
else
|
|
TARGET_TAG="${OS_DISTRO}-${PACKAGE_STREAM}"
|
|
fi
|
|
|
|
function fail {
|
|
>&2 echo "$*"
|
|
exit -1
|
|
}
|
|
|
|
cat << EOF
|
|
|
|
The following will publish latest rolling image:
|
|
|
|
1. Pull ${CONTAINER}:${TAG}
|
|
2. Tag #1 as ${CONTAINER}:${TARGET_TAG}
|
|
3. Tag #1 as ${CONTAINER}:latest-${TARGET_TAG}
|
|
4. Delete ${CONTAINER}:${TAG}
|
|
5. Push #2
|
|
6. Push #3
|
|
|
|
EOF
|
|
|
|
echo "Pulling ${REGISTRY_URL}/${CONTAINER}:${TAG}"
|
|
docker pull ${REGISTRY_URL}/${CONTAINER}:${TAG} ||
|
|
fail "Unable to pull image"
|
|
|
|
echo "Tagging as ...:${TARGET_TAG}"
|
|
docker tag ${REGISTRY_URL}/${CONTAINER}:${TAG} \
|
|
${REGISTRY_URL}/${CONTAINER}:${TARGET_TAG} ||
|
|
fail "Unable to tag image (1/2)"
|
|
|
|
echo "Tagging as ...:latest-${TARGET_TAG}"
|
|
docker tag ${REGISTRY_URL}/${CONTAINER}:${TAG} \
|
|
${REGISTRY_URL}/${CONTAINER}:latest-${TARGET_TAG} ||
|
|
fail "Unable to tag image (2/2)"
|
|
|
|
# Parse out the FQDN from the REGISTRY_URL and escape
|
|
# the group/project/path
|
|
FQDN=${REGISTRY_URL%%/*}
|
|
PROJECT=$(echo ${REGISTRY_URL#*/}/${CONTAINER} | sed s,/,%2F,g)
|
|
|
|
HEADER="Authorization:Basic $(echo -n "${HARBOR_USER}:${HARBOR_PASSWD}" | base64)"
|
|
|
|
delete_tag() {
|
|
_project=$1
|
|
_repository=$2
|
|
_tag=$3
|
|
_fqdn=${REGISTRY_URL%%/*}
|
|
_repository=${_repository//\//%2F}
|
|
curl --noproxy '*' -s -k \
|
|
-i \
|
|
-X DELETE \
|
|
-H "${HEADER}" \
|
|
-H "accept: application/json" \
|
|
"https://${_fqdn}/api/v2.0/projects/${_project}/repositories/${_repository}/artifacts/${_tag}"
|
|
|
|
# To just delete the tag and not the image, use:
|
|
# "https://${_fqdn}/api/v2.0/projects/${_project}/repositories/${_repository}/artifacts/${_tag}/tags/${_tag}"
|
|
}
|
|
|
|
echo -e "Deleting tag:\n vtt-osgc solutions/${CONTAINER}:${TAG}\nFrom:\n ${PROJECT}"
|
|
|
|
RESULTS=$(delete_tag vtt-osgc solutions/${CONTAINER} ${TAG})
|
|
|
|
if echo ${RESULTS} | grep -q "HTTP.*200"; then
|
|
echo "Tag deleted successfully."
|
|
else
|
|
echo "Error deleting tag:" >&2
|
|
echo "${RESULTS}" >&2
|
|
fi
|
|
|
|
iter=5
|
|
while (( iter )); do
|
|
echo "Pushing as ...:${TARGET_TAG}"
|
|
if docker push ${REGISTRY_URL}/${CONTAINER}:${TARGET_TAG}; then
|
|
break
|
|
fi
|
|
echo "Unable to push ${CONTAINER}:${TARGET_TAG}" >&2
|
|
iter=$((iter-1))
|
|
if (( ! iter )); then
|
|
fail "No more tries."
|
|
fi
|
|
echo "Waiting 10s before trying again..."
|
|
sleep 10
|
|
done
|
|
|
|
iter=5
|
|
while (( iter )); do
|
|
echo "Pushing as ...:latest-${TARGET_TAG}"
|
|
if docker push ${REGISTRY_URL}/${CONTAINER}:latest-${TARGET_TAG}; then
|
|
break
|
|
fi
|
|
echo "Unable to push ${CONTAINER}:latest-${TARGET_TAG}"
|
|
iter=$((iter-1))
|
|
if (( ! iter )); then
|
|
fail "No more tries."
|
|
fi
|
|
echo "Waiting 10s before trying again..."
|
|
sleep 10
|
|
done
|
|
|
|
echo "Done tagging and pushing ${CONTAINER} as ${TARGET_TAG}"
|