2024-06-03 12:27:43 +00:00
#!/bin/bash
2018-10-12 11:54:04 +00:00
2024-06-03 12:27:43 +00:00
# use this as: PUSH=0 ./build ... in order to set the variable. default is PUSH=1
PUSH=${PUSH:-1}
# use this as: FORCE=1 ./build ... in order to set the variable. default is FORCE=0
FORCE=${FORCE:-0}
if [ ! "$1" == "" ]; then
IMAGE_VERSION=$1
else
echo "No nginx version specified."
echo "Avaiilable versions: 1.26, 1.27, mainline, stable"
echo "Exiting (1)."
exit 1
fi
if [ ! "$2" == "" ]; then
VARIANTS=$2
else
VARIANTS=("alpine" "alpine-slim" "bookworm")
fi
# set username and password
# requires vars DOCKER_USER and DOCKER_PASS to be defined before calling.
# useful functions.
function docker_tag_exists() {
TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${DOCKER_USER}'", "password": "'${DOCKER_PASS}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token)
curl --silent -f --head -lL https://hub.docker.com/v2/repositories/$1/tags/$2/ > /dev/null
}
for item in "${!VARIANTS[@]}"
do
IMAGE_VARIANT=${VARIANTS[$item]}
UPSTREAM_IMAGE=nginx
UPSTREAM_IMAGE_TAG=${IMAGE_VERSION}-${IMAGE_VARIANT}
IMAGE=bkraul/nginx-nextcloud
echo "Building nginx-nextcloud ${IMAGE_VERSION}, variant: ${IMAGE_VARIANT}..."
# pull the parent image from docker hub.
echo "Pulling upstream image ${UPSTREAM_IMAGE}:${UPSTREAM_IMAGE_TAG}..."
docker pull ${UPSTREAM_IMAGE}:${UPSTREAM_IMAGE_TAG} > /dev/null
UPSTREAM_ID=$(docker image inspect --format='{{index .Id}}' ${UPSTREAM_IMAGE}:${UPSTREAM_IMAGE_TAG})
UPSTREAM_ID=${UPSTREAM_ID:7}
# get the version number.
NGINX_VERSION=$(docker inspect ${UPSTREAM_IMAGE}:${UPSTREAM_IMAGE_TAG} | jq -r '.[].Config.Env[] | select(match("^NGINX_VERSION"))')
echo "Returned nginx version: ${NGINX_VERSION}"
NGINX_VERSION=${NGINX_VERSION:14}
IMAGE_TAG=${NGINX_VERSION}-${IMAGE_VARIANT}
if docker_tag_exists ${IMAGE} ${IMAGE_TAG}; then
# nothing to do, the image already exists.
echo Image ${IMAGE}:${IMAGE_TAG} already exists.
echo "----------------------------"
if [ $FORCE == 0 ]; then continue; fi
fi
# determine the common files to use.
# Dockerfile
DOCKERFILE_DIR="./build-common/dockerfile"
DOCKERFILE=""
if [ -f "${DOCKERFILE_DIR}/Dockerfile" ]; then DOCKERFILE="${DOCKERFILE_DIR}/Dockerfile"; fi
if [ -f "${DOCKERFILE_DIR}/Dockerfile-${IMAGE_VERSION}" ]; then DOCKERFILE="${DOCKERFILE_DIR}/Dockerfile-${IMAGE_VERSION}"; fi
if [ -f "${DOCKERFILE_DIR}/Dockerfile-${IMAGE_VARIANT}" ]; then echo "Image variant"; DOCKERFILE="${DOCKERFILE_DIR}/Dockerfile-${IMAGE_VARIANT}"; fi
if [ -f "${DOCKERFILE_DIR}/Dockerfile-${IMAGE_VARIANT}-${IMAGE_VERSION}" ]; then DOCKERFILE="${DOCKERFILE_DIR}/Dockerfile-${IMAGE_VARIANT}-${IMAGE_VERSION}"; fi
# if no valid Dockerfile is found, we abort current build and go to next variant if available.
if [ "${DOCKERFILE}" = "" ]; then echo "No valid Dockerfile found."; continue; fi
# image doesn't exist we need build and push
echo Image ${IMAGE}:${IMAGE_TAG} does not exist.
echo "Building image(s)..."
docker build \
--file ${DOCKERFILE} \
--no-cache \
-t ${IMAGE}:${UPSTREAM_IMAGE_TAG} \
-t ${IMAGE}:${IMAGE_TAG} \
--build-arg BASE_IMAGE="${UPSTREAM_IMAGE}:${UPSTREAM_IMAGE_TAG}" \
--progress plain \
./build-common
if [ $? == 0 ]; then
# don't push if specified so (continue the variant loop).
if [ ${PUSH} == 0 ]; then
echo "----------------------------"
continue;
fi
echo "Pushing image(s)..."
docker push ${IMAGE}:${IMAGE_TAG}
docker push ${IMAGE}:${UPSTREAM_IMAGE_TAG}
else
echo "The build operation failed."
echo "Please debug and try again."
fi
echo "----------------------------"
done