1
0
Fork 0
cautious-journey/scripts/docker-build.sh

48 lines
1.4 KiB
Bash
Executable File

#! /bin/bash
set -euxo pipefail
cd ${IMAGE_ROOT}
export CLEAN_TAG="$(echo "${CI_COMMIT_TAG:-${CI_COMMIT_REF_SLUG}}" | sed -r 's/[^-_a-zA-Z0-9\\.]/-/g')"
export IMAGE_TAG="${IMAGE_NAME}:${IMAGE_VERSION}-${CLEAN_TAG}"
# pull the previous image from the same branch or master to leverage its layers
docker pull docker.artifacts.apextoaster.com/${IMAGE_TAG} || \
docker pull docker.artifacts.apextoaster.com/"${IMAGE_NAME}:${IMAGE_VERSION}-master" || \
true # no existing image is not an error
# build and test a new image
docker build -t ${IMAGE_TAG} .
# prepare to push
function push_image() {
local PUSH_TAG="${1:-${IMAGE_TAG}}"
docker push ${PUSH_TAG} || true
docker tag ${PUSH_TAG} docker-push.artifacts.apextoaster.com/${PUSH_TAG}
docker push docker-push.artifacts.apextoaster.com/${PUSH_TAG}
if [[ "${CI_COMMIT_REF_SLUG}" == "master" ]];
then
docker tag ${PUSH_TAG} ${IMAGE_NAME}:${IMAGE_VERSION}
docker push ${IMAGE_NAME}:${IMAGE_VERSION}
docker tag ${IMAGE_NAME}:${IMAGE_VERSION} docker-push.artifacts.apextoaster.com/${IMAGE_NAME}:${IMAGE_VERSION}
docker push docker-push.artifacts.apextoaster.com/${IMAGE_NAME}:${IMAGE_VERSION}
fi
}
# push under the normal tag
push_image "${IMAGE_TAG}"
# push under each alt tag
for alt_name in "${IMAGE_ALT:-}";
do
[[ -z "${alt_name}" ]] && continue
ALT_TAG="${alt_name}:${IMAGE_VERSION}-${CI_COMMIT_REF_SLUG}"
docker tag ${IMAGE_TAG} ${ALT_TAG}
push_image "${ALT_TAG}"
done