dev-containers/build-image.sh

85 lines
2.3 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
set -e
if [ $# -lt 1 ]; then
echo "Usage: $0 <image-name> [--no-cache]"
echo "Example: $0 base-debian --no-cache"
exit 1
fi
IMAGE_NAME=$1
NO_CACHE=false
if [[ "$2" == "--no-cache" ]]; then
NO_CACHE=true
fi
echo "Building Docker image: $IMAGE_NAME"
if ! command -v dpkg &> /dev/null; then
echo "❌ Error: dpkg not found. This script only works on Debian-based systems."
exit 1
fi
ARCH=$(dpkg --print-architecture) # Outputs amd64, arm64, armhf, etc.
echo "🛠 Detected architecture: $ARCH"
REPO="registry.tomastm.com"
TAG_LATEST="latest"
BUILD_DIR="./${IMAGE_NAME}"
if [ ! -d "${BUILD_DIR}" ]; then
echo "❌ Error: Directory '${BUILD_DIR}' does not exist."
exit 1
fi
_UID=$(id -u)
_GID=$(id -g)
COMMIT_ID=$(git rev-parse --short HEAD)
IMAGE_ARCH="${REPO}/${IMAGE_NAME}:${COMMIT_ID}-${ARCH}"
IMAGE_LATEST="${REPO}/${IMAGE_NAME}:${TAG_LATEST}-${ARCH}"
echo "📦 Running Docker build..."
DOCKER_BUILD_CMD=("docker" "build" "-t" "${IMAGE_ARCH}" "--build-arg" "UID=${_UID}" "--build-arg" "GID=${_GID}")
if [ "$NO_CACHE" = true ]; then
DOCKER_BUILD_CMD+=("--no-cache")
fi
DOCKER_BUILD_CMD+=("${BUILD_DIR}")
"${DOCKER_BUILD_CMD[@]}"
docker tag ${IMAGE_ARCH} ${IMAGE_LATEST}
echo "✅ Successfully built and tagged:"
echo " 📌 ${IMAGE_ARCH}"
echo " 🔄 ${IMAGE_LATEST}"
# Ask whether to push the image
read -p "🚀 Do you want to push the image to ${REPO}? (y/N): " PUSH_CONFIRM
if [[ "$PUSH_CONFIRM" =~ ^[Yy]$ ]]; then
echo "📤 Pushing images..."
docker push ${IMAGE_ARCH}
docker push ${IMAGE_LATEST}
echo "✅ Successfully pushed images to ${REPO}"
else
echo " Skipping push. You can manually push later with:"
echo " docker push ${IMAGE_ARCH}"
echo " docker push ${IMAGE_LATEST}"
exit 0
fi
# After pushing, check if both architectures are available and create the manifest
echo "🔍 Checking if both architectures are pushed..."
if docker manifest inspect "${REPO}/${IMAGE_NAME}:${COMMIT_ID}-amd64" &>/dev/null && \
docker manifest inspect "${REPO}/${IMAGE_NAME}:${COMMIT_ID}-arm64" &>/dev/null; then
echo "📜 Both architectures are available. Creating multi-architecture manifest..."
./create-manifest.sh ${IMAGE_NAME} ${COMMIT_ID}
else
echo "⚠️ Only one architecture is available. Multi-arch manifest will not be created yet."
fi