diff --git a/Dockerfile b/Dockerfile index 375e348..a4bd315 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,12 +10,14 @@ WORKDIR $BUILD_DIR COPY .src/packages ./packages # PACKAGES -RUN \ - echo -e "\n ************************************************* \n"\ - echo "****** Building Image from Base: $BASE_IMAGE; : Distro: $LINUX_DISTRO; *****"; \ - echo " ---- running packages install script ---"; \ - cd packages; /bin/sh ./packages.sh; cd ..; \ - echo -e "\n********************************************************" +RUN < $IMAGE_NAME<-arch>:${TAG:-latest}" +[[ $push || $TARGET == "private" ]] && echo "Will push image to ${REPO:-hub.docker.com}" +[[ $TARGET == "deploy" ]] && echo "Will build and push both amd64 and arm64 images to hub.docker.com" +[[ $TARGET == "dev" || ! $TARGET ]] && echo "Building image for local machine with architecture $ARCH" echo "Linux Distro: $LINUX_DISTRO" -echo "Using build target: $TARGET" +echo "Using build target: ${TARGET:-default}" echo "Build Command: docker buildx --builder ${builder} bake ${nocache} ${TARGET}" if [[ $verbose ]]; then echo -e "\n---------------------------------" @@ -261,7 +266,7 @@ if [[ ! $no_prompt ]]; then fi builder=default -if [ $TARGET == "deploy" ]; then +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 @@ -277,7 +282,7 @@ fi rm -rf $BDIR/.src rsync -aAru ${BUILD_SRC:-src}/ $BDIR/.src ls -la $BDIR/.src - +echo running build command: docker buildx --builder ${builder} bake ${nocache} ${TARGET} 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 @@ -290,19 +295,22 @@ if [[ ($try || $TARGET == "dev") ]] && [[ ! $no_prompt ]]; then try_container -m opt $([[ $TARGET == "deploy" ]] && echo -p) $IMAGE_NAME fi -} +if [[ $TARGET == "private" ]]; then + # echo pushing arm64 image $IMAGE_NAME to ${REPO:-docker hub} + image_push -a -r $REPO $IMAGE_NAME + # echo pushing amd image $IMAGE_NAME to ${REPO:-docker hub} + image_push -r $REPO $IMAGE_NAME + else + if [[ $push && (! $TARGET == "dev") ]];then + # echo pushing $IMAGE_NAME to ${REPO:-docker hub} + image_push $([[ $TARGET == "arm" ]] && echo -a) -r $REPO $IMAGE_NAME + fi +fi + +} # if script was executed then call the function (return 0 2>/dev/null) || docker_image_build $@ -# 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 \ No newline at end of file diff --git a/docker-bake.hcl b/docker-bake.hcl index 3e5c8f1..c53bba2 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -20,6 +20,9 @@ variable "KEEP" { variable "SYSADMIN_PW" { default = "" } +variable "ARCH" { + default = "" +} function "tag" { params = [suffix] @@ -27,24 +30,24 @@ function "tag" { } # groups group "dev" { - targets = ["amd"] + targets = ["${ARCH}"] } group "default" { - targets = ["amd"] + targets = ["${ARCH}"] } group "deploy" { targets = ["multi"] } group "private" { targets = [ - "amd", - "arm" + "amd64", + "arm64" ] } # intended for use with default local docker builder # uses 'dev' group in docker-bake.hcl # assume dev machine is amd64 machine -target "amd" { +target "amd64" { context = "." dockerfile = "Dockerfile" args = { @@ -61,8 +64,8 @@ target "amd" { # intended for use with default docker driver on an arm64 machine # use with 'arm' group -target "arm" { - inherits = ["amd"] +target "arm64" { + inherits = ["amd64"] tags = tag("arm64") platforms = ["linux/arm64"] } @@ -70,7 +73,7 @@ target "arm" { # must use with docker-container driver for multiarch image deployment to registry # uses 'deploy' group in docker-bake.hcl target "multi" { - inherits = ["amd"] + inherits = ["amd64"] tags = tag("") platforms = ["linux/amd64", "linux/arm64"] output = ["type=registry"] diff --git a/examples/.env b/examples/.env new file mode 100644 index 0000000..c169afd --- /dev/null +++ b/examples/.env @@ -0,0 +1,6 @@ +SYSADMIN_PW=ucommandit +# default is alpine +# LINUX_DISTRO=alpine +RUSER=ucommandit +# TARGET=deploy +BUILD_SRC=../src diff --git a/install b/install index cd96516..ce07ebe 100755 --- a/install +++ b/install @@ -2,7 +2,7 @@ builder=$(dirname "$(realpath "$BASH_SOURCE")") target=$1 [[ $# -eq 2 ]] && target=$1/$2 -target=${target:-/opt/bin/dbuild} +target=${target:-/opt/bin/udbuild} cmd=$(basename $target) parent=$(dirname $target) echo NOTE: the uci build script can be called directly at $builder @@ -11,7 +11,7 @@ declare -a a="(${PATH//:/ })" for i in ${a[*]}; do [[ $i == $parent ]] && found=true; done if [[ $found ]]; then echo creating a link \'$cmd\' in \'$parent\' to \'$builder\' - if ln -ns $builder/build ${1:-/opt/bin/dbuild}; then + if ln -ns $builder/build $target; then [[ ! $(command -v $cmd) ]] && echo FATAL: link failed $cmd not found in path \ || echo install success: try \'$cmd -h\' now else @@ -22,6 +22,8 @@ if [[ $found ]]; then echo $parent not in current path echo $PATH echo link to script not created - echo Alternatively export an environment variable with $builder - echo and use that to invoke the script + echo "add the following export somewhere in your shell (e.g. ~/.bashrc)" + echo "export UDBUILD=$builder/build" + echo 'and then use $UDBUILD to invoke the build script ( e.g $UDBUILD -e mybuild.env)' + echo "or rerun this script using a directory in the system path (e.g ./install /usr/bin build)" fi \ No newline at end of file diff --git a/lib/cmds/image-push b/lib/cmds/image-push index 003d792..7070133 100755 --- a/lib/cmds/image-push +++ b/lib/cmds/image-push @@ -7,11 +7,16 @@ image_push () { # $1 name, $2 user(or repo), $3 repo declare OPTION; declare OPTARG; declare OPTIND -while getopts 'e:dt:r:u:' OPTION; do +while getopts 'ae:pt:r:u:' OPTION; do # echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND} case "$OPTION" in -e) if ! source_env_file $OPTARG ; then return 2; fi - ;; + +e) + if ! source_env_file $OPTARG; then return 2; fi + ;; +a) + ARM=arm64 +;; t) TAG=$OPTARG ;; @@ -21,8 +26,8 @@ u) r) REPO=$OPTARG ;; -d) # pull image from dockerhub if not available - hub=true +p) # pull image from dockerhub if not available + PULL=true ;; *) echo unknown run option -$OPTARG echo "USAGE: start " @@ -35,34 +40,35 @@ shift $((OPTIND - 1)) # image tag name=${1:-$NAME} +name=${NAME:-$LINUX_DISTRO} +name=${NAME:-alpine} user=${2:-$RUSER} repo=${3:-$REPO} - source=$([[ $user ]] && echo ${user}/)$name:${TAG:-latest} -source2=$([[ $arm ]] && echo ${source//:/-arm64:} || echo $source) +source2=$([[ $ARM ]] && echo ${source//:/-arm64:} || echo $source) target=$([[ $repo ]] && echo ${repo}/)$source2 if ! docker image inspect $source2 > /dev/null 2>&1; then echo "no image $source2 available to push" - [[ ! $hub ]] &&echo use -h to attempt to pull image from hub.docker.com - if [[ $hub ]]; then + [[ ! $PULL ]] && echo NOTE: use -p or set PULL=true to attempt to pull image from hub.docker.com + if [[ $PULL ]]; then echo attempting to pull $source2 if ! docker pull $source2 > /dev/null 2>&1; then echo unable to pull $source2 from hub.docker.com - platform=$([[ $arm ]] && echo "--platform linux/$arm") + platform=$([[ $ARM ]] && echo "--platform linux/$ARM") echo trying to pull $platform $source from hub.docker.com if ! docker pull $platform $source > /dev/null 2>&1; then echo unable to pull $platform $source, aborting exit 2 else - hub=downloaded + PULL=downloaded source2=$source fi else - hub=downloaded + PULL=downloaded fi else exit 1 @@ -75,7 +81,7 @@ if ! docker image push $target > /dev/null 2>&1; then echo ERROR: unable to push $source2 to repository at $1 as $target fi -if [[ $hub == downloaded ]]; then +if [[ $PULL == downloaded ]]; then echo removing $source2 downloaded from hub.docker.com docker docker image rm $source2 > /dev/null 2>&1 fi diff --git a/lib/helpers.lib b/lib/helpers.lib index 6586c1b..1432276 100755 --- a/lib/helpers.lib +++ b/lib/helpers.lib @@ -1,5 +1,19 @@ #!/bin/bash +get_arch () { + local arch="$(uname -m)" + case "$arch" in + x86_64) arch='amd64' ;; + armhf) arch='armv6' ;; + armv7) arch='armv7' ;; + aarch64) arch='arm64' ;; + ppc64el|ppc64le) arch='ppc64le' ;; + s390x) arch='s390x';; + *) return 1 ;;\ + esac; + echo $arch +} + isAbsPath() { if [[ "${1:0:1}" == / || "${1:0:2}" == ~[/a-z] ]] then @@ -89,7 +103,6 @@ source_env_file () { fi return 2 fi - } docker_image_distro() {