2022-10-20 18:48:17 -07:00
|
|
|
#!/bin/bash
|
2023-01-17 15:20:30 -08:00
|
|
|
|
2023-04-14 21:27:40 -07:00
|
|
|
udbuild () {
|
2023-01-17 15:20:30 -08:00
|
|
|
|
2023-04-14 21:27:40 -07:00
|
|
|
local targets=(dev arm64 amd64 publish multi default)
|
|
|
|
local log_dir; local no_prompt
|
|
|
|
local append_efile
|
2022-10-20 18:48:17 -07:00
|
|
|
declare OPTION; declare OPTARG; declare OPTIND
|
2022-10-22 08:23:16 -07:00
|
|
|
|
2023-01-17 15:20:30 -08:00
|
|
|
BDIR=$(dirname "$(realpath "$BASH_SOURCE")")
|
2023-01-22 15:02:36 -08:00
|
|
|
export BDIR
|
2023-03-25 11:27:22 -07:00
|
|
|
# load script library
|
2023-01-17 15:20:30 -08:00
|
|
|
source $BDIR/lib/load.sh
|
2023-04-14 21:27:40 -07:00
|
|
|
BUILD_EFILE=""
|
2023-01-17 11:53:24 -08:00
|
|
|
|
2023-03-25 11:27:22 -07:00
|
|
|
# check for subcommands first
|
2023-01-17 15:20:30 -08:00
|
|
|
case "$1" in
|
2023-03-22 09:10:54 -07:00
|
|
|
try)
|
2023-04-14 21:27:40 -07:00
|
|
|
shift 1; try_container "$@"; return $? ;;
|
|
|
|
load_env_file)
|
|
|
|
echo -e "@@@@@@ loading build environment file for external use @@@@@@"
|
|
|
|
BUILD_EFILE=$(echo -- "$@" | grep -oP -- '(?<=-e )[^ ]*')
|
|
|
|
source_env_file "$BUILD_EFILE"
|
|
|
|
echo -e "@@@@@@@@@@@@@@@@@ returning to calling script @@@@@@@@@@@@@@@"
|
2023-01-17 15:20:30 -08:00
|
|
|
return $?
|
|
|
|
;;
|
2023-04-14 21:27:40 -07:00
|
|
|
build_src) shift 1; get_build_src "$@"; return $? ;;
|
|
|
|
help)
|
|
|
|
;&
|
|
|
|
--help)
|
|
|
|
;&
|
|
|
|
-help) shift 1; usage "$@"; return $? ;;
|
|
|
|
source) type udbuild; return $? ;;
|
|
|
|
image)
|
2023-01-17 15:20:30 -08:00
|
|
|
shift 1
|
2023-04-14 21:27:40 -07:00
|
|
|
case "$1" in
|
|
|
|
name) shift 1; image_name "$@" ;;
|
|
|
|
tag) shift 1; image_tag "$@" ;;
|
|
|
|
push) shift 1; image_push "$@" ;;
|
|
|
|
delete) shift 1; image_delete "$@" ;;
|
|
|
|
info)
|
|
|
|
shift 1
|
|
|
|
case "$1" in
|
|
|
|
arch) shift 1; image_arch "$@" ;;
|
|
|
|
exists) shift 1; image_exists "$@" ;;
|
|
|
|
tags) shift 1; image_tags "$@" ;;
|
|
|
|
id) shift 1; image_id "$@" ;;
|
|
|
|
* ) image_info "$@"
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
*) echo no image subcommand $1 ;;
|
|
|
|
esac
|
|
|
|
return $?
|
2023-01-17 15:20:30 -08:00
|
|
|
;;
|
|
|
|
esac
|
2022-10-22 08:23:16 -07:00
|
|
|
|
|
|
|
exit_abnormal() { # Function: Exit with error.
|
|
|
|
usage
|
2023-01-17 15:20:30 -08:00
|
|
|
return ${1:-1}
|
2022-10-22 08:23:16 -07:00
|
|
|
}
|
|
|
|
|
2023-01-17 15:20:30 -08:00
|
|
|
[[ -z "$PS1" ]] || no_prompt=true
|
2023-01-22 15:02:36 -08:00
|
|
|
overwrite=true
|
2022-10-22 08:23:16 -07:00
|
|
|
|
2023-04-14 21:27:40 -07:00
|
|
|
while getopts 'fg:e:b:d:t:nc:r:u:lhs:a:voi:p' OPTION; do
|
2022-10-22 08:23:16 -07:00
|
|
|
# echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND}
|
|
|
|
case "$OPTION" in
|
2023-04-14 21:27:40 -07:00
|
|
|
i)
|
|
|
|
IMAGE_INFO=$OPTARG
|
|
|
|
;;
|
2023-03-27 11:07:25 -07:00
|
|
|
e)
|
2023-04-14 21:27:40 -07:00
|
|
|
BUILD_EFILE=$OPTARG
|
|
|
|
if ! source_env_file $BUILD_EFILE; then return 2; fi
|
2023-03-27 11:07:25 -07:00
|
|
|
;;
|
2023-01-17 15:20:30 -08:00
|
|
|
o)
|
2023-01-22 15:02:36 -08:00
|
|
|
unset overwrite
|
2023-01-17 15:20:30 -08:00
|
|
|
;;
|
|
|
|
v)
|
2023-04-14 21:27:40 -07:00
|
|
|
VERBOSE=true
|
2023-01-17 15:20:30 -08:00
|
|
|
;;
|
2023-01-17 11:53:24 -08:00
|
|
|
a)
|
2023-04-14 21:27:40 -07:00
|
|
|
append_efile=$OPTARG
|
|
|
|
;;
|
|
|
|
f)
|
|
|
|
REBUILD=init
|
|
|
|
;;
|
|
|
|
p)
|
|
|
|
echo "build script will be run WITHOUT user prompts (i.e. non-interactive)"
|
2023-01-17 11:53:24 -08:00
|
|
|
no_prompt=true
|
|
|
|
;;
|
2022-10-22 08:23:16 -07:00
|
|
|
b)
|
|
|
|
# CUSTOM BASE IMAGE
|
|
|
|
BASE_IMAGE=$OPTARG
|
2022-10-20 18:48:17 -07:00
|
|
|
;;
|
2023-01-17 11:53:24 -08:00
|
|
|
s)
|
2023-03-25 11:27:22 -07:00
|
|
|
# building source from which to bind into build, default is src/ in current directory
|
|
|
|
BUILD_SRC=$OPTARG
|
2023-01-17 11:53:24 -08:00
|
|
|
;;
|
2022-10-22 08:23:16 -07:00
|
|
|
d)
|
2023-01-17 11:53:24 -08:00
|
|
|
# LINUX_DISTRO=$OPTARG
|
2022-10-22 08:23:16 -07:00
|
|
|
LINUX_DISTRO=$OPTARG
|
2022-10-20 18:48:17 -07:00
|
|
|
;;
|
2023-03-25 11:27:22 -07:00
|
|
|
l)
|
|
|
|
# append distro name to image name
|
2023-04-14 21:27:40 -07:00
|
|
|
APPEND_DISTRO=true
|
2022-10-20 18:48:17 -07:00
|
|
|
;;
|
2022-10-22 08:23:16 -07:00
|
|
|
t)
|
2023-03-27 11:07:25 -07:00
|
|
|
TARGET=$OPTARG
|
2023-01-22 15:02:36 -08:00
|
|
|
;;
|
|
|
|
g)
|
2022-10-22 08:23:16 -07:00
|
|
|
TAG=$OPTARG
|
|
|
|
;;
|
|
|
|
u)
|
|
|
|
RUSER=$OPTARG
|
|
|
|
;;
|
|
|
|
c)
|
2023-04-14 21:27:40 -07:00
|
|
|
TRY_CMD=$OPTARG
|
2023-04-01 07:17:43 -07:00
|
|
|
;;
|
2022-10-22 08:23:16 -07:00
|
|
|
n)
|
|
|
|
nocache="--no-cache"
|
|
|
|
;;
|
|
|
|
r)
|
|
|
|
REPO=$OPTARG
|
|
|
|
;;
|
|
|
|
h)
|
|
|
|
exit_abnormal 0
|
2023-01-17 15:42:56 -08:00
|
|
|
return 0
|
2022-10-22 08:23:16 -07:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "unknown $0 option -$OPTARG"
|
2023-03-27 11:07:25 -07:00
|
|
|
exit_abnormal 1
|
2022-10-22 08:23:16 -07:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
2022-10-20 18:48:17 -07:00
|
|
|
|
2022-10-22 08:23:16 -07:00
|
|
|
shift $((OPTIND - 1))
|
2022-10-20 18:48:17 -07:00
|
|
|
|
2023-04-14 21:27:40 -07:00
|
|
|
[[ ! $BUILD_EFILE ]] && source_env_file
|
|
|
|
|
|
|
|
if ! get_build_src; then
|
|
|
|
if [[ $no_prompt ]] ; then
|
|
|
|
echo aborting the build...
|
|
|
|
echo -e "\e[1;31mNOTE: use '_default_' to explicitly use build source in uci-docker-build repo\e[1;37m"
|
|
|
|
return 2
|
|
|
|
else
|
|
|
|
echo "Do you want to use the uci-docker-build default build source"
|
|
|
|
echo "at $BDIR/src "
|
|
|
|
read -n 1 -p "instead? [y]=>" REPLY
|
|
|
|
[[ $REPLY != "y" ]] && echo -e "\n" && return 2
|
|
|
|
BUILD_SRC=$BDIR/src
|
|
|
|
echo -e "\n\e[1;31mNOTE: use '_default_' to explicitly use build source in uci-docker-build repo\e[1;37m"
|
2023-03-27 11:07:25 -07:00
|
|
|
fi
|
2023-04-14 21:27:40 -07:00
|
|
|
fi
|
2023-03-25 11:27:22 -07:00
|
|
|
|
2023-04-14 21:27:40 -07:00
|
|
|
TARGET=${TARGET:-default}
|
2023-03-27 11:07:25 -07:00
|
|
|
[[ ! "${targets[@]}" =~ $TARGET ]] && echo $TARGET is not a valid target && echo valid targets are: ${targets[@]} && exit 4
|
2023-01-22 15:02:36 -08:00
|
|
|
|
2023-04-14 21:27:40 -07:00
|
|
|
get_distro
|
2023-03-22 09:10:54 -07:00
|
|
|
|
2023-04-03 14:14:17 -07:00
|
|
|
IMAGE_NAME=$(make_image_name $@)
|
2023-03-27 11:07:25 -07:00
|
|
|
|
2023-01-17 15:20:30 -08:00
|
|
|
# TODO writing to existing tag untags existing image so write a new tag to that image then continue
|
2023-01-21 22:41:14 -08:00
|
|
|
# retag existing image and remove former tag
|
|
|
|
if [[ $(image_exists $IMAGE_NAME) ]]; then
|
|
|
|
if [[ $overwrite ]]; then
|
|
|
|
image_delete $IMAGE_NAME
|
|
|
|
else
|
|
|
|
newtag=$(date +'%d%H%M%S')
|
2023-01-22 15:02:36 -08:00
|
|
|
echo image exists retaging $(image_name $IMAGE_NAME) with tag :$newtag
|
2023-01-21 22:41:14 -08:00
|
|
|
image_tag $IMAGE_NAME $IMAGE_NAME:$newtag
|
|
|
|
image_tag -r $IMAGE_NAME
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-04-01 07:17:43 -07:00
|
|
|
ARCH=$(get_arch)
|
2023-04-14 21:27:40 -07:00
|
|
|
log_dir=$PWD/logs
|
|
|
|
mkdir -p $log_dir
|
|
|
|
[[ $TARGET == "dev" ]] && VERBOSE=true
|
2023-04-01 07:17:43 -07:00
|
|
|
|
2022-10-20 18:48:17 -07:00
|
|
|
export BASE_IMAGE
|
|
|
|
export TAG
|
|
|
|
export IMAGE_NAME
|
|
|
|
export LINUX_DISTRO
|
2023-04-03 14:14:17 -07:00
|
|
|
export BUILD_SRC
|
2023-04-01 07:17:43 -07:00
|
|
|
export ARCH
|
2023-04-14 21:27:40 -07:00
|
|
|
export VERBOSE
|
2023-03-25 11:27:22 -07:00
|
|
|
|
2023-04-14 21:27:40 -07:00
|
|
|
build_info
|
2023-01-17 15:20:30 -08:00
|
|
|
|
2023-01-17 11:53:24 -08:00
|
|
|
if [[ ! $no_prompt ]]; then
|
|
|
|
read -n 1 -p "do you want to continue [y]=>" REPLY
|
2023-03-27 11:07:25 -07:00
|
|
|
[[ $REPLY != "y" ]] && echo -e "\n" && return 4
|
2023-01-17 11:53:24 -08:00
|
|
|
fi
|
2022-10-20 18:48:17 -07:00
|
|
|
|
2023-04-14 21:27:40 -07:00
|
|
|
if ! source $BDIR/Dockerfile.d/create; then
|
|
|
|
echo unable to create Dockerfile from template, aborting build
|
|
|
|
return 3
|
|
|
|
fi
|
|
|
|
|
2022-10-20 18:48:17 -07:00
|
|
|
builder=default
|
2023-04-14 21:27:40 -07:00
|
|
|
if [[ $TARGET == "publish" ]]; then
|
|
|
|
builder=publish
|
|
|
|
pushd "$BDIR" > /dev/null || return 3
|
|
|
|
if ! docker buildx ls | grep -q publish ; then
|
|
|
|
echo publish builder does not exist, creating with docker-container driver
|
|
|
|
docker buildx create --name publish --driver docker-container >/dev/null
|
|
|
|
docker buildx ls | grep publish
|
2022-10-20 18:48:17 -07:00
|
|
|
fi
|
2023-04-14 21:27:40 -07:00
|
|
|
popd > /dev/null || return 4
|
2022-10-20 18:48:17 -07:00
|
|
|
fi
|
|
|
|
|
2023-04-14 21:27:40 -07:00
|
|
|
# copy or bind build source directory to temporary .src/ subdirectory in build repo
|
|
|
|
[[ -d $BDIR/.src ]] && rm -rf $BDIR/.src
|
|
|
|
if [[ $(which rsync 2> /dev/null ) ]]; then
|
2023-03-25 11:27:22 -07:00
|
|
|
rsync -aAru ${BUILD_SRC:-src}/ $BDIR/.src
|
2023-04-14 21:27:40 -07:00
|
|
|
else
|
|
|
|
echo no rsync copying with cp
|
|
|
|
/bin/cp -a ${BUILD_SRC:-src}/. $BDIR/.src > /dev/null 2>&1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -f $append_efile ]]; then
|
|
|
|
/bin/cp "$append_efile" "$BDIR/.src/init/env/_build.env_"
|
|
|
|
echo 'source $ENV_DIR/_build.env_' >> $BDIR/.src/init/build.env
|
|
|
|
fi
|
|
|
|
|
|
|
|
pushd "$BDIR" > /dev/null || return 3
|
|
|
|
|
|
|
|
######### RUNNING THE DOCKER BUILD COMMAND ######################
|
2023-04-01 07:17:43 -07:00
|
|
|
echo running build command: docker buildx --builder ${builder} bake ${nocache} ${TARGET}
|
2023-03-27 11:07:25 -07:00
|
|
|
docker buildx --builder ${builder} bake ${nocache} ${TARGET} 2>&1 | tee "$log_dir/${IMAGE_NAME//\//-}build.log"
|
|
|
|
[[ $? == 0 ]] && echo succcess building image $IMAGE_NAME || exit_abnormal 5
|
2023-04-14 21:27:40 -07:00
|
|
|
popd > /dev/null || return 4
|
2023-01-17 11:53:24 -08:00
|
|
|
|
2023-03-22 09:10:54 -07:00
|
|
|
rm -rf $BDIR/.src
|
2023-01-17 11:53:24 -08:00
|
|
|
|
2023-04-14 21:27:40 -07:00
|
|
|
if [[ ($TRY_CMD || $TARGET == "dev") ]]; then
|
2023-01-17 15:20:30 -08:00
|
|
|
echo trying newly built image in a container
|
2023-04-14 21:27:40 -07:00
|
|
|
echo name before try $IMAGE_NAME
|
|
|
|
try_container build -m opt $([[ $TARGET == "publish" ]] && echo -p) ${TRY_CMD:-shell}
|
2023-01-17 15:20:30 -08:00
|
|
|
fi
|
2023-01-17 11:53:24 -08:00
|
|
|
|
2023-04-01 07:17:43 -07:00
|
|
|
if [[ $TARGET == "private" ]]; then
|
2023-04-14 21:27:40 -07:00
|
|
|
echo pushing arm64 image $IMAGE_NAME to ${REPO:-docker hub}
|
2023-04-01 07:17:43 -07:00
|
|
|
image_push -a -r $REPO $IMAGE_NAME
|
2023-04-14 21:27:40 -07:00
|
|
|
echo pushing amd image $IMAGE_NAME to ${REPO:-docker hub}
|
2023-04-01 07:17:43 -07:00
|
|
|
image_push -r $REPO $IMAGE_NAME
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
2023-01-17 15:20:30 -08:00
|
|
|
|
|
|
|
# if script was executed then call the function
|
2023-04-14 21:27:40 -07:00
|
|
|
(return 0 2>/dev/null) || udbuild "$@"
|
2023-03-27 11:07:25 -07:00
|
|
|
|
|
|
|
|
|
|
|
|