uci-docker-build/build

105 lines
3.0 KiB
Bash
Executable File

#!/bin/bash
declare targets=(dev arm amd deploy private multi)
declare OPTION; declare OPTARG; declare OPTIND
while getopts 'b:d:t:nyr:u:px' OPTION; do
# echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND}
case "$OPTION" in
b)
# CUSTOM BASE IMAGE
BASE_IMAGE=$OPTARG
;;
d)
# LINUX_DISTRO=$OPTARG
LINUX_DISTRO=$OPTARG
;;
x)
# Exclude distro from image name
exclude_distro=true
;;
t)
TAG=$OPTARG
;;
u)
RUSER=$OPTARG
;;
y)
try=true
;;
p)
push=true
;;
n)
NO_CACHE=true
;;
r)
REPO=$OPTARG
;;
*) echo unknown run option -$OPTARG
echo "USAGE: build <setup options> target(or target)"
echo "available options -d <LINUX_DISTRO>; -t <TAG>; -e execute test script after build; -n for --no_cache "
;;
esac
done
shift $((OPTIND - 1))
[ $NO_CACHE ] && nocache="--no-cache" || nocache=""
target=${1:-dev}
LINUX_DISTRO=${LINUX_DISTRO:-alpine}
name=$2
RUSER=${3:-$RUSER}
IMAGE_NAME=$([[ $RUSER ]] && echo ${RUSER}/)${name}$([[ (! $exclude_distro) && $name ]] && echo "-")$([[ ! $exclude_distro ]] && echo ${LINUX_DISTRO})
if [[ $BASE_IMAGE ]]; then
echo determining DISTRO of base image: $BASE_IMAGE
LINUX_DISTRO=$(./image-distro $BASE_IMAGE)
[[ ! $LINUX_DISTRO ]] && echo "unable to get base image OS for: $BASE_IMAGE, aborting build" && exit 1
else
BASE_IMAGE=$LINUX_DISTRO
fi
# BASE_IMAGE=$([[ $BASE_IMAGE == *:* ]] && echo $BASE_IMAGE || echo $BASE_IMAGE:latest)
#$([[ ! $BASE_IMAGE == *:* ]] && echo :latest)
export BASE_IMAGE
export TAG
export IMAGE_NAME
export LINUX_DISTRO
echo "building with base image: $BASE_IMAGE, output image name => $IMAGE_NAME"
echo "Linux Distro: $LINUX_DISTRO"
echo "***** using build target: $target ****"
docker buildx bake --print $target
echo "******************"
read -n 1 -p "do you want to continue [y]=>" REPLY
[[ $REPLY != "y" ]] && echo -e "\n" && exit 0
[[ ! "${targets[@]}" =~ $target ]] && echo $target is not a valid target && echo valid targets are: ${targets[@]} && exit 4
builder=default
if [ $target == "deploy" ]; then
builder=deploy
if ! docker buildx ls | grep -q deploy ; then
echo multiarch deploy builder does not exist, creating with docker-container driver
docker buildx create --name deploy --driver docker-container >/dev/null
docker buildx ls | grep deploy
fi
fi
[[ $target == "private" && ! $REPO ]] && echo "must use '-r <private repo>' if building to private repo" && exit 3
docker buildx --builder ${builder} bake ${nocache} ${target} 2>&1 | tee build.log
if [[ $target == "private" ]]; then
./push -a -r $REPO $IMAGE_NAME
./push -r $REPO $IMAGE_NAME
else
if [[ $push && (! $target == "dev") ]];then
echo pushing now
./push $([[ $target == "arm" ]] && echo -a) -r $REPO $IMAGE_NAME
fi
fi
[[ $try || $target == "dev" ]] && ./try $([[ $target == "deploy" ]] && echo -p) $name $RUSER