|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# This script is used to build a Docker image for Icinga 2. |
| 4 | + |
| 5 | +# Action defines the type of build to perform. It can be one of: |
| 6 | +# |
| 7 | +# - load: Build the image for the building platform and load it into the local Docker engine image storage. |
| 8 | +# - push: Build the image for all specified platforms and push them to the Docker registry (icinga/icinga2:<TAG>). |
| 9 | +# - cache: Build the image for all specified platforms using the default Buildx's exporter (cacheonly) and will just |
| 10 | +# create a cache for each platform. There will be no images that you can use but any subsequent build will use the |
| 11 | +# cache and speed up the build process. |
| 12 | +# |
| 13 | +# If no action is provided, it defaults to "load", and any specified platforms will be ignored. |
| 14 | +ACTION="${1:-load}" |
| 15 | +# Tag defines the tag to use for the built image. If not provided, it defaults to "test". |
| 16 | +# The resulting image will be tagged as "icinga/icinga2:<TAG>". |
| 17 | +TAG="${2:-test}" |
| 18 | +# Platform defines the platforms to build the image for. It defaults to "linux/amd64,linux/arm64/v8". |
| 19 | +PLATFORM="${3:-linux/amd64,linux/arm64/v8}" |
| 20 | +# ROOT_DIR is the root directory of the Icinga 2 source code repository and where the Containerfile is located. |
| 21 | +# It's also used to determine the build context for the Docker build, so that every file referenced in the |
| 22 | +# Containerfile can be found relative to this directory by Docker. |
| 23 | +ROOT_DIR="$(dirname "$(realpath "$0")")/../.." |
| 24 | + |
| 25 | +if ! command -v docker &> /dev/null; then |
| 26 | + echo "Docker is not installed. Please install Docker to build the image." >&2 |
| 27 | + exit 1 |
| 28 | +fi |
| 29 | + |
| 30 | +if ! command -v docker buildx &> /dev/null; then |
| 31 | + echo "Docker Buildx is not installed. Please install Docker Buildx to build the image." >&2 |
| 32 | + exit 1 |
| 33 | +fi |
| 34 | + |
| 35 | +# Set up the buildx command with the specified platform. |
| 36 | +BuildX=(docker buildx build --platform "${PLATFORM}") |
| 37 | +BUILD_ARGS=(-t "icinga/icinga2:${TAG}" -f "${ROOT_DIR}"/Containerfile "${ROOT_DIR}") |
| 38 | + |
| 39 | +# Check the action and perform the appropriate build command. |
| 40 | +case "$ACTION" in |
| 41 | + push) |
| 42 | + "${BuildX[@]}" --push "${BUILD_ARGS[@]}" |
| 43 | + ;; |
| 44 | + load) |
| 45 | + docker buildx build --load "${BUILD_ARGS[@]}" |
| 46 | + ;; |
| 47 | + cache) |
| 48 | + "${BuildX[@]}" "${BUILD_ARGS[@]}" |
| 49 | + ;; |
| 50 | + *) |
| 51 | + echo "Unknown action: ${ACTION}. Valid actions are: push, load, cache." >&2 |
| 52 | + exit 1 |
| 53 | +esac |
0 commit comments