#!/bin/bash doBuildSystem() { echo "v2023-11-08" } # Compute images informations LOGFILE=cbslog.log BASE=$OWNER/$NAME LATEST=${BASE}:latest # Cleanup log file if [ -f $LOGFILE ]; then rm -rf $LOGFILE touch $LOGFILE fi # Display introduction doIntro() { echo "Docker Image Build: $HOSTNAME" doBuildSystem echo } # Prepare for cross compile doPrepareCrossCompile() { echo "> Prepare for cross compile" export DOCKER_DEFAULT_PLATFORM=$1 export DOCKER_CLI_EXPERIMENTAL=enabled } # Cross-platform emulator collection distributed with Docker images doBinFMT() { echo "> Perform cross-platform setup" docker run --privileged --rm tonistiigi/binfmt:latest --install all } # Add a Tag to image doDockerTag() { TAG=$1 echo "> Add [$TAG] tag to image for registry [$REGISTRY]" docker tag ${BASE} ${REGISTRY}/${BASE}:${TAG} if [ $? -eq 0 ]; then echo " * tag done!" else echo " * tag failed!" fi } # Push to registry doRegistryPush() { echo "> Push [$BASE] image to [$REGISTRY]" docker image push --all-tags ${REGISTRY}/${BASE} } # Build Docker image doBuild() { PLATFORM="${1:=linux/amd64}" IMAGE=$2 CACHING=$3 echo "> Build image: [$IMAGE] for [$PLATFORM] ($CACHING)" docker buildx build \ $CACHING \ --network host \ --compress \ --output=type=docker \ --pull \ --build-arg CONCURRENCY=$(nproc) \ --platform=${PLATFORM} \ -t "${IMAGE}" \ . 2>&1 } doBuildCache() { doBuild $1 $2 "" } doBuildNoCache() { doBuild $1 $2 "--no-cache" } # Perform 'failed' action doFailed() { echo "* failed!" exit 1 } # Perform 'done' action doDone() { echo "* done." } if [ "$(id -u)" != "0" ]; then doIntro echo "This script must be run as root" 1>&2 echo exit 1 fi clear cd $CWD || exit 1 # Show introduction doIntro doPerformAll() { # Perform cross-compile tasks doBinFMT if [ $? -ne 0 ]; then doFailed else doDone fi # Perform image build if [ $CACHE == true ]; then doBuildCache "${OS}" "$LATEST" else doBuildNoCache "${OS}" "$LATEST" fi if [ $? -ne 0 ]; then doFailed else doDone fi # Perform image tagging doDockerTag "latest" if [ $? -ne 0 ]; then doFailed else doDone fi # Perform push ro registry doRegistryPush if [ $? -ne 0 ]; then doFailed else doDone fi } doPerformAll | tee $LOGFILE sync echo "> Finished!" exit 0