2022-10-20 18:48:17 -07:00
|
|
|
#!/bin/bash
|
2023-01-17 15:20:30 -08:00
|
|
|
|
|
|
|
image_push () {
|
|
|
|
|
2022-10-20 18:48:17 -07:00
|
|
|
#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
|
2023-04-01 07:17:43 -07:00
|
|
|
while getopts 'ae:pt:r:u:' OPTION; do
|
2022-10-20 18:48:17 -07:00
|
|
|
# echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND}
|
|
|
|
case "$OPTION" in
|
2023-04-01 07:17:43 -07:00
|
|
|
|
|
|
|
e)
|
|
|
|
if ! source_env_file $OPTARG; then return 2; fi
|
|
|
|
;;
|
|
|
|
a)
|
|
|
|
ARM=arm64
|
|
|
|
;;
|
2023-03-27 11:07:25 -07:00
|
|
|
t)
|
|
|
|
TAG=$OPTARG
|
|
|
|
;;
|
|
|
|
u)
|
|
|
|
RUSER=$OPTARG
|
|
|
|
;;
|
|
|
|
r)
|
|
|
|
REPO=$OPTARG
|
|
|
|
;;
|
2023-04-01 07:17:43 -07:00
|
|
|
p) # pull image from dockerhub if not available
|
|
|
|
PULL=true
|
2022-10-20 18:48:17 -07:00
|
|
|
;;
|
|
|
|
*) 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
|
2023-03-27 11:07:25 -07:00
|
|
|
name=${1:-$NAME}
|
2023-04-01 07:17:43 -07:00
|
|
|
name=${NAME:-$LINUX_DISTRO}
|
|
|
|
name=${NAME:-alpine}
|
2022-10-20 18:48:17 -07:00
|
|
|
user=${2:-$RUSER}
|
|
|
|
repo=${3:-$REPO}
|
|
|
|
|
|
|
|
source=$([[ $user ]] && echo ${user}/)$name:${TAG:-latest}
|
|
|
|
|
2023-04-01 07:17:43 -07:00
|
|
|
source2=$([[ $ARM ]] && echo ${source//:/-arm64:} || echo $source)
|
2022-10-20 18:48:17 -07:00
|
|
|
|
|
|
|
target=$([[ $repo ]] && echo ${repo}/)$source2
|
|
|
|
|
|
|
|
if ! docker image inspect $source2 > /dev/null 2>&1; then
|
|
|
|
echo "no image $source2 available to push"
|
2023-04-01 07:17:43 -07:00
|
|
|
[[ ! $PULL ]] && echo NOTE: use -p or set PULL=true to attempt to pull image from hub.docker.com
|
|
|
|
if [[ $PULL ]]; then
|
2022-10-20 18:48:17 -07:00
|
|
|
echo attempting to pull $source2
|
|
|
|
if ! docker pull $source2 > /dev/null 2>&1; then
|
|
|
|
echo unable to pull $source2 from hub.docker.com
|
2023-04-01 07:17:43 -07:00
|
|
|
platform=$([[ $ARM ]] && echo "--platform linux/$ARM")
|
2022-10-20 18:48:17 -07:00
|
|
|
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
|
2023-04-01 07:17:43 -07:00
|
|
|
PULL=downloaded
|
2022-10-20 18:48:17 -07:00
|
|
|
source2=$source
|
|
|
|
fi
|
|
|
|
else
|
2023-04-01 07:17:43 -07:00
|
|
|
PULL=downloaded
|
2022-10-20 18:48:17 -07:00
|
|
|
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
|
|
|
|
|
2023-04-01 07:17:43 -07:00
|
|
|
if [[ $PULL == downloaded ]]; then
|
2022-10-20 18:48:17 -07:00
|
|
|
echo removing $source2 downloaded from hub.docker.com docker
|
|
|
|
docker image rm $source2 > /dev/null 2>&1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo removing tag $target
|
2023-01-17 15:20:30 -08:00
|
|
|
docker image rm $target > /dev/null 2>&1
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# if script was executed then call the function
|
|
|
|
(return 0 2>/dev/null) || image_push $@
|