Docker: Add --ask-push option to build.sh

This commit is contained in:
Gernot Schulz 2020-08-19 16:45:12 +02:00 committed by Finn Stutzenstein
parent 9ddf9ddb8c
commit 8faa2ad38f
No known key found for this signature in database
GPG Key ID: 9042F605C6324654

View File

@ -27,6 +27,7 @@ Options:
-D, --docker-repo Specify a Docker repository -D, --docker-repo Specify a Docker repository
(default: unspecified, i.e., system default) (default: unspecified, i.e., system default)
-t, --tag Tag the Docker image (default: $DOCKER_TAG) -t, --tag Tag the Docker image (default: $DOCKER_TAG)
--ask-push Offer to push newly built images to registry
--no-cache Pass --no-cache to docker-build --no-cache Pass --no-cache to docker-build
EOF EOF
} }
@ -38,7 +39,7 @@ if [[ -f "$CONFIG" ]]; then
fi fi
shortopt="hr:D:t:" shortopt="hr:D:t:"
longopt="help,docker-repo:,tag:,no-cache" longopt="help,docker-repo:,tag:,ask-push,no-cache"
ARGS=$(getopt -o "$shortopt" -l "$longopt" -n "$ME" -- "$@") ARGS=$(getopt -o "$shortopt" -l "$longopt" -n "$ME" -- "$@")
if [ $? -ne 0 ]; then usage; exit 1; fi if [ $? -ne 0 ]; then usage; exit 1; fi
eval set -- "$ARGS"; eval set -- "$ARGS";
@ -55,6 +56,10 @@ while true; do
DOCKER_TAG="$2" DOCKER_TAG="$2"
shift 2 shift 2
;; ;;
--ask-push)
ASK_PUSH=1
shift 1
;;
--no-cache) --no-cache)
OPTIONS+="--no-cache" OPTIONS+="--no-cache"
shift 1 shift 1
@ -91,9 +96,22 @@ for i in "${SELECTED_TARGETS[@]}"; do
else else
docker build --tag "$img" --pull "${OPTIONS[@]}" "$loc" docker build --tag "$img" --pull "${OPTIONS[@]}" "$loc"
fi fi
BUILT_IMAGES+=("$img OFF") BUILT_IMAGES+=("$img ON")
done done
if [[ "${#BUILT_IMAGES[@]}" -ge 1 ]]; then
printf "\nSuccessfully built images:\n\n"
for i in "${BUILT_IMAGES[@]}"; do
read -r img x <<< "$i"
printf " - $img\n"
done
else
echo "No images were built."
exit 3
fi
[[ "$ASK_PUSH" ]] || exit 0
if hash whiptail > /dev/null 2>&1; then if hash whiptail > /dev/null 2>&1; then
while read img; do while read img; do
echo "Pushing ${img}." echo "Pushing ${img}."
@ -104,9 +122,13 @@ if hash whiptail > /dev/null 2>&1; then
${BUILT_IMAGES[@]} \ ${BUILT_IMAGES[@]} \
3>&2 2>&1 1>&3 ) 3>&2 2>&1 1>&3 )
else else
printf "\nSuccessfully built images:\n\n" echo
for i in "${BUILT_IMAGES[@]}"; do for i in "${BUILT_IMAGES[@]}"; do
read -r img x <<< "$i" read -r img x <<< "$i"
printf " - $img\n" read -p "Push image '$img' to repository? [Y/n] " REPL
case "$REPL" in
N|n|No|no|NO) exit 0;;
*) docker push "$img" ;;
esac
done done
fi fi