4NK_dev/scripts/build_and_push.sh

85 lines
2.3 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
REGISTRY=${REGISTRY:-git.4nkweb.com}
NAMESPACE=${NAMESPACE:-4nk}
# Support d'un paramètre positionnel prioritaire pour le tag
if [ "${1:-}" != "" ]; then
DOCKER_TAG="$1"
fi
if [ -z "${DOCKER_TAG:-}" ]; then
echo "[docker][erreur] DOCKER_TAG non défini (obligatoire). Fournissez-le en argument ou via l'environnement." >&2
exit 1
fi
echo "[docker] registry=${REGISTRY} namespace=${NAMESPACE} tag=${DOCKER_TAG}"
if ! command -v docker >/dev/null 2>&1; then
echo "[docker][erreur] docker introuvable" >&2
exit 1
fi
if [ ! -f .gitmodules ]; then
echo "[docker][erreur] .gitmodules introuvable dans $(pwd)" >&2
exit 1
fi
# Prépare le répertoire de logs
LOG_DIR="log/ci/${DOCKER_TAG}"
mkdir -p "${LOG_DIR}"
SUMMARY_FILE="${LOG_DIR}/summary.txt"
: > "${SUMMARY_FILE}"
# Parcourt les submodules déclarés
mapfile -t submodules < <(git config -f .gitmodules --name-only --get-regexp 'submodule\..*\.path' | sed -E 's/\.path$//')
for name in "${submodules[@]}"; do
path=$(git config -f .gitmodules --get "${name}.path" || true)
url=$(git config -f .gitmodules --get "${name}.url" || true)
branch=$(git config -f .gitmodules --get "${name}.branch" || echo "master")
if [ -z "${path}" ] || [ -z "${url}" ]; then
echo "[docker][warn] entrée incomplète pour ${name}, ignoré" >&2
continue
fi
image_name=$(basename "${path}")
full_ref="${REGISTRY}/${NAMESPACE}/${image_name}:${DOCKER_TAG}"
if [ ! -d "${path}" ]; then
echo "[docker][warn] sous-dossier ${path} manquant, on saute" >&2
continue
fi
if [ ! -f "${path}/Dockerfile" ]; then
echo "[docker] aucun Dockerfile dans ${path}, ignoré"
continue
fi
log_file="${LOG_DIR}/$(basename "${path}").log"
echo "[docker] build ${full_ref} depuis ${path} (branche ${branch}) | log: ${log_file}"
{
echo "== BUILD ${full_ref} =="
date -Is
docker buildx build \
--pull \
--ssh default \
--progress=plain \
--tag "${full_ref}" \
"${path}"
} 2>&1 | tee -a "${log_file}"
echo "[docker] push ${full_ref} | log: ${log_file}"
{
echo "== PUSH ${full_ref} =="
date -Is
docker push "${full_ref}"
} 2>&1 | tee -a "${log_file}"
echo "${full_ref} ${log_file}" >> "${SUMMARY_FILE}"
done
echo "[docker] terminé"
echo "[docker] récapitulatif: ${SUMMARY_FILE}"