refactor: enhance build and push script with commit info and image tracking

This commit is contained in:
Marco Pedone 2025-11-18 18:29:41 +01:00
parent 40bd07ded3
commit b75e7f2412

View file

@ -1,4 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# filepath: e:\infoalloggi-monorepo\build_and_push_ghcr.sh
set -euo pipefail set -euo pipefail
# Build and push multiple Docker images to a registry (GHCR or self-hosted) # Build and push multiple Docker images to a registry (GHCR or self-hosted)
@ -36,6 +37,18 @@ else
echo "No credentials provided; assuming you're already logged into ${IMAGE_REGISTRY} or using a credential helper." echo "No credentials provided; assuming you're already logged into ${IMAGE_REGISTRY} or using a credential helper."
fi fi
# Get last git commit info (once for all images)
GIT_COMMIT_ID=$(git rev-parse --short HEAD)
GIT_COMMIT_MSG=$(git log -1 --pretty=%B | tr -d '\n')
DESCRIPTION="Commit: ${GIT_COMMIT_ID} - ${GIT_COMMIT_MSG}"
# Array to store built images
declare -a BUILT_IMAGES=()
echo "================================"
echo "PHASE 1: Building all images"
echo "================================"
for svc in ${SERVICES}; do for svc in ${SERVICES}; do
case "${svc}" in case "${svc}" in
db) db)
@ -56,17 +69,40 @@ for svc in ${SERVICES}; do
;; ;;
esac esac
# Get last git commit info
GIT_COMMIT_ID=$(git rev-parse --short HEAD)
GIT_COMMIT_MSG=$(git log -1 --pretty=%B | tr -d '\n')
DESCRIPTION="Commit: ${GIT_COMMIT_ID} - ${GIT_COMMIT_MSG}"
IMAGE="${IMAGE_REGISTRY}/${OWNER}/${PREFIX}-${svc}:${TAG}" IMAGE="${IMAGE_REGISTRY}/${OWNER}/${PREFIX}-${svc}:${TAG}"
echo ""
echo "Building ${PREFIX}-${svc} -> ${IMAGE} (context: ${ctx})" echo "Building ${PREFIX}-${svc} -> ${IMAGE} (context: ${ctx})"
docker build --build-arg ENV_FILE="${ENV_FILE}" -t "${IMAGE}" --label "org.opencontainers.image.description=${DESCRIPTION}" -f "$ctx/$dockerfile" "$ctx"
echo "Pushing ${IMAGE}" docker build \
docker push "${IMAGE}" --build-arg ENV_FILE="${ENV_FILE}" \
-t "${IMAGE}" \
--label "org.opencontainers.image.description=${DESCRIPTION}" \
-f "$ctx/$dockerfile" \
"$ctx"
# Store the image name for later pushing
BUILT_IMAGES+=("${IMAGE}")
echo "✓ Built ${IMAGE}"
done done
echo "All done. Built and pushed services: ${SERVICES} with tag ${TAG} to ${IMAGE_REGISTRY}/${OWNER}/${PREFIX}-<service>:${TAG}" echo ""
echo "================================"
echo "PHASE 2: Pushing all images"
echo "================================"
for IMAGE in "${BUILT_IMAGES[@]}"; do
echo ""
echo "Pushing ${IMAGE}"
docker push "${IMAGE}"
echo "✓ Pushed ${IMAGE}"
done
echo ""
echo "================================"
echo "All done!"
echo "Built and pushed ${#BUILT_IMAGES[@]} images with tag ${TAG}"
echo "Images:"
for IMAGE in "${BUILT_IMAGES[@]}"; do
echo " - ${IMAGE}"
done
echo "================================"