#!/bin/bash image_push () { local DIR DIR=$(cd "$(dirname "$BASH_SOURCE")" >/dev/null 2>&1 ; pwd -P ) #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 [[ $# -lt 1 ]] && echo "image base name required" && exit declare OPTION; declare OPTARG; declare OPTIND while getopts 'aht:r:u:' OPTION; do # echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND} case "$OPTION" in r) REPO=$OPTARG ;; u) RUSER=$OPTARG ;; t) TAG=$OPTARG ;; h) # pull image from dockerhub if not available hub=true ;; a) # arm image arm=arm64 ;; *) 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 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" [[ ! $hub ]] &&echo use -h to attempt to pull image from hub.docker.com if [[ $hub ]]; 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 hub=downloaded source2=$source fi else hub=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 [[ $hub == 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 $@