|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# |
| 4 | +# Script builds all Dockerfiles defined in build directory |
| 5 | +# Prints summary after all images have been built (successfully or not) |
| 6 | +# |
| 7 | + |
| 8 | +image_name="$1" |
| 9 | + |
| 10 | +# Checks if image name was provided by user |
| 11 | +if [ -z "${image_name}" ]; then |
| 12 | + echo "Usage: $0 [docker_image_name]" >&2 |
| 13 | + exit -1 |
| 14 | +fi |
| 15 | + |
| 16 | +# Change current path to project root |
| 17 | +script_path=$(dirname "$0") |
| 18 | +cd "${script_path}/../" || exit -1 |
| 19 | + |
| 20 | +# Text formatting |
| 21 | +text_bold=$(tput bold) |
| 22 | +text_red=$(tput setaf 1) |
| 23 | +text_normal=$(tput sgr0) |
| 24 | + |
| 25 | +# Summary arrays |
| 26 | +build_failed=() |
| 27 | +build_done=() |
| 28 | + |
| 29 | +# Loop through all available directories in ./build |
| 30 | +for i in ./build/*/; do |
| 31 | + version=$(basename "$i"); |
| 32 | + image="${image_name}:${version}" |
| 33 | + |
| 34 | + echo "${text_bold}* Building ${image} ${text_normal}" |
| 35 | + |
| 36 | + # Builds image and check for return code |
| 37 | + if docker build --pull -t "${image}" "./build/${version}"; then |
| 38 | + build_done+=( "${image}" ) |
| 39 | + else |
| 40 | + echo "${text_bold}${text_red}* ERROR when building ${image} ${text_normal}" |
| 41 | + build_failed+=( "${image}" ) |
| 42 | + fi |
| 43 | +done |
| 44 | + |
| 45 | + |
| 46 | +# Prints summary for successful builds |
| 47 | +if [ "${#build_done[@]}" -gt 0 ]; then |
| 48 | + echo "${text_bold}" |
| 49 | + echo "Docker has successfully built the following images:${text_normal}" |
| 50 | + for img in "${build_done[@]}"; do |
| 51 | + echo "* ${img}" |
| 52 | + done |
| 53 | + echo -n "${text_normal}" |
| 54 | +fi |
| 55 | + |
| 56 | +# Prints summary for failed builds |
| 57 | +if [ "${#build_failed[@]}" -gt 0 ]; then |
| 58 | + echo "${text_red}" |
| 59 | + echo "Docker failed when building the following images:" |
| 60 | + for img in "${build_failed[@]}"; do |
| 61 | + echo "${text_red}* ${img}${text_normal}" |
| 62 | + done |
| 63 | +fi |
0 commit comments