#!/bin/bash image_push () { #tags an image and pushes it to a repository # if not reposity is given will use docker.io and push to hub.docker.com # $1 name, $2 user(or repo), $3 repo declare OPTION; declare OPTARG; declare OPTIND 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 ;; a) ARM=arm64 ;; t) TAG=$OPTARG ;; u) RUSER=$OPTARG ;; r) REPO=$OPTARG ;; p) # pull image from dockerhub if not available PULL=true ;; *) echo unknown run option -$OPTARG echo "USAGE: start " echo "available options: -h pull from hub.docker.com if not available, -a push arm64 image, -t custom tag " ;; esac done 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) target=$([[ $repo ]] && echo ${repo}/)$source2 if ! docker image inspect $source2 > /dev/null 2>&1; then echo "no image $source2 available to push" [[ ! $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") 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 PULL=downloaded source2=$source fi else PULL=downloaded fi else exit 1 fi fi echo pushing $source2 to $target docker tag $source2 $target if ! docker image push $target > /dev/null 2>&1; then echo ERROR: unable to push $source2 to repository at $1 as $target fi if [[ $PULL == downloaded ]]; then echo removing $source2 downloaded from hub.docker.com docker docker image rm $source2 > /dev/null 2>&1 fi echo removing tag $target docker image rm $target > /dev/null 2>&1 } # if script was executed then call the function (return 0 2>/dev/null) || image_push $@