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

98 lines
2.6 KiB
Bash
Executable File

#!/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
source=$$image_name
declare OPTION; declare OPTARG; declare OPTIND
while getopts 'e:aht:r:u:' OPTION; do
# echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND}
case "$OPTION" in
e)
efile=$OPTARG
;;
h) # 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))
SDIR=$1
shift
[[ $efile ]] && efile = $SDIR/$efile
if [[ -f $efile ]]; then
source $efile
[[ ! $? -eq 0 ]] && echo source of $efile failed, exiting && return 2
else
echo no environment file at $efile, exiting
return 2
fi
echo "----------"
echo loaded environment filen $efile
cat $efile
echo "----------"
# 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 $@