nextcloud/build
2024-06-02 23:37:22 +00:00

103 lines
3.6 KiB
Bash
Executable File

#!/bin/bash
# use this as: PUSH=0 ./build ... in order to set the variable. default is PUSH=1
PUSH=${PUSH:-1}
FORCE=${FORCE:-0}
if [ ! "$1" == "" ]; then
IMAGE_VERSION=$1
else
echo "No Nextcloud version specified. Exiting (1)."
exit 1
fi
if [ ! "$2" == "" ]; then
VARIANTS=$2
else
VARIANTS=("fpm" "fpm-alpine")
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=nextcloud
UPSTREAM_IMAGE_TAG=${IMAGE_VERSION}-${IMAGE_VARIANT}
IMAGE=bkraul/nextcloud
echo "Building 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.
NEXTCLOUD_VERSION=$(docker inspect ${UPSTREAM_IMAGE}:${UPSTREAM_IMAGE_TAG} | jq -r '.[].Config.Env[] | select(match("^NEXTCLOUD_VERSION"))')
NEXTCLOUD_VERSION=${NEXTCLOUD_VERSION:18}
IMAGE_TAG=${NEXTCLOUD_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 "----------------------------"
echo "FORCE: $FORCE"
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)..."
#--file ${DOCKERFILE} \
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