diff --git a/.gitignore b/.gitignore index 69c936c..054a8bb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ /archive/ -/images/ /build.log +_opt/ diff --git a/Dockerfile b/Dockerfile index 544bc7f..fc95c2b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,15 +1,20 @@ ARG BASE_IMAGE FROM $BASE_IMAGE +ARG BASE_IMAGE +ARG KEEP ARG LINUX_DISTRO=alpine -WORKDIR /opt/$LINUX_DISTRO -COPY ./distros/$LINUX_DISTRO ./distros/common ./ +ARG SCRIPTS=/build +WORKDIR $SCRIPTS +COPY .src ./ -RUN echo "****** Building UCI Image ffrom Base $BASE_IMAGE; Distro: $LINUX_DISTRO *****"; \ - chmod -R +x /opt/$LINUX_DISTRO; ls -la; pwd; \ - echo " **** Running ${LINUX_DISTRO} specific init script ***** "; cat init.sh; ./init.sh; \ - echo " ***** Running common initialzation script *****"; cat common.sh; ./common.sh \ - echo " ********************************************************" +RUN echo -e "\n ************************************************* \n"\ + echo "****** Building Image from Base: $BASE_IMAGE; : Distro: $LINUX_DISTRO; *****"; \ + chmod -R +x .; \ + pwd; ls -la; \ + echo " ---- running init script ---"; ./init.sh; \ + echo -e "\n********************************************************" +VOLUME [ "/data", "/opt", "/shell" ] +WORKDIR /opt # ENTRYPOINT ["entrypoint.sh"] CMD ["/bin/bash"] - diff --git a/build b/build index 95e7635..9fddc33 100755 --- a/build +++ b/build @@ -2,6 +2,15 @@ declare targets=(dev arm amd deploy private multi) declare OPTION; declare OPTARG; declare OPTIND +SDIR=$(pwd) +BDIR=$(dirname "$(realpath "$0")") +pushd $BDIR > /dev/null + +if [ $1 == "try" ]; then + ./try $2 $SDIR + exit +fi + usage() { # Function: Print a help message. echo "Image Build Script: Creates one or more images using a target in the docker-bake.hcl file" echo "USAGE: $0 buildtarget " @@ -12,10 +21,12 @@ usage() { # Function: Print a help message. echo "-n for --no_cache" echo "-p push to repository; after build push to repository default is hub.docker.common (not need for deploy target)" echo required argument options: + echo "-d supported: alpine, debian, ubuntu, default: alpine; if base image set distro will be determined" echo "-t tag following : in output image name (i.e. REPO/USER/NAME:TAG), default: latest" echo "-u ; repository user prefix in output image name (i.e. REPO/USER/NAME:TAG)" echo "-r ; private repo name, do not use for hub.docker.com (docker.io)" echo "-b ; used in FROM in Dockerfile, default is official distro image (e.g. alpine:latest)" + echo "-w ; set a custom WORKDIR in Dockerfile, default is /opt/build" } exit_abnormal() { # Function: Exit with error. @@ -23,16 +34,41 @@ exit_abnormal() { # Function: Exit with error. exit ${1:-1} } +source $BDIR/helpers.lib -while getopts ':b:d:t:ncr:u:pxh' OPTION; do +scripts_dir=$SDIR/src + +while getopts ':b:d:t:ncr:u:pxhs:w:ak' OPTION; do # echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND} case "$OPTION" in + a) + # automated - script is to be run without prompt (non-interactive) + no_prompt=true + ;; + k) + # keep the build scripts in the image, by default they are removed + KEEP=true + ;; + w) + # the work directory to put the build scripts in container (default is /opt/build) + SCRIPTS=$OPTARG + ;; b) # CUSTOM BASE IMAGE BASE_IMAGE=$OPTARG ;; + s) + # scripts subdirectory from which to bind to scripts/, default is src/ + if isAbsPath $OPTARG; then + scripts_dir=$OPTARG + elif [ "$OPTARG" == "_base_" ]; then + scripts_dir=$BDIR/src + else + scripts_dir=$SDIR/$OPTARG + fi + ;; d) - # LINUX_DISTRO=$OPTARG + # LINUX_DISTRO=$OPTARG LINUX_DISTRO=$OPTARG ;; x) @@ -69,15 +105,18 @@ done shift $((OPTIND - 1)) + + 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=$(get_distro -d $BASE_IMAGE) [[ ! $LINUX_DISTRO ]] && echo "unable to get base image OS for: $BASE_IMAGE, aborting build" && exit 1 else BASE_IMAGE=$LINUX_DISTRO @@ -90,14 +129,25 @@ export BASE_IMAGE export TAG export IMAGE_NAME export LINUX_DISTRO +export SCRIPTS +export KEEP -echo "building with base image: $BASE_IMAGE, output image name => $IMAGE_NAME" +echo " ******************************************" +echo "Building with base image: $BASE_IMAGE, output image name => $IMAGE_NAME" echo "Linux Distro: $LINUX_DISTRO" -echo "***** using build target: $target ****" +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 +echo -e "\n---------------------------------" +echo "build scripts at $scripts_dir to be copied to ${SCRIPTS:-/opt/build} in container ***** " +ls -la $scripts_dir +echo -e "\n----- base init script init.sh ------" +cat $scripts_dir/init.sh +echo -e "\n---------------------------------" +echo -e "\n***************************************" +if [[ ! $no_prompt ]]; then + read -n 1 -p "do you want to continue [y]=>" REPLY + [[ $REPLY != "y" ]] && echo -e "\n" && exit 0 +fi [[ ! "${targets[@]}" =~ $target ]] && echo $target is not a valid target && echo valid targets are: ${targets[@]} && exit 4 @@ -113,7 +163,18 @@ fi [[ $target == "private" && ! $REPO ]] && echo "must use '-r ' if building to private repo" && exit 3 -docker buildx --builder ${builder} bake ${nocache} ${target} 2>&1 | tee build.log +# mount source directory to temporary .src/ subdirectory + +fusermount -u .src > /dev/null 2>&1 +mkdir .src +bindfs ${scripts_dir:-src} .src + +docker buildx --builder ${builder} bake ${nocache} ${target} 2>&1 | tee build.log + +fusermount -u .src > /dev/null 2>&1 +rm -rf .src/ + +[[ $? == 0 ]] && echo succcess building image $IMAGE_NAME || exit_abnormal 1 if [[ $target == "private" ]]; then ./push -a -r $REPO $IMAGE_NAME @@ -125,4 +186,7 @@ if [[ $target == "private" ]]; then fi fi -[[ $try || $target == "dev" ]] && ./try $([[ $target == "deploy" ]] && echo -p) $name $RUSER \ No newline at end of file +[[ ($try || $target == "dev") ]] && ./try $([[ $target == "deploy" ]] && echo -p) $IMAGE_NAME + +popd > /dev/null +#echo reset to calling directory $PWD \ No newline at end of file diff --git a/docker-bake.hcl b/docker-bake.hcl index 27ce6f6..a7b6734 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -5,12 +5,18 @@ variable "TAG" { variable "LINUX_DISTRO" { default = "alpine" } +variable "SCRIPTS" { + default = "" +} variable "IMAGE_NAME" { default = "alpine" } variable "BASE_IMAGE" { default = "alpine" } +variable "KEEP" { + default = "" +} function "tag" { params = [suffix] @@ -42,6 +48,8 @@ target "amd" { LINUX_DISTRO = "${LINUX_DISTRO}" BASE_IMAGE = "${BASE_IMAGE}" TAG = "${TAG}" + SCRIPTS = "/build/${SCRIPTS}" + KEEP = "${KEEP}" } tags = tag("") platforms = ["linux/amd64"] diff --git a/readme.md b/readme.md index ad72481..c641153 100644 --- a/readme.md +++ b/readme.md @@ -14,7 +14,7 @@ By using the -b flag you can set an alternative FROM image. In this way you can The repo also supports (with scripts) pushing to alternate private repositories (like self hosted gitea) -starting a new branch is a good way to work on a new image by editing the script files in the distros folder +starting a new branch is a good way to work on a new image by editing the script files in the src folder