uci-docker-build/lib/cmds/image-push

89 lines
2.3 KiB
Plaintext
Raw Normal View History

#!/bin/bash
image_push () {
#tags an image and pushes it to a <custom private> 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 'e:dt: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
;;
t)
TAG=$OPTARG
;;
u)
RUSER=$OPTARG
;;
r)
REPO=$OPTARG
;;
d) # pull image from dockerhub if not available
hub=true
;;
*) echo unknown run option -$OPTARG
echo "USAGE: start <options>"
echo "available options: -h pull from hub.docker.com if not available, -a push arm64 image, -t <latest> custom tag "
;;
esac
done
shift $((OPTIND - 1))
# image tag
name=${1:-$NAME}
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 $@