dev-containers/build-image.sh
2025-02-25 16:03:06 +01:00

68 lines
1.8 KiB
Bash
Executable File
Raw 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 [ $# -eq 0 ]; then
echo "Usage: $0 <image-name>"
echo "Example: $0 base-debian"
exit 1
fi
IMAGE_NAME=${1:-base-debian}
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}"
docker build \
-t ${IMAGE_ARCH} \
--build-arg UID=${_UID} \
--build-arg GID=${_GID} \
${BUILD_DIR}
docker tag ${IMAGE_ARCH} ${IMAGE_LATEST}
echo "✅ Successfully built and tagged:"
echo " 📌 ${IMAGE_ARCH}"
echo " 🔄 ${IMAGE_LATEST}"
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}"
echo " Next step: If you have built and pushed both amd64 and arm64 versions, run the following command to create a multi-arch manifest:"
echo " docker manifest create ${REPO}/${IMAGE_NAME}:${COMMIT_ID} \\"
echo " ${REPO}/${IMAGE_NAME}:${COMMIT_ID}-amd64 \\"
echo " ${REPO}/${IMAGE_NAME}:${COMMIT_ID}-arm64"
echo " docker manifest push ${REPO}/${IMAGE_NAME}:${COMMIT_ID}"
else
echo " Skipping push. You can manually push later with:"
echo " docker push ${IMAGE_ARCH}"
echo " docker push ${IMAGE_LATEST}"
fi